Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sorting in admin view #1217

Merged
merged 2 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 42 additions & 6 deletions frontend/src/routes/AdminPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ import {
GroupLogoWrapper,
} from "./styles";
import { Application } from "src/types";
import {
AlphabeticalComparatorAsc,
AlphabeticalComparatorDesc,
ApplicationComparator,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this should be imported as a type but oh well

CreatedAtComparatorAsc,
CreatedAtComparatorDesc,
UpdatedAtComparatorAsc,
UpdatedAtComparatorDesc,
} from "src/utils/sortAdmissions";

export interface CsvData {
name: string;
Expand Down Expand Up @@ -57,16 +66,31 @@ const AdminPage = () => {
error,
} = useApplications(admissionSlug ?? "");

const [applicationComparator, setApplicationComparator] =
useState<ApplicationComparator>(UpdatedAtComparatorDesc);

const stringToComparatorMapper = new Map<string, ApplicationComparator>([
["alphabeticaldesc", AlphabeticalComparatorDesc],
["alphabeticalasc", AlphabeticalComparatorAsc],
["createddesc", CreatedAtComparatorDesc],
["createdasc", CreatedAtComparatorAsc],
["updateddesc", UpdatedAtComparatorDesc],
["updatedasc", UpdatedAtComparatorAsc],
]);

const handleSortChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
const selectedValue = event.target.value;
const selectedComparator =
stringToComparatorMapper.get(selectedValue) ?? AlphabeticalComparatorDesc;
setApplicationComparator(selectedComparator);
};

useEffect(() => {
if (!applications) return;
setSortedApplications(
[...applications].sort((a, b) => {
if (a.user.full_name < b.user.full_name) return -1;
if (a.user.full_name > b.user.full_name) return 1;
return 0;
})
[...applications].sort(applicationComparator.compare)
);
}, [applications]);
}, [applications, applicationComparator]);

useEffect(() => {
// Push all the individual applications into csvData with the right format
Expand Down Expand Up @@ -126,6 +150,18 @@ const AdminPage = () => {
<StatisticsName>Antall søkere</StatisticsName>
{numApplicants} {numApplicants == 1 ? "søker" : "søkere"}
</StatisticsWrapper>
<StatisticsWrapper>
<StatisticsName style={{ marginBottom: "1.25em" }}>
Sorter etter
</StatisticsName>
<select onChange={handleSortChange}>
{Array.from(stringToComparatorMapper.keys()).map((key) => (
<option key={key} value={key}>
{stringToComparatorMapper.get(key)?.description}
</option>
))}
</select>{" "}
</StatisticsWrapper>
</Statistics>
<CSVExport
data={csvData}
Expand Down
66 changes: 66 additions & 0 deletions frontend/src/utils/sortAdmissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Application } from "src/types";

interface ApplicationComparator {
compare(app1: Application, app2: Application): number;
description: string;
}

const CreatedAtComparatorDesc: ApplicationComparator = {
compare: (app1, app2) => {
const date1 = new Date(app1.created_at);
const date2 = new Date(app2.created_at);
return date1.getTime() - date2.getTime();
},
description: "Dato opprettet synkende",
};

const CreatedAtComparatorAsc: ApplicationComparator = {
compare: (app1, app2) => {
const date1 = new Date(app1.created_at);
const date2 = new Date(app2.created_at);
return date2.getTime() - date1.getTime();
},
description: "Dato opprettet stigende",
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also kind of think I would prefer to have the field sorted on and the direction of the sort as separte options. Like having one seletor for what to sort by, and one for the direction of the sort. Just makes more sense to me. But it's not important, it can be changed later on if we want it:)

const UpdatedAtComparatorAsc: ApplicationComparator = {
compare: (app1, app2) => {
const date1 = new Date(app1.updated_at);
const date2 = new Date(app2.updated_at);
return date1.getTime() - date2.getTime();
},
description: "Dato oppdatert stigende",
};

const UpdatedAtComparatorDesc: ApplicationComparator = {
compare: (app1, app2) => {
const date1 = new Date(app1.updated_at);
const date2 = new Date(app2.updated_at);
return date2.getTime() - date1.getTime();
},
description: "Dato oppdatert synkende",
};

const AlphabeticalComparatorAsc: ApplicationComparator = {
compare: (app1, app2) => {
return app2.user.full_name.localeCompare(app1.user.full_name);
},
description: "Alfabetisk stigende",
};

const AlphabeticalComparatorDesc: ApplicationComparator = {
compare: (app1, app2) => {
return app1.user.full_name.localeCompare(app2.user.full_name);
},
description: "Alfabetisk synkende",
};

export type { ApplicationComparator };
export {
CreatedAtComparatorDesc,
CreatedAtComparatorAsc,
UpdatedAtComparatorAsc,
UpdatedAtComparatorDesc,
AlphabeticalComparatorAsc,
AlphabeticalComparatorDesc,
};