| | 1 | | @using Syki.Front.Features.Academic.GetCourses |
| | 2 | |
|
| | 3 | | @namespace Syki.Front.Features.Academic.CreateDiscipline |
| | 4 | |
|
| | 5 | | <MudDialog Class="pb-2" DefaultFocus="DefaultFocus.None"> |
| | 6 | | <TitleContent> |
| | 7 | | <SykiDialogTitle Text="Nova Disciplina" /> |
| | 8 | | </TitleContent> |
| | 9 | | <DialogContent> |
| 0 | 10 | | <MudForm @ref="@_form" Class="pt-1"> |
| 0 | 11 | | <SykiTextField Label="Nome" AutoFocus @bind-Value="@_name" /> |
| 0 | 12 | | <MudSelect |
| 0 | 13 | | Dense |
| 0 | 14 | | Margin="Margin.Dense" |
| 0 | 15 | | Variant="Variant.Outlined" |
| | 16 | | Class="mb-2" |
| | 17 | | T="CourseOut" |
| | 18 | | Label="Cursos" |
| | 19 | | MultiSelection |
| | 20 | | Required |
| | 21 | | RequiredError="Informe!" |
| | 22 | | @bind-SelectedValues="@SelectedCourses" |
| | 23 | | MultiSelectionTextFunc="@(new Func<List<string>, string>(GetMultiSelectionText))" |
| | 24 | | > |
| 0 | 25 | | @foreach (var course in _courses) |
| | 26 | | { |
| 0 | 27 | | <MudSelectItem Class="d-flex align-start gap-0" T="CourseOut" Value="@course">@course.Name</MudSelec |
| | 28 | | } |
| | 29 | | </MudSelect> |
| | 30 | | </MudForm> |
| | 31 | | </DialogContent> |
| | 32 | | <DialogActions> |
| | 33 | | <DialogCancelButton OnClick="@Cancel" /> |
| | 34 | | <SykiProgressCircular Loading="@_loading" /> |
| | 35 | | <DialogSaveButton OnClick="@Submit" /> |
| | 36 | | </DialogActions> |
| | 37 | | </MudDialog> |
| | 38 | |
|
| | 39 | | @inject ISnackbar Snackbar |
| | 40 | | @inject GetCoursesClient GetCoursesClient |
| | 41 | | @inject CreateDisciplineClient CreateDisciplineClient |
| | 42 | |
|
| | 43 | | @code |
| | 44 | | { |
| | 45 | | [CascadingParameter] |
| 0 | 46 | | IMudDialogInstance MudDialog { get; set; } |
| | 47 | |
|
| | 48 | | MudForm _form; |
| | 49 | | private bool _loading; |
| | 50 | | private string _name; |
| | 51 | |
|
| 0 | 52 | | private List<CourseOut> _courses = []; |
| 0 | 53 | | IEnumerable<CourseOut> SelectedCourses { get; set; } = new HashSet<CourseOut>(); |
| | 54 | |
|
| | 55 | | protected override async Task OnInitializedAsync() |
| | 56 | | { |
| 0 | 57 | | _courses = await GetCoursesClient.Get(); |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | private string GetMultiSelectionText(List<string> selected) |
| | 61 | | { |
| 0 | 62 | | if (selected.Count == 1) |
| 0 | 63 | | return selected.First(); |
| | 64 | |
|
| 0 | 65 | | if (selected.Count > 1) |
| 0 | 66 | | return $"{selected.Count} cursos selecionados"; |
| | 67 | |
|
| 0 | 68 | | return ""; |
| | 69 | | } |
| | 70 | |
|
| | 71 | | private async Task Submit() |
| | 72 | | { |
| 0 | 73 | | if (_loading) return; |
| | 74 | |
|
| 0 | 75 | | await _form.Validate(); |
| 0 | 76 | | if (!_form.IsValid) return; |
| | 77 | |
|
| 0 | 78 | | _loading = true; |
| 0 | 79 | | var response = await CreateDisciplineClient.Create(_name, SelectedCourses.ToList().ConvertAll(c => c.Id)); |
| 0 | 80 | | if (response.IsSuccessStatusCode) |
| | 81 | | { |
| 0 | 82 | | MudDialog.Close(DialogResult.Ok(true)); |
| 0 | 83 | | Snackbar.Add("Disciplina cadastrada com sucesso!", Severity.Success); |
| | 84 | | } |
| | 85 | | else |
| | 86 | | { |
| 0 | 87 | | var error = await response.ToError(); |
| 0 | 88 | | Snackbar.Add(error.Message, Severity.Error); |
| | 89 | | } |
| 0 | 90 | | _loading = false; |
| 0 | 91 | | } |
| | 92 | |
|
| 0 | 93 | | void Cancel() => MudDialog.Cancel(); |
| | 94 | | } |