-
Notifications
You must be signed in to change notification settings - Fork 69
/
AdminLessonInfo.tsx
181 lines (164 loc) · 5.02 KB
/
AdminLessonInfo.tsx
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import React, { useEffect, useState } from 'react'
import * as Sentry from '@sentry/browser'
import { FormCard } from '../../FormCard'
import _ from 'lodash'
import {
getPropertyArr,
makeGraphqlVariable,
errorCheckAllFields
} from '../../../helpers/admin/adminHelpers'
import {
Lesson,
useCreateLessonMutation,
useUpdateLessonMutation
} from '../../../graphql/index'
import { AdminLessonChallenges, NewChallenge } from './AdminLessonChallenges'
import { lessonSchema } from '../../../helpers/formValidation'
import { formChange } from '../../../helpers/formChange'
type LessonInfoProps = {
lessons: Lesson[] | undefined
setLessons: React.Dispatch<React.SetStateAction<Lesson[] | null>>
selectedLesson: number
}
type EditLessonProps = {
setLessons: React.Dispatch<React.SetStateAction<Lesson[] | null>>
lesson: Lesson | undefined
}
type NewLessonProps = {
setLessons: React.Dispatch<React.SetStateAction<Lesson[] | null>>
}
// Creates card for a lessons's information to update
const EditLesson: React.FC<EditLessonProps> = ({ setLessons, lesson }) => {
const [alterLesson, { loading, data, error }] = useUpdateLessonMutation()
const [lessonProperties, setLessonProperties] = useState(
getPropertyArr(lesson, ['challenges', '__typename', 'modules'])
)
// when data is fully loaded after sending mutation request, update front-end lessons info
useEffect(() => {
!loading && data && setLessons(data.updateLesson)
}, [data])
// alter gets called when someone clicks button to update a lesson
const alter = async () => {
const newProperties = [...lessonProperties]
const valid = await errorCheckAllFields(newProperties, lessonSchema)
if (!valid) {
setLessonProperties(newProperties)
return
}
try {
await alterLesson(makeGraphqlVariable(lessonProperties))
} catch (err) {
Sentry.captureException(err)
}
}
const handleChange = async (value: string, propertyIndex: number) => {
await formChange(
value,
propertyIndex,
lessonProperties,
setLessonProperties,
lessonSchema
)
}
return (
<>
<span className="text-primary fw-bold display-3">Lesson Info</span>
<div className="mt-3">
<FormCard
onChange={handleChange}
values={lessonProperties}
submitError={error?.message}
onSubmit={{ title: 'Update Lesson', onClick: alter }}
title={lesson && lesson.title + ''}
/>
</div>
</>
)
}
const newLessonAttributes = {
title: '',
description: '',
docUrl: '',
githubUrl: '',
videoUrl: '',
order: '',
slug: '',
chatUrl: ''
}
// Renders when someone clicks on `create new button` on the sidebar
const NewLesson: React.FC<NewLessonProps> = ({ setLessons }) => {
const [createLesson, { loading, data, error }] = useCreateLessonMutation()
const [lessonProperties, setLessonProperties] = useState(
getPropertyArr(newLessonAttributes)
)
// when data is fully loaded after sending mutation request, update front-end lessons info
useEffect(() => {
!loading && data && setLessons(data.createLesson)
}, [data])
// alter gets called when someone clicks button to create a lesson
const alter = async () => {
const newProperties = [...lessonProperties]
const valid = await errorCheckAllFields(newProperties, lessonSchema)
if (!valid) {
setLessonProperties(newProperties)
return
}
try {
await createLesson(makeGraphqlVariable(lessonProperties))
window.location.reload()
} catch (err) {
Sentry.captureException(err)
}
}
const handleChange = async (value: string, propertyIndex: number) => {
await formChange(
value,
propertyIndex,
lessonProperties,
setLessonProperties,
lessonSchema
)
}
return (
<div className="col-8 text-center">
<div className="mb-2">
<span className="text-primary fw-bold display-3">
Create New Lesson
</span>
</div>
<FormCard
onChange={handleChange}
values={lessonProperties}
submitError={error?.message}
onSubmit={{ title: 'Create Lesson', onClick: alter }}
/>
</div>
)
}
export const AdminLessonInfo: React.FC<LessonInfoProps> = ({
setLessons,
lessons,
selectedLesson
}) => {
// true when user clicks on `create new lesson` button
if (!lessons || selectedLesson === lessons.length) {
return <NewLesson setLessons={setLessons} />
}
// set currently selected lesson
const lesson = lessons[selectedLesson] ? lessons[selectedLesson] : lessons[0]
const lessonId = lesson.id
return (
<div className="col-8 text-center" key={_.uniqueId()}>
<EditLesson setLessons={setLessons} lesson={lesson} />
<hr />
<NewChallenge setLessons={setLessons} lessonId={lessonId} />
<hr />
<span className="text-primary fw-bold display-3">Lesson Challenges</span>
<AdminLessonChallenges
challenges={lesson && lesson.challenges}
lessonId={lessonId}
setLessons={setLessons}
/>
</div>
)
}