-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregatta.ts
132 lines (119 loc) · 4.15 KB
/
regatta.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { defineStore } from 'pinia';
import { NewRegatta, Regatta, RegattaDetail } from '~~/models/regatta';
import { useRegattaService } from '~~/composables/useRegattaService';
const regattaService = useRegattaService();
import { useDateFormatter } from '~~/composables/useDateFormatter';
const { isBeforeOrAfter } = useDateFormatter();
interface RegattaState {
ids: string[];
entities: { [id: string]: Regatta };
detailIds: string[];
detailEntities: { [id: string]: RegattaDetail };
selectedId: string | null;
}
export const useRegattaStore = defineStore('regattas', {
state: (): RegattaState => ({
ids: [],
entities: {},
detailIds: [],
detailEntities: {},
selectedId: null
}),
getters: {
allRegattas(state: RegattaState) {
return state.ids
.map((id: string) => state.entities[id])
.sort((a: Regatta, b: Regatta) =>
isBeforeOrAfter(a.start_date, b.start_date)
);
},
selectedRegatta(state: RegattaState) {
return (
(state.selectedId && state.entities[state.selectedId]) || null
);
},
selectedRegattaDetail(state: RegattaState) {
return (
(state.selectedId && state.detailEntities[state.selectedId]) ||
null
);
}
},
actions: {
async loadRegattas() {
try {
const loadedRegattas = await regattaService.loadRegattas();
const regattaIds = loadedRegattas.map((regatta) => regatta.id);
const regattaEntities = loadedRegattas.reduce(
(entities: { [id: string]: Regatta }, regatta: Regatta) => {
return { ...entities, [regatta.id]: regatta };
},
{}
);
this.ids = regattaIds;
this.entities = regattaEntities;
} catch (error) {
console.error(error);
useToastService().showError(
'Something went wrong loading the regattas'
);
}
},
async loadSelectedRegatta() {
const regattaId = this.selectedId;
if (regattaId == null) {
return;
}
try {
const regatta = await regattaService.loadRegattaDetail(
regattaId
);
this.detailIds = [...this.detailIds, regatta.id];
this.detailEntities = {
...this.detailEntities,
[regatta.id]: regatta
};
} catch (error) {
console.error(error);
useToastService().showError(
'Something went wrong loading the selected regatta'
);
}
},
async add(newRegatta: NewRegatta) {
try {
const regatta = await regattaService.addRegatta(newRegatta);
this.ids = [...this.ids, regatta.id];
this.entities = {
...this.entities,
[regatta.id]: regatta
};
} catch (error) {
console.error(error);
useToastService().showError(
'Something went wrong adding the regatta'
);
}
},
delete(id: string) {
this.ids.splice(this.ids.indexOf(id), 1);
delete this.entities[id];
this.detailIds.splice(this.detailIds.indexOf(id), 1);
delete this.detailEntities[id];
},
async edit(id: string, data: NewRegatta) {
try {
const editedRegatta = await regattaService.editRegatta(
id,
data
);
this.entities[id] = editedRegatta;
} catch (error) {
console.error(error);
useToastService().showError(
'Something went wrong editing the regatta'
);
}
}
}
});