Skip to content

Commit

Permalink
closes #111 (and accidentaly #201 aswell :-D) - Fix up "filtering and…
Browse files Browse the repository at this point in the history
… sorting" remembering on subject list
  • Loading branch information
Xopabyteh committed May 23, 2024
1 parent 411af0b commit 4dc7328
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
22 changes: 11 additions & 11 deletions Web.Client/Pages/Electives/SubjectList.razor
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,22 @@
<HxListLayout Title="Předměty"
TFilterModel="SubjectListQueryFilter"
@bind-FilterModel='subjectListFilter'
@bind-FilterModel:after='HandleFilterChanged'>
@bind-FilterModel:after='subjectsGrid.RefreshDataAsync'>
<CommandsTemplate>
<AuthorizeView Roles="@nameof(Role.Administrator)">
<HxButton Text="Nový předmět" OnClick="HandleNewItemClicked" Color="ThemeColor.Primary" />
</AuthorizeView>
</CommandsTemplate>
<FilterTemplate>
<HxInputText @bind-Value="@context.Name" Label="Název" />
<GradePicker @bind-Value='@context.GradeId' Label='Ročník'/>
<SubjectCategoryPicker @bind-Value="context.SubjectCategoryId" Label="Skupina" />
<EducationalAreaPicker @bind-Value="@context.EducationalAreaId" Label="Vzdělávací oblast" />
<TeacherPicker @bind-Value="context.TeacherId" Label="Vyučující" />
</FilterTemplate>
<SearchTemplate>
<strong class="d-none d-sm-block">Ročník:</strong>
<GradePicker @bind-Value='subjectListFilter.GradeId'
@bind-Value:after='HandleFilterChanged'
@bind-Value:after='subjectsGrid.RefreshDataAsync'
ExcludedGrades='[GradeEntry.Prima]'/> @*Don't show prima, it would create confusion like:
Why is there no subject for prima?'*@
</SearchTemplate>
Expand All @@ -53,19 +52,21 @@
ItemRowCssClassSelector="item => GetRowCssClass(item)"
HeaderRowCssClass="text-nowrap"
Context="item"
@bind-CurrentUserState='gridUserState'
@bind-CurrentUserState:after='subjectsGrid.RefreshDataAsync'
Responsive="true">
<Columns>
<HxGridColumn TItem="SubjectListItemDto" HeaderText="Název" ItemTextSelector="@(item => item.Name)" SortString="@nameof(SubjectListItemDto.Name)" IsDefaultSortColumn="true" />
<HxGridColumn TItem="SubjectListItemDto" HeaderText="Vyučující" ItemTextSelector="@(item => GetTeachers(item.TeacherIds))" SortString="@nameof(SubjectListItemDto.TeacherIds)" />
<HxGridColumn TItem="SubjectListItemDto" HeaderText="Rozvrh" ItemTextSelector="@(item => ValueFormatter.GetScheduleTimeSlot(item.ScheduleDayOfWeek, item.ScheduleSlotInDay))" SortString="@nameof(SubjectListItemDto.ScheduleSlotInDay)" ItemCssClass="text-nowrap" />
<HxGridColumn TItem="SubjectListItemDto" HeaderText="Kapacita" SortString="@nameof(SubjectListItemDto.Capacity)" >
<HxGridColumn Id='0' TItem="SubjectListItemDto" HeaderText="Název" ItemTextSelector="@(item => item.Name)" SortString="@nameof(SubjectListItemDto.Name)" IsDefaultSortColumn="true" />
<HxGridColumn Id='1' TItem="SubjectListItemDto" HeaderText="Vyučující" ItemTextSelector="@(item => GetTeachers(item.TeacherIds))" SortString="@nameof(SubjectListItemDto.TeacherIds)" />
<HxGridColumn Id='2' TItem="SubjectListItemDto" HeaderText="Rozvrh" ItemTextSelector="@(item => ValueFormatter.GetScheduleTimeSlot(item.ScheduleDayOfWeek, item.ScheduleSlotInDay))" SortString="@nameof(SubjectListItemDto.ScheduleSlotInDay)" ItemCssClass="text-nowrap" />
<HxGridColumn Id='3' TItem="SubjectListItemDto" HeaderText="Kapacita" SortString="@nameof(SubjectListItemDto.Capacity)" >
<ItemTemplate Context="item">
<CapacityDisplay Registrations="@item.StudentRegistrationsCountMain" Capacity="@item.Capacity" />
</ItemTemplate>
</HxGridColumn>
<HxGridColumn TItem="SubjectListItemDto" HeaderText="Ročník(y)" ItemTextSelector="@(item => GetGrades(item.GradeIds))" SortString="@nameof(SubjectListItemDto.GradeIds)" />
<HxGridColumn TItem="SubjectListItemDto" HeaderText="Skupina" ItemTextSelector="@(item => SubjectCategoriesDataStore.GetByKey(item.CategoryId.Value)?.Name)" SortString="@nameof(SubjectListItemDto.CategoryId)" />
<HxGridColumn TItem="SubjectListItemDto" HeaderText="Vzdělávací oblast(i)" ItemTextSelector="@(item => GetEducationalAreas(item.EducationalAreaIds))" SortString="@nameof(SubjectListItemDto.EducationalAreaIds)" />
<HxGridColumn Id='4' TItem="SubjectListItemDto" HeaderText="Ročník(y)" ItemTextSelector="@(item => GetGrades(item.GradeIds))" SortString="@nameof(SubjectListItemDto.GradeIds)" />
<HxGridColumn Id='5' TItem="SubjectListItemDto" HeaderText="Skupina" ItemTextSelector="@(item => SubjectCategoriesDataStore.GetByKey(item.CategoryId.Value)?.Name)" SortString="@nameof(SubjectListItemDto.CategoryId)" />
<HxGridColumn Id='6' TItem="SubjectListItemDto" HeaderText="Vzdělávací oblast(i)" ItemTextSelector="@(item => GetEducationalAreas(item.EducationalAreaIds))" SortString="@nameof(SubjectListItemDto.EducationalAreaIds)" />
<HxContextMenuGridColumn Context="item">
<AuthorizeView Roles="@nameof(Role.Administrator)">
<HxContextMenu>
Expand All @@ -77,7 +78,6 @@
</HxContextMenuGridColumn>
</Columns>
</HxGrid>

