| | 1 | | @namespace Syki.Front.Features.Academic.AssignDisciplinesToTeacher |
| | 2 | |
|
| | 3 | | <MudDrawer @bind-Open="@_open" Width="@_width" Anchor="Anchor.Right" Elevation="1" Variant="@DrawerVariant.Temporary"> |
| | 4 | | <MudDrawerHeader Class="justify-space-between"> |
| | 5 | | <MudStack Row Justify="Justify.FlexStart" AlignItems="AlignItems.Center" Spacing="3"> |
| | 6 | | <MudIcon Icon="@Icons.Material.Filled.Apps" /> |
| | 7 | | <MudText Typo="Typo.h5"><b>Disciplinas do professor</b></MudText> |
| | 8 | | </MudStack> |
| | 9 | | <MudIconButton Icon="@Icons.Material.Filled.Close" OnClick="@Close" /> |
| | 10 | | </MudDrawerHeader> |
| | 11 | |
|
| | 12 | | <MudGrid Spacing="1" Class="px-6"> |
| 0 | 13 | | <MudForm @ref="@_form" Style="width: 100%"> |
| 0 | 14 | | <MudGrid Spacing="2"> |
| 0 | 15 | | <MudItem xs="12" Class="my-2"> |
| 0 | 16 | | <MudStack Row AlignItems="AlignItems.Center" Justify="Justify.FlexStart" Spacing="1"> |
| 0 | 17 | | <MudIcon Icon="@Icons.Material.Filled.Person4" Size="Size.Medium" /> |
| 0 | 18 | | <MudText Typo="Typo.h6"><b>@_teacher.Name</b></MudText> |
| | 19 | | </MudStack> |
| | 20 | | </MudItem> |
| | 21 | |
|
| | 22 | | <MudItem xs="12"> |
| | 23 | | <SykiTitleText Icon="@GetSelectedDisciplinesIcon()" Text="@GetSelectedDisciplinesTitle()" /> |
| | 24 | | </MudItem> |
| | 25 | |
|
| | 26 | | <MudItem xs="12"> |
| | 27 | | <MudSelect |
| | 28 | | Dense |
| | 29 | | Margin="Margin.Dense" |
| | 30 | | Variant="Variant.Outlined" |
| | 31 | | Value="@_course" |
| 0 | 32 | | ValueChanged="@((CourseOut newValue) => HandleSelectCourse(newValue))" |
| | 33 | | Label="Curso" |
| | 34 | | AdornmentColor="Color.Primary" |
| | 35 | | > |
| 0 | 36 | | @foreach (CourseOut? item in _courses) |
| | 37 | | { |
| 0 | 38 | | <MudSelectItem Value="@item">@item.Name</MudSelectItem> |
| | 39 | | } |
| | 40 | | </MudSelect> |
| | 41 | | </MudItem> |
| | 42 | | <MudItem xs="12"> |
| | 43 | | <MudAutocomplete |
| | 44 | | MaxItems="50" |
| | 45 | | T="AssignDisciplinesToTeacherSelect" |
| | 46 | | SearchFunc="@SearchDiscipline" |
| | 47 | | Dense |
| | 48 | | Placeholder="" |
| 0 | 49 | | ToStringFunc="@(e => null)" |
| | 50 | | DebounceInterval="100" |
| | 51 | | ResetValueOnEmptyText |
| | 52 | | Margin="Margin.Dense" |
| | 53 | | Variant="Variant.Outlined" |
| | 54 | | Class="mb-2" |
| | 55 | | Value="@_discipline" |
| 0 | 56 | | ValueChanged="(AssignDisciplinesToTeacherSelect newValue) => HandleDisciplineChanged(newValue)" |
| | 57 | | Label="Disciplina" |
| | 58 | | AdornmentColor="Color.Primary" |
| | 59 | | > |
| | 60 | | <ItemTemplate Context="e"> |
| | 61 | | <MudStack Row Spacing="0"> |
| 0 | 62 | | <MudText Typo="Typo.body1">@e.Name</MudText> |
| | 63 | | </MudStack> |
| | 64 | | </ItemTemplate> |
| | 65 | | <NoItemsTemplate> |
| | 66 | | <MudStack Row Spacing="0" AlignItems="AlignItems.Center" Justify="Justify.Center"> |
| 0 | 67 | | <MudText Typo="Typo.body1">@GetEmptyDisciplinesText()</MudText> |
| | 68 | | </MudStack> |
| | 69 | | </NoItemsTemplate> |
| | 70 | | </MudAutocomplete> |
| | 71 | | </MudItem> |
| | 72 | | </MudGrid> |
| | 73 | |
|
| | 74 | | <MudItem xs="12" Class="mt-2"> |
| 0 | 75 | | @if (_selectedDisciplines.Count > 0) |
| | 76 | | { |
| | 77 | | <MudDataGrid |
| | 78 | | Dense |
| | 79 | | Hover |
| | 80 | | Class="my-2" |
| | 81 | | SortMode="SortMode.None" |
| 0 | 82 | | Items="@_selectedDisciplines.OrderBy(d => d.Name)" |
| | 83 | | > |
| | 84 | | <Columns> |
| | 85 | | <PropertyColumn Property="x => x.Name" Title="Disciplina" /> |
| | 86 | | <TemplateColumn CellClass="d-flex justify-end"> |
| | 87 | | <CellTemplate> |
| 0 | 88 | | <MudIconButton Size="@Size.Small" Icon="@Icons.Material.Outlined.Delete" Color="@Col |
| | 89 | | </CellTemplate> |
| | 90 | | </TemplateColumn> |
| | 91 | | </Columns> |
| | 92 | | </MudDataGrid> |
| | 93 | | } |
| | 94 | | </MudItem> |
| | 95 | | </MudForm> |
| | 96 | | </MudGrid> |
| | 97 | |
|
| | 98 | | <MudStack Row Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center" Spacing="3" Class="px-2 pt-6"> |
| | 99 | | <DialogCancelButton OnClick="@Close" /> |
| 0 | 100 | | <DialogSaveButton OnClick="@Submit" Disabled="@_selectedDisciplines.Select(x => x.Id).ToList().IsEquivalentTo(_t |
| | 101 | | </MudStack> |
| | 102 | | </MudDrawer> |
| | 103 | |
|
| | 104 | | @inject ISnackbar Snackbar |
| | 105 | | @inject NavigationManager Nav |
| | 106 | | @inject IBrowserViewportService BrowserViewportService |
| | 107 | | @inject GetAcademicTeacherClient GetAcademicTeacherClient |
| | 108 | | @inject GetCourseDisciplinesClient GetCourseDisciplinesClient |
| | 109 | | @inject GetCoursesWithDisciplinesClient GetCoursesWithDisciplinesClient |
| | 110 | | @inject AssignDisciplinesToTeacherClient AssignDisciplinesToTeacherClient |
| | 111 | |
|
| | 112 | | @code |
| | 113 | | { |
| | 114 | | [Parameter] |
| 0 | 115 | | public EventCallback AfterSubmit { get; set; } |
| | 116 | |
|
| | 117 | | private bool _open; |
| 0 | 118 | | private string _width = "520px"; |
| | 119 | | private Breakpoint _breakpoint; |
| | 120 | |
|
| | 121 | | private MudForm _form; |
| | 122 | | private bool _loading; |
| | 123 | |
|
| 0 | 124 | | private GetAcademicTeacherOut _teacher = new(); |
| | 125 | |
|
| | 126 | | private CourseOut? _course; |
| 0 | 127 | | private List<CourseOut> _courses = []; |
| | 128 | |
|
| | 129 | | private AssignDisciplinesToTeacherSelect? _discipline; |
| 0 | 130 | | private List<CourseDisciplineOut> _disciplines = []; |
| 0 | 131 | | private HashSet<AssignDisciplinesToTeacherSelect> _selectedDisciplines = []; |
| | 132 | |
|
| | 133 | | protected override async Task OnInitializedAsync() |
| | 134 | | { |
| 0 | 135 | | _breakpoint = await BrowserViewportService.GetCurrentBreakpointAsync(); |
| 0 | 136 | | _width = _breakpoint == Breakpoint.Xs ? "100%" : "500px"; |
| 0 | 137 | | _courses = await GetCoursesWithDisciplinesClient.Get(); |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | async Task HandleSelectCourse(CourseOut newValue) |
| | 141 | | { |
| 0 | 142 | | _course = newValue; |
| 0 | 143 | | _disciplines = _course != null ? await GetCourseDisciplinesClient.Get(_course!.Id) : []; |
| 0 | 144 | | _disciplines.ForEach(d => d.IsSelected = _selectedDisciplines.Any(td => td.Id == d.Id)); |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | private async Task<IEnumerable<AssignDisciplinesToTeacherSelect>> SearchDiscipline(string value, CancellationToken c |
| | 148 | | { |
| 0 | 149 | | await Task.Yield(); |
| | 150 | |
|
| 0 | 151 | | var filtered = _disciplines.Where(d => !d.IsSelected) |
| 0 | 152 | | .ToList().ConvertAll(x => new AssignDisciplinesToTeacherSelect { Id = x.Id, Name = x.Name }); |
| | 153 | |
|
| 0 | 154 | | if (string.IsNullOrEmpty(value)) |
| 0 | 155 | | return filtered; |
| | 156 | |
|
| 0 | 157 | | return filtered.Where(x => x.Name.Contains(value, StringComparison.InvariantCultureIgnoreCase)); |
| 0 | 158 | | } |
| | 159 | |
|
| | 160 | | private void HandleDisciplineChanged(AssignDisciplinesToTeacherSelect newValue) |
| | 161 | | { |
| 0 | 162 | | _disciplines.First(d => d.Id == newValue.Id).IsSelected = true; |
| 0 | 163 | | _selectedDisciplines.Add(newValue); |
| 0 | 164 | | } |
| | 165 | |
|
| | 166 | | private void UnselectDiscipline(AssignDisciplinesToTeacherSelect discipline) |
| | 167 | | { |
| 0 | 168 | | _disciplines.FirstOrDefault(d => d.Id == discipline.Id)?.IsSelected = false; |
| 0 | 169 | | _selectedDisciplines.Remove(discipline); |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | public async Task Open(Guid teacherId) |
| | 173 | | { |
| 0 | 174 | | _loading = true; |
| | 175 | |
|
| 0 | 176 | | var response = await GetAcademicTeacherClient.Get(teacherId); |
| 0 | 177 | | if (response.IsSuccess) |
| | 178 | | { |
| 0 | 179 | | _teacher = response.Success; |
| 0 | 180 | | _selectedDisciplines = _teacher.Disciplines |
| 0 | 181 | | .Select(x => new AssignDisciplinesToTeacherSelect() { Id = x.Id, Name = x.Name }) |
| 0 | 182 | | .ToHashSet(); |
| | 183 | | } |
| | 184 | | else |
| | 185 | | { |
| 0 | 186 | | Snackbar.Add(response.Error.Message, Severity.Error); |
| | 187 | | } |
| | 188 | |
|
| 0 | 189 | | _course = null; |
| 0 | 190 | | _discipline = null; |
| 0 | 191 | | _disciplines = []; |
| | 192 | |
|
| 0 | 193 | | _open = true; |
| 0 | 194 | | await _form?.ResetAsync(); |
| | 195 | |
|
| 0 | 196 | | _loading = false; |
| | 197 | |
|
| 0 | 198 | | StateHasChanged(); |
| 0 | 199 | | } |
| | 200 | |
|
| | 201 | | private void Close() |
| | 202 | | { |
| 0 | 203 | | _open = false; |
| 0 | 204 | | } |
| | 205 | |
|
| | 206 | | private async Task Submit() |
| | 207 | | { |
| 0 | 208 | | if (_loading) return; |
| | 209 | |
|
| 0 | 210 | | await _form.Validate(); |
| 0 | 211 | | if (!_form.IsValid) return; |
| | 212 | |
|
| 0 | 213 | | _loading = true; |
| 0 | 214 | | var selectedDisciplines = _selectedDisciplines.Select(x => x.Id).ToList(); |
| 0 | 215 | | var response = await AssignDisciplinesToTeacherClient.Assign(_teacher.Id, selectedDisciplines); |
| 0 | 216 | | if (response.IsSuccess) |
| | 217 | | { |
| 0 | 218 | | Snackbar.Add("Disciplinas salvas com sucesso!", Severity.Success); |
| 0 | 219 | | await AfterSubmit.InvokeAsync(); |
| 0 | 220 | | _open = false; |
| | 221 | | } |
| | 222 | | else |
| | 223 | | { |
| 0 | 224 | | Snackbar.Add(response.Error.Message, Severity.Error); |
| | 225 | | } |
| 0 | 226 | | _loading = false; |
| 0 | 227 | | } |
| | 228 | |
|
| | 229 | | private string GetSelectedDisciplinesIcon() |
| | 230 | | { |
| 0 | 231 | | var count = _selectedDisciplines.Count; |
| 0 | 232 | | if (count == 0) return Icons.Material.Filled.Error; |
| | 233 | |
|
| 0 | 234 | | return Icons.Material.Filled.Check; |
| | 235 | | } |
| | 236 | |
|
| | 237 | | private string GetSelectedDisciplinesTitle() |
| | 238 | | { |
| 0 | 239 | | var count = _selectedDisciplines.Count; |
| 0 | 240 | | if (count == 0) return "Nenhuma disciplina selecionada"; |
| | 241 | |
|
| 0 | 242 | | if (count == 1) return $"1 disciplina selecionada"; |
| | 243 | |
|
| 0 | 244 | | return $"{count} disciplinas selecionadas"; |
| | 245 | | } |
| | 246 | |
|
| | 247 | | private string GetEmptyDisciplinesText() |
| | 248 | | { |
| 0 | 249 | | if (_course == null) return "Selecione um curso"; |
| | 250 | |
|
| 0 | 251 | | return "Selecione outro curso"; |
| | 252 | | } |
| | 253 | | } |