Skip to content

Commit 3d3554b

Browse files
committed
lessonsOnCurrentDateをgraphql経由で取得するように修正・lessonsの削除・lessonsGroupByPeriodを追加
1 parent 16f5373 commit 3d3554b

File tree

1 file changed

+74
-25
lines changed

1 file changed

+74
-25
lines changed

src/store/modules/classData.ts

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ import {
55
mutation,
66
} from 'vuex-class-component'
77
import { AppStore } from '@/store/modules/app'
8+
import { UserStore } from '@/store/modules/user'
89
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'
1213
import { createClass, createLesson, updateLesson } from '@/graphql/mutations'
13-
import { GetClassQuery } from '@/API'
14+
import { GetClassQuery, ListLessonsQuery } from '@/API'
1415
import { vxm } from '@/store'
1516

17+
type LessonsGroupedBy = {
18+
[key: string]: classData.LessonWithId[]
19+
}
20+
1621
const VuexModule = createModule({
1722
namespaced: 'classData',
1823
strict: false,
@@ -36,11 +41,11 @@ const generateUniqueId = (): string => {
3641
export class ClassDataStore extends VuexModule implements classData.ClassData {
3742
classId: classData.ClassId = ''
3843
className: string = ''
39-
lessons: classData.LessonWithId[] = []
40-
41-
public get lessonsOnCurrentDate(): classData.LessonWithId[] {
42-
const appStore = createProxy(this.$store, AppStore)
44+
lessonsGroupByPeriod: LessonsGroupedBy = {}
4345

46+
@action
47+
public async lessonsOnCurrentDate(date: Date) {
48+
const userStore = createProxy(this.$store, UserStore)
4449
// Generate a new Date object with a specified date & time
4550
const d = (date: Date, hours: number, minutes: number, seconds: number) => {
4651
const newDate = new Date(date)
@@ -49,13 +54,38 @@ export class ClassDataStore extends VuexModule implements classData.ClassData {
4954
newDate.setSeconds(seconds)
5055
return newDate
5156
}
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[]
5989
}
6090

6191
public get isLoaded(): boolean {
@@ -74,13 +104,9 @@ export class ClassDataStore extends VuexModule implements classData.ClassData {
74104
}
75105

76106
const className = classObject.className
77-
78-
const classLessonsItems = result.data?.getClass?.lessons?.items as any[]
79-
80107
this.setClassData({
81108
classId,
82109
className,
83-
lessons: classLessonsItems,
84110
})
85111
}
86112

@@ -129,7 +155,6 @@ export class ClassDataStore extends VuexModule implements classData.ClassData {
129155
this.setClassData({
130156
classId,
131157
className,
132-
lessons: [],
133158
})
134159
}
135160

@@ -149,7 +174,7 @@ export class ClassDataStore extends VuexModule implements classData.ClassData {
149174
} catch {
150175
throw new Error('エラーによって処理に失敗しました')
151176
}
152-
await this.loadClassData(this.classId)
177+
await this.getLessonsByCurrentDate()
153178
}
154179

155180
@action
@@ -172,22 +197,46 @@ export class ClassDataStore extends VuexModule implements classData.ClassData {
172197
} catch {
173198
throw new Error('エラーによって処理に失敗しました')
174199
}
175-
await this.loadClassData(this.classId)
200+
await this.getLessonsByCurrentDate()
176201
}
177202

178203
@mutation
179-
public setClassData({ classId, className, lessons }: classData.ClassData) {
204+
public setClassData({ classId, className }: classData.ClassData) {
180205
this.classId = classId
181206
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)
183232
}
184233

185234
@action
186235
public async unloadClassData() {
187236
this.setClassData({
188237
classId: '',
189238
className: '',
190-
lessons: [],
191239
})
240+
this.lessonsGroupByPeriod = {}
192241
}
193242
}

0 commit comments

Comments
 (0)