</DataTemplate>
</HxListLayout>

Expand Down
69 changes: 50 additions & 19 deletions Web.Client/Pages/Electives/SubjectList.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,25 @@ public partial class SubjectList
private SubjectListQueryFilter subjectListFilter = new();
private static SubjectListQueryFilter s_rememberedSubjectListFilter; // Null by default

private GridUserState gridUserState = new(); // Sorting
private static GridUserState s_rememberedGridUserState; // Null by default

private List<StudentSubjectRegistrationDto> registeredSubjects = new(); // Never null, may be empty...
private bool showRocnikovkaWarning = false;
private bool showExtensionSeminarWarning = false;

protected override void OnInitialized()
{
// Load remembered state (if we have some)
if (s_rememberedSubjectListFilter is not null)
{
subjectListFilter = s_rememberedSubjectListFilter;
}

if (s_rememberedGridUserState is not null)
{
gridUserState = s_rememberedGridUserState;
}
}

protected override async Task OnInitializedAsync()
Expand All @@ -46,26 +55,21 @@ protected override async Task OnInitializedAsync()
await TeachersDataStore.EnsureDataAsync();
await GradesDataStore.EnsureDataAsync();

if ((await ClientAuthService.GetCurrentClaimsPrincipalAsync()).IsInRole(nameof(Role.Student)))
// Student specific setup
if ((await ClientAuthService.GetCurrentClaimsPrincipalAsync())
.IsInRole(nameof(Role.Student)))
{
var gradeEntry = await ClientAuthService.GetCurrentStudentGradeIdAsync();
// Get grade (shouldn't be null, user is student)
var gradeId = await ClientAuthService.GetCurrentStudentGradeIdAsync();
Debug.Assert(gradeId != null, nameof(gradeId) + " != null");
Debug.Assert(gradeEntry != null, nameof(gradeEntry) + " != null");

var nextGradeId = gradeId.Value.NextGrade(); // Should have a value (octava isn't allowed on this page)
var nextGradeEntry = gradeEntry.Value.NextGrade(); // Should have a value (octava isn't allowed on this page)

// Determine "rocnikovka warning"
showRocnikovkaWarning = (nextGradeId is GradeEntry.Sexta or GradeEntry.Septima);
showRocnikovkaWarning = (nextGradeEntry is GradeEntry.Sexta or GradeEntry.Septima);

// Determine "extension seminar warning"
showExtensionSeminarWarning = (nextGradeId is GradeEntry.Kvinta or GradeEntry.Sexta);

// Set grade filter to next grade
if (s_rememberedSubjectListFilter is null)
{
subjectListFilter.GradeId = (int?)nextGradeId;
s_rememberedSubjectListFilter = subjectListFilter;
}
showExtensionSeminarWarning = (nextGradeEntry is GradeEntry.Kvinta or GradeEntry.Sexta);

// Get registered subjects
await StudentSubjectRegistrationsDataStore.EnsureDataAsync();
Expand Down Expand Up @@ -100,6 +104,12 @@ protected string GetRowCssClass(SubjectListItemDto item)

private async Task<GridDataProviderResult<SubjectListItemDto>> LoadSubjectsAsync(GridDataProviderRequest<SubjectListItemDto> request)
{
await TrySetupFilterForFirstLoad();

// Remember filter and sorting for next time coming to this page
s_rememberedSubjectListFilter = subjectListFilter;
s_rememberedGridUserState = gridUserState;

var sorting = request.Sorting?.Select(s => new SortItem(s.SortString, s.SortDirection)).ToArray();

var subjectListRequest = new DataFragmentRequest<SubjectListQueryFilter>()
Expand All @@ -119,6 +129,33 @@ private async Task<GridDataProviderResult<SubjectListItemDto>> LoadSubjectsAsync
};
}

