Skip to content

Commit decde31

Browse files
authored
Merge pull request #144 from MaySoMusician/feature/lessons-vuex-store
新Lessons形式に対応する
2 parents 30c0e96 + f062fe6 commit decde31

File tree

2 files changed

+106
-72
lines changed

2 files changed

+106
-72
lines changed

src/store/modules/classData.ts

Lines changed: 44 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,20 @@ import {
66
} from 'vuex-class-component'
77
import firebase from '@/plugins/firebase'
88
import { AppStore } from '@/store/modules/app'
9-
import Timestamp = firebase.firestore.Timestamp
9+
import { classData } from '@/types/store/classData'
1010

1111
const VuexModule = createModule({
1212
namespaced: 'classData',
1313
strict: false,
1414
target: 'nuxt'
1515
})
1616

17-
type ClassId = string
18-
type ClassName = string
19-
type Lessons = Lesson[]
17+
export class ClassDataStore extends VuexModule implements classData.ClassData {
18+
classId: classData.ClassId = ''
19+
className: string = ''
20+
lessons: classData.Lesson[] = []
2021

21-
interface Lesson {
22-
subject: string
23-
content: string
24-
startTime: Date
25-
endTime: Date
26-
}
27-
28-
interface RawLesson {
29-
subject: string
30-
content: string
31-
startTime: Timestamp
32-
endTime: Timestamp
33-
}
34-
35-
interface RawClassData {
36-
className: ClassName
37-
lessons: Lessons
38-
}
39-
40-
interface ClassData {
41-
classId: ClassId
42-
className: ClassName
43-
lessons: Lessons
44-
}
45-
46-
export class ClassDataStore extends VuexModule implements ClassData {
47-
classId: ClassId = ''
48-
className: ClassName = ''
49-
lessons: Lessons = []
50-
51-
public get lessonsOnCurrentDate(): Lessons {
22+
public get lessonsOnCurrentDate(): classData.Lesson[] {
5223
const appStore = createProxy(this.$store, AppStore)
5324

5425
// Generate a new Date object with a specified date & time
@@ -73,57 +44,58 @@ export class ClassDataStore extends VuexModule implements ClassData {
7344
}
7445

7546
@mutation
76-
private setClassId(classId: ClassId) {
47+
private setClassData({ classId, className, lessons }: classData.ClassData) {
7748
this.classId = classId
78-
}
79-
80-
@mutation
81-
private setDataFromRawClassData({ className, lessons }: RawClassData) {
8249
this.className = className
8350
this.lessons = lessons
8451
}
8552

8653
@action
87-
public async loadClassData(classId: ClassId) {
88-
let className = ''
89-
const lessons: Lesson[] = []
90-
await firebase
54+
public async loadClassData(classId: classData.ClassId) {
55+
const lessons: classData.Lesson[] = []
56+
57+
const classDataDocument = firebase
9158
.firestore()
9259
.collection('classData')
9360
.doc(classId)
61+
62+
// classData ドキュメントのフィールドを取得
63+
const classDataSnapshot = await classDataDocument.get()
64+
65+
if (!classDataSnapshot.exists) throw new Error('クラスIDが間違っています')
66+
const classData = classDataSnapshot.data() as classData.ClassData
67+
const className = classData.className
68+
69+
// classData ドキュメント下の lessons コレクションを取得
70+
const classDataLessonsSnapshot = await classDataDocument
9471
.collection('Lessons')
9572
.orderBy('startTime')
9673
.get()
97-
.then(querySnapshot => {
98-
querySnapshot.forEach(function(doc) {
99-
const data = doc.data() as RawLesson
100-
const reformatData = {
101-
subject: data.subject,
102-
content: data.content,
103-
startTime: data.startTime.toDate(),
104-
endTime: new Date()
105-
}
106-
lessons.push(reformatData)
107-
})
108-
})
109-
.catch(() => {
110-
return Promise.reject(new Error('クラスIDが間違っています'))
111-
})
112-
113-
await firebase
114-
.firestore()
115-
.collection('classData')
116-
.doc(classId)
117-
.get()
118-
.then(snapshot => {
119-
if (!snapshot.exists)
120-
return Promise.reject(new Error('クラスIDが間違っています'))
12174

122-
const data = snapshot.data() as ClassData
123-
className = data.className
75+
try {
76+
classDataLessonsSnapshot.forEach(doc => {
77+
const retrieved = doc.data() as classData.database.Lesson
78+
const converted: classData.Lesson = {
79+
startTime: retrieved.startTime.toDate(),
80+
endTime: retrieved.endTime.toDate(),
81+
title: retrieved.title,
82+
subject: retrieved.subject,
83+
goal: retrieved.goal,
84+
description: retrieved.description,
85+
videos: retrieved.videos,
86+
pages: retrieved.pages,
87+
materials: retrieved.materials,
88+
isHidden: retrieved.isHidden
89+
}
90+
91+
lessons.push(converted)
12492
})
125-
this.setClassId(classId)
126-
this.setDataFromRawClassData({
93+
} catch {
94+
throw new Error('クラスIDが間違っています')
95+
}
96+
97+
this.setClassData({
98+
classId,
12799
className,
128100
lessons
129101
})

src/types/store/classData.d.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import firebase from '@/plugins/firebase'
2+
import FireStoreTimestamp = firebase.firestore.Timestamp
3+
4+
declare namespace classData {
5+
// 汎用的な型の定義
6+
export type UrlString = string
7+
export type ColorCodeString = string
8+
9+
// ID型の定義
10+
export type ClassId = string
11+
export type LessonId = string // unused
12+
13+
export interface ClassData {
14+
classId: ClassId
15+
className: string
16+
lessons: Lesson[]
17+
}
18+
19+
export interface Lesson {
20+
startTime: Date
21+
endTime: Date
22+
title: string
23+
subject: Subject
24+
goal: string
25+
description: string
26+
videos: Video[]
27+
pages: string
28+
materials: Material[]
29+
isHidden: boolean
30+
}
31+
32+
export interface Subject {
33+
name: string
34+
color: ColorCodeString
35+
}
36+
37+
export interface Video {
38+
title: string
39+
url: UrlString
40+
thumbnailUrl: UrlString
41+
}
42+
43+
export interface Material {
44+
title: string
45+
url: UrlString
46+
}
47+
48+
namespace database {
49+
export interface Lesson {
50+
startTime: FireStoreTimestamp
51+
endTime: FireStoreTimestamp
52+
title: string
53+
subject: Subject
54+
goal: string
55+
description: string
56+
videos: Video[]
57+
pages: string
58+
materials: Material[]
59+
isHidden: boolean
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)