-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfields.svelte.ts
62 lines (58 loc) · 1.69 KB
/
fields.svelte.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import type { Competence } from "$lib/interfaces/Competence";
import type { Field } from "$lib/interfaces/Field";
import type { Lesson } from "$lib/interfaces/Lesson";
import { lessonComparator } from "$lib/resources/lessons.svelte";
import { queryClient } from "$lib/utils/queryClient";
import { createQuery } from "@tanstack/svelte-query";
import { SvelteMap } from "svelte/reactivity";
import { derived, fromStore } from "svelte/store";
export const fields = fromStore(
derived(
createQuery<Record<string, Field>>(
{
queryKey: ["v1.0", "field", { "override-group": true }],
},
queryClient,
),
({ data, isSuccess }) =>
isSuccess
? new SvelteMap<string, Field>(Object.entries(data))
: undefined,
undefined,
),
);
export function sortFields(
unsortedFields: SvelteMap<string, Field>,
unsortedLessons: SvelteMap<string, Lesson>,
unsortedCompetences: SvelteMap<string, Competence>,
): SvelteMap<string, Field> {
return new SvelteMap(
[...unsortedFields].sort((first, second) =>
fieldComparator(
first[1],
second[1],
unsortedLessons,
unsortedCompetences,
),
),
);
}
function fieldComparator(
first: Field,
second: Field,
lessons: SvelteMap<string, Lesson>,
competences: SvelteMap<string, Competence>,
): number {
const firstFirstLesson = lessons.get(first.lessons[0]);
const secondFirstLesson = lessons.get(second.lessons[0]);
if (firstFirstLesson === undefined) {
if (secondFirstLesson === undefined) {
return 0;
}
return 1;
}
if (secondFirstLesson === undefined) {
return -1;
}
return lessonComparator(firstFirstLesson, secondFirstLesson, competences);
}