/// <summary>
/// Presets the filter for the current user for better UX
/// If student, show subjects for next grade
/// </summary>
/// <returns></returns>
private async Task TrySetupFilterForFirstLoad()
{
// Setup filter for the first time coming into here
if (s_rememberedSubjectListFilter is not null)
{
// -> Not here for the first time
return;
}

// for STUDENTS - Set grade filter to next grade
if ((await ClientAuthService.GetCurrentClaimsPrincipalAsync())
.IsInRole(nameof(Role.Student)))
{
// User is a student
var gradeEntry = await ClientAuthService.GetCurrentStudentGradeIdAsync();
var nextGradeEntry = gradeEntry.Value.NextGrade(); // Should have a value (octava isn't allowed on this page)
subjectListFilter.GradeId = (int?)nextGradeEntry;
}

// maybe todo? : show teachers only their subjects?
}

private Task HandleSelectedDataItemChanged(SubjectListItemDto selection)
{
subjectSelected = selection;
Expand Down Expand Up @@ -160,10 +197,4 @@ private string GetTeachers(List<int> teacherIds)
return String.Join(", ", teacherIds.Select(id => TeachersDataStore.GetByKeyOrDefault(id)?.Name))
.Trim(',', ' ');
}

private Task HandleFilterChanged()
{
s_rememberedSubjectListFilter = subjectListFilter;
return subjectsGrid.RefreshDataAsync();
}
}

0 comments on commit 4dc7328

Please sign in to comment.