-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdashboard.course.$lessonId.edit.tsx
More file actions
156 lines (140 loc) · 5.42 KB
/
dashboard.course.$lessonId.edit.tsx
File metadata and controls
156 lines (140 loc) · 5.42 KB
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
import { Dialog, Transition } from '@headlessui/react'
import { Fragment } from 'react'
import type { ActionFunction, LoaderFunction } from '@remix-run/node'
import { json, redirect } from '@remix-run/node'
import { Form, useLoaderData, useMatches, useNavigate } from '@remix-run/react'
import { Lesson } from '@prisma/client'
import { Field } from '~/components/form-elements'
import { classNames } from '~/utils/class-names'
import { Handle } from '~/utils/types'
import { getLessonById, updateLessonDescription } from '~/models/lesson'
import { requireUser } from '~/services/auth.server'
import { getFirstCourse } from '~/models/course'
import { requireCourseAuthor } from '~/utils/permissions'
export const handle: Handle = { name: 'Edit' }
export const loader: LoaderFunction = async ({ request, params }) => {
const user = await requireUser(request)
const course = await getFirstCourse()
const { lessonId } = params
if (!lessonId) {
return redirect('/dashboard/course')
}
if (!requireCourseAuthor(user, course)) {
return redirect('/dashboard')
}
const lesson = await getLessonById(lessonId)
if (!lesson) {
return redirect('/dashboard/course')
}
return { lesson }
}
export const action: ActionFunction = async ({ request, params }) => {
const user = await requireUser(request)
const course = await getFirstCourse()
if (!requireCourseAuthor(user, course)) {
return redirect('/dashboard')
}
const { lessonId } = params
if (!lessonId) {
return redirect('/dashboard/course')
}
const formData = await request.formData()
const description = formData.get('description')
if (typeof description !== 'string') {
return {
formError: 'Form not submitted correctly.',
field: formData.entries(),
}
}
const lesson = await updateLessonDescription(lessonId, description)
if (!lesson) {
return json(
{ lessonId, description, error: 'Failed to update lesson description' },
{ status: 500 }
)
}
return redirect(`/dashboard/course/${lesson.id}`)
}
export default function EditLesson() {
const matches = useMatches()
const lessonId = matches[matches.length - 1]?.params?.lessonId
const navigate = useNavigate()
const { lesson } = useLoaderData<{ lesson: Lesson }>()
return (
<Transition.Root show as={Fragment}>
<Dialog
as="div"
className="fixed z-10 inset-0 overflow-y-auto"
onClose={() =>
navigate(`/dashboard/course/${lessonId}`, {
replace: true,
})
}
>
<div className="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
<Transition.Child
as={Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="ease-in duration-200"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<Dialog.Overlay className="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" />
</Transition.Child>
{/* This element is to trick the browser into centering the modal contents. */}
<span
className="hidden sm:inline-block sm:align-middle sm:h-screen"
aria-hidden="true"
>
​
</span>
<Transition.Child
as={Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
enterTo="opacity-100 translate-y-0 sm:scale-100"
leave="ease-in duration-200"
leaveFrom="opacity-100 translate-y-0 sm:scale-100"
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
>
<div className="inline-block align-bottom bg-white rounded-lg px-4 pt-5 pb-4 text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-5xl sm:w-full sm:p-6">
<div className="px-4 py-5 sm:p-6">
<h3 className="text-lg leading-6 font-medium text-gray-900">
Ubah deskripsi materi
</h3>
<Form replace method="post">
<div className="mt-5">
<div className="rounded-md bg-gray-50 px-6 py-5 mt-5">
<Field
type="textarea"
name="description"
label="Deskripsi"
placeholder="Tuliskan deskripsi sesuai dengan materi"
autoCapitalize="sentence"
rows={10}
defaultValue={lesson.description ?? ''}
/>
<button
type="submit"
aria-label="Verifikasi"
className={classNames(
'inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-blue-500',
'bg-indigo-600 hover:bg-indigo-700',
'mt-4'
)}
>
Ubah
</button>
</div>
</div>
</Form>
</div>
</div>
</Transition.Child>
</div>
</Dialog>
</Transition.Root>
)
}