@@ -5,14 +5,19 @@ import {
5
5
mutation ,
6
6
} from 'vuex-class-component'
7
7
import { AppStore } from '@/store/modules/app'
8
+ import { UserStore } from '@/store/modules/user'
8
9
import classData from '@/types/store/classData'
9
- import { Auth , API , graphqlOperation } from 'aws-amplify'
10
- import { GraphQLResult } from '@aws-amplify/api'
11
- import { getClass } from '@/graphql/queries'
10
+ import { API , Auth , graphqlOperation } from 'aws-amplify'
11
+ import { GRAPHQL_AUTH_MODE , GraphQLResult } from '@aws-amplify/api'
12
+ import { getClass , listLessons } from '@/graphql/queries'
12
13
import { createClass , createLesson , updateLesson } from '@/graphql/mutations'
13
- import { GetClassQuery } from '@/API'
14
+ import { GetClassQuery , ListLessonsQuery } from '@/API'
14
15
import { vxm } from '@/store'
15
16
17
+ type LessonsGroupedBy = {
18
+ [ key : string ] : classData . LessonWithId [ ]
19
+ }
20
+
16
21
const VuexModule = createModule ( {
17
22
namespaced : 'classData' ,
18
23
strict : false ,
@@ -36,11 +41,11 @@ const generateUniqueId = (): string => {
36
41
export class ClassDataStore extends VuexModule implements classData . ClassData {
37
42
classId : classData . ClassId = ''
38
43
className : string = ''
39
- lessons : classData . LessonWithId [ ] = [ ]
40
-
41
- public get lessonsOnCurrentDate ( ) : classData . LessonWithId [ ] {
42
- const appStore = createProxy ( this . $store , AppStore )
44
+ lessonsGroupByPeriod : LessonsGroupedBy = { }
43
45
46
+ @action
47
+ public async lessonsOnCurrentDate ( date : Date ) {
48
+ const userStore = createProxy ( this . $store , UserStore )
44
49
// Generate a new Date object with a specified date & time
45
50
const d = ( date : Date , hours : number , minutes : number , seconds : number ) => {
46
51
const newDate = new Date ( date )
@@ -49,13 +54,38 @@ export class ClassDataStore extends VuexModule implements classData.ClassData {
49
54
newDate . setSeconds ( seconds )
50
55
return newDate
51
56
}
52
- const start = d ( appStore . currentDate , 0 , 0 , 0 ) . getTime ( )
53
- const end = d ( appStore . currentDate , 23 , 59 , 59 ) . getTime ( )
54
-
55
- return this . lessons . filter ( ( lesson ) => {
56
- const startOfLesson = new Date ( lesson . startTime ) . getTime ( )
57
- return start <= startOfLesson && end >= startOfLesson
58
- } )
57
+ const start = d ( date , 0 , 0 , 0 )
58
+ const end = d ( date , 23 , 59 , 59 )
59
+
60
+ const lessons = ( await API . graphql ( {
61
+ query : listLessons ,
62
+ variables : {
63
+ filter : {
64
+ and : [
65
+ {
66
+ classId : {
67
+ eq : this . classId ,
68
+ } ,
69
+ } ,
70
+ {
71
+ startTime : {
72
+ ge : start ,
73
+ } ,
74
+ } ,
75
+ {
76
+ startTime : {
77
+ le : end ,
78
+ } ,
79
+ } ,
80
+ ] ,
81
+ } ,
82
+ } ,
83
+ authMode : userStore . isLoginWithAPIKEY
84
+ ? GRAPHQL_AUTH_MODE . API_KEY
85
+ : GRAPHQL_AUTH_MODE . AMAZON_COGNITO_USER_POOLS ,
86
+ } ) ) as GraphQLResult < ListLessonsQuery >
87
+
88
+ return lessons . data ?. listLessons ?. items as any [ ]
59
89
}
60
90
61
91
public get isLoaded ( ) : boolean {
@@ -74,13 +104,9 @@ export class ClassDataStore extends VuexModule implements classData.ClassData {
74
104
}
75
105
76
106
const className = classObject . className
77
-
78
- const classLessonsItems = result . data ?. getClass ?. lessons ?. items as any [ ]
79
-
80
107
this . setClassData ( {
81
108
classId,
82
109
className,
83
- lessons : classLessonsItems ,
84
110
} )
85
111
}
86
112
@@ -129,7 +155,6 @@ export class ClassDataStore extends VuexModule implements classData.ClassData {
129
155
this . setClassData ( {
130
156
classId,
131
157
className,
132
- lessons : [ ] ,
133
158
} )
134
159
}
135
160
@@ -149,7 +174,7 @@ export class ClassDataStore extends VuexModule implements classData.ClassData {
149
174
} catch {
150
175
throw new Error ( 'エラーによって処理に失敗しました' )
151
176
}
152
- await this . loadClassData ( this . classId )
177
+ await this . getLessonsByCurrentDate ( )
153
178
}
154
179
155
180
@action
@@ -172,22 +197,46 @@ export class ClassDataStore extends VuexModule implements classData.ClassData {
172
197
} catch {
173
198
throw new Error ( 'エラーによって処理に失敗しました' )
174
199
}
175
- await this . loadClassData ( this . classId )
200
+ await this . getLessonsByCurrentDate ( )
176
201
}
177
202
178
203
@mutation
179
- public setClassData ( { classId, className, lessons } : classData . ClassData ) {
204
+ public setClassData ( { classId, className } : classData . ClassData ) {
180
205
this . classId = classId
181
206
this . className = className
182
- this . lessons = lessons
207
+ }
208
+
209
+ @mutation
210
+ public setLessonsGroupByPeriod ( lessons : classData . LessonWithId [ ] ) {
211
+ const groupBy = (
212
+ targets : classData . LessonWithId [ ] ,
213
+ key : keyof classData . LessonWithId
214
+ ) =>
215
+ targets . reduce (
216
+ ( acc : LessonsGroupedBy , currentLesson : classData . LessonWithId ) => {
217
+ const valueToGroup = currentLesson [ key ] . toString ( )
218
+ acc [ valueToGroup ] = acc [ valueToGroup ] || [ ]
219
+ acc [ valueToGroup ] . push ( currentLesson )
220
+ return acc
221
+ } ,
222
+ { }
223
+ )
224
+ this . lessonsGroupByPeriod = groupBy ( lessons , 'startTime' )
225
+ }
226
+
227
+ @action
228
+ public async getLessonsByCurrentDate ( ) {
229
+ const appStore = createProxy ( this . $store , AppStore )
230
+ const lessons = await this . lessonsOnCurrentDate ( appStore . currentDate )
231
+ await this . setLessonsGroupByPeriod ( lessons )
183
232
}
184
233
185
234
@action
186
235
public async unloadClassData ( ) {
187
236
this . setClassData ( {
188
237
classId : '' ,
189
238
className : '' ,
190
- lessons : [ ] ,
191
239
} )
240
+ this . lessonsGroupByPeriod = { }
192
241
}
193
242
}
0 commit comments