From 83264731f315338502ebe7abcff649d6c30ecd4b Mon Sep 17 00:00:00 2001 From: MarcoE Date: Mon, 15 Sep 2025 17:11:17 +0200 Subject: [PATCH] Migliorato modal activity --- .../SingleElements/Modal/ActivityForm.razor | 285 +++++++++++------- 1 file changed, 175 insertions(+), 110 deletions(-) diff --git a/salesbook.Shared/Components/SingleElements/Modal/ActivityForm.razor b/salesbook.Shared/Components/SingleElements/Modal/ActivityForm.razor index 70193ff..4689b9a 100644 --- a/salesbook.Shared/Components/SingleElements/Modal/ActivityForm.razor +++ b/salesbook.Shared/Components/SingleElements/Modal/ActivityForm.razor @@ -40,19 +40,18 @@
Commessa - @if (Commesse.IsNullOrEmpty()) - { - Nessuna commessa presente - } - else - { - - @foreach (var com in Commesse) - { - @($"{com.CodJcom} - {com.Descrizione}") - } - - } +
@@ -91,13 +90,19 @@
Assegnata a - - - @foreach (var user in Users) - { - @user.FullName - } - +
@@ -150,7 +155,7 @@ } } } - + @if (ActivityFileList != null) { foreach (var file in ActivityFileList) @@ -210,10 +215,10 @@ - + - + a.Type == AttachedDTO.TypeAttached.Position) + .Select(async attached => + { var position = new PositionDTO { Description = attached.Description, Lat = attached.Lat, Lng = attached.Lng }; - ActivityModel.Position = await IntegryApiService.SavePosition(position); - } - } + }); + + await Task.WhenAll(positionTasks); } private async Task SaveAttached(string activityId) { - if (AttachedList != null) - { - foreach (var attached in AttachedList) - { - if (attached.FileContent is not null && attached.Type != AttachedDTO.TypeAttached.Position) - { - await IntegryApiService.UploadFile(activityId, attached.FileBytes, attached.Name); - } - } - } + if (AttachedList is null) return; + + var uploadTasks = AttachedList + .Where(a => a.FileContent is not null && a.Type != AttachedDTO.TypeAttached.Position) + .Select(a => IntegryApiService.UploadFile(activityId, a.FileBytes, a.Name)); + + await Task.WhenAll(uploadTasks); } private bool CheckPreSave() { Snackbar.Clear(); - if (!ActivityModel.ActivityTypeId.IsNullOrEmpty()) return true; - Snackbar.Add("Tipo attività obbligatorio!", Severity.Error); + Snackbar.Add("Tipo attività obbligatorio!", Severity.Error); return false; } @@ -389,13 +395,14 @@ return Task.Run(async () => { if (!IsNew && Id != null) - { ActivityFileList = await IntegryApiService.GetActivityFile(Id); - } Users = await ManageData.GetTable(); + if (!ActivityModel.UserName.IsNullOrEmpty()) + SelectedUser = Users.FindLast(x => x.UserName.Equals(ActivityModel.UserName)); + ActivityResult = await ManageData.GetTable(); - Clienti = await ManageData.GetClienti(new WhereCondContact {FlagStato = "A"}); + Clienti = await ManageData.GetClienti(new WhereCondContact { FlagStato = "A" }); Pros = await ManageData.GetProspect(); await InvokeAsync(StateHasChanged); @@ -404,76 +411,137 @@ private async Task LoadActivityType() { - if (ActivityModel.UserName is null) ActivityType = []; + if (ActivityModel.UserName is null) + { + ActivityType = []; + return; + } - ActivityType = await ManageData.GetTable(x => - x.UserName != null && x.UserName.Equals(ActivityModel.UserName) + ActivityType = await ManageData.GetTable(x => x.UserName != null && x.UserName.Equals(ActivityModel.UserName) ); } - private async Task LoadCommesse() => - Commesse = await ManageData.GetTable(x => x.CodAnag != null && x.CodAnag.Equals(ActivityModel.CodAnag)); - - private async Task?> SearchCliente(string value, CancellationToken token) + private async Task LoadCommesse() { - if (string.IsNullOrEmpty(value)) - return null; + if (_lastLoadedCodAnag == ActivityModel.CodAnag) return; - var listToReturn = new List(); - - listToReturn.AddRange( - Clienti.Where(x => x.RagSoc.Contains(value, StringComparison.OrdinalIgnoreCase)).Select(x => $"{x.CodAnag} - {x.RagSoc}") - ); - - listToReturn.AddRange( - Pros.Where(x => x.RagSoc.Contains(value, StringComparison.OrdinalIgnoreCase)).Select(x => $"{x.CodPpro} - {x.RagSoc}") - ); - - return listToReturn; + Commesse = await ManageData.GetTable(x => x.CodAnag == ActivityModel.CodAnag); + Commesse = Commesse.OrderByDescending(x => x.CodJcom).ToList(); + _lastLoadedCodAnag = ActivityModel.CodAnag; } - private async Task OnClienteChanged() + private async Task> SearchCommesseAsync(string value, CancellationToken token) + { + await LoadCommesse(); + if (Commesse.IsNullOrEmpty()) return []; + + IEnumerable list; + + if (string.IsNullOrWhiteSpace(value)) + { + list = Commesse.OrderByDescending(x => x.CodJcom).Take(10); + } + else + { + list = Commesse + .Where(x => (x.CodJcom.Contains(value, StringComparison.OrdinalIgnoreCase) + || x.Descrizione.Contains(value, StringComparison.OrdinalIgnoreCase)) + && (x.CodAnag == ActivityModel.CodAnag || ActivityModel.CodAnag == null)) + .OrderByDescending(x => x.CodJcom) + .Take(50); + } + + return token.IsCancellationRequested ? [] : list; + } + + private Task> SearchUtentiAsync(string value, CancellationToken token) + { + IEnumerable list; + + if (string.IsNullOrWhiteSpace(value)) + { + list = Users.OrderBy(u => u.FullName).Take(50); + } + else + { + list = Users + .Where(x => x.UserName.Contains(value, StringComparison.OrdinalIgnoreCase) + || x.FullName.Contains(value, StringComparison.OrdinalIgnoreCase)) + .OrderBy(u => u.FullName) + .Take(50); + } + + return Task.FromResult(token.IsCancellationRequested ? [] : list); + } + + private Task?> SearchCliente(string value, CancellationToken token) + { + if (string.IsNullOrWhiteSpace(value)) + return Task.FromResult?>(null); + + var results = new List(); + + results.AddRange(Clienti + .Where(x => x.RagSoc.Contains(value, StringComparison.OrdinalIgnoreCase)) + .Select(x => $"{x.CodAnag} - {x.RagSoc}")); + + results.AddRange(Pros + .Where(x => x.RagSoc.Contains(value, StringComparison.OrdinalIgnoreCase)) + .Select(x => $"{x.CodPpro} - {x.RagSoc}")); + + return Task.FromResult?>(results); + } + + private Task OnClienteChanged() { ActivityModel.CodJcom = null; + if (string.IsNullOrWhiteSpace(ActivityModel.Cliente)) return Task.CompletedTask; - if (ActivityModel.Cliente.IsNullOrEmpty()) return; + var parts = ActivityModel.Cliente.Split('-', 2, StringSplitOptions.TrimEntries); + if (parts.Length == 2) + { + ActivityModel.CodAnag = parts[0]; + ActivityModel.Cliente = parts[1]; + } - var match = Regex.Match(ActivityModel.Cliente!, @"^\s*(\S+)\s*-\s*(.*)$"); - if (!match.Success) - return; + OnAfterChangeValue(); + return Task.CompletedTask; + } - ActivityModel.CodAnag = match.Groups[1].Value; - ActivityModel.Cliente = match.Groups[2].Value; + private async Task OnCommessaSelectedAfter() + { + var com = SelectedComessa; + if (com != null) + { + ActivityModel.CodJcom = com.CodJcom; + ActivityModel.Commessa = com.Descrizione; + } + else + { + ActivityModel.CodJcom = null; + ActivityModel.Commessa = null; + } - await LoadCommesse(); OnAfterChangeValue(); } - private async Task OnCommessaChanged() - { - ActivityModel.Commessa = (await ManageData.GetTable(x => x.CodJcom.Equals(ActivityModel.CodJcom))).Last().Descrizione; - OnAfterChangeValue(); - } - - private async Task OnUserChanged() + private async Task OnUserSelectedAfter() { + ActivityModel.UserName = SelectedUser?.UserName; await LoadActivityType(); OnAfterChangeValue(); } private void OnAfterChangeTimeBefore() { - if (ActivityModel.EstimatedTime != null) + if (ActivityModel.EstimatedTime is not null) { - if (ActivityModel.MinuteBefore != -1) + ActivityModel.NotificationDate = ActivityModel.MinuteBefore switch { - ActivityModel.NotificationDate = ActivityModel.MinuteBefore == 0 ? - ActivityModel.EstimatedTime : ActivityModel.EstimatedTime.Value.AddMinutes(ActivityModel.MinuteBefore * -1); - } - else - { - ActivityModel.NotificationDate = null; - } + -1 => null, + 0 => ActivityModel.EstimatedTime, + _ => ActivityModel.EstimatedTime.Value.AddMinutes(ActivityModel.MinuteBefore * -1) + }; } OnAfterChangeValue(); @@ -482,24 +550,21 @@ private void OnAfterChangeValue() { if (!IsNew) - { LabelSave = !OriginalModel.Equals(ActivityModel) ? "Aggiorna" : null; - } - if (ActivityModel.EstimatedEndtime <= ActivityModel.EstimatedTime) + if (ActivityModel.EstimatedTime is not null && + (ActivityModel.EstimatedEndtime is null || ActivityModel.EstimatedEndtime <= ActivityModel.EstimatedTime)) { ActivityModel.EstimatedEndtime = ActivityModel.EstimatedTime.Value.AddHours(1); } + + StateHasChanged(); } - private async Task OnAfterChangeEsito() + private Task OnAfterChangeEsito() { OnAfterChangeValue(); - - // var result = await ConfirmMemo.ShowAsync(); - - // if (result is true) - // OpenAddMemo = !OpenAddMemo; + return Task.CompletedTask; } private void OpenSelectEsito() @@ -637,7 +702,7 @@ } } - private static string AdjustCoordinate(double coordinate) => + private static string AdjustCoordinate(double coordinate) => coordinate.ToString(CultureInfo.InvariantCulture).Replace(",", "."); } \ No newline at end of file