-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclub.ts
99 lines (88 loc) · 2.91 KB
/
club.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { defineStore } from 'pinia';
import { Club, ClubDetail } from '~~/models/club';
import { useClubService } from '~~/composables/useClubService';
const clubService = useClubService();
import { useToastService } from '~~/composables/useToastService';
import { useRegattaStore } from './regatta';
const { showError } = useToastService();
interface ClubState {
ids: string[];
entities: { [id: string]: Club };
detailIds: string[];
detailEntities: { [id: string]: ClubDetail };
selectedId: string | null;
}
export const useClubStore = defineStore('clubs', {
state: (): ClubState => ({
ids: [],
entities: {},
detailIds: [],
detailEntities: {},
selectedId: null
}),
getters: {
allClubs(state: ClubState) {
return state.ids.map((id: string) => state.entities[id]);
},
selectedClub(state: ClubState) {
return (
(state.selectedId && state.entities[state.selectedId]) || null
);
},
getClubById(state: ClubState) {
return (id: string) => {
return (id && state.entities[id]) || null;
};
},
selectedClubDetail(state: ClubState) {
return (
(state.selectedId && state.detailEntities[state.selectedId]) ||
null
);
}
},
actions: {
async loadClubs() {
const regattaId = useRegattaStore().selectedId;
if (regattaId == null) {
return;
}
try {
const loadedClubs = await clubService.loadClubs(regattaId);
const clubIds = loadedClubs.map((club) => club.id);
const clubEntities = loadedClubs.reduce(
(entities: { [id: string]: Club }, club: Club) => {
return { ...entities, [club.id]: club };
},
{}
);
this.ids = clubIds;
this.entities = clubEntities;
} catch (error) {
console.error(error);
useToastService().showError(
'Something went wrong loading the clubs'
);
}
},
async loadSelectedClub() {
const clubId = this.selectedId;
if (clubId == null) {
return;
}
try {
const club = await clubService.loadClubDetail(clubId);
this.detailIds = [...this.detailIds, club.id];
this.detailEntities = {
...this.detailEntities,
[club.id]: club
};
} catch (error) {
console.error(error);
useToastService().showError(
'Something went wrong loading the selected club'
);
}
}
}
});