Skip to content

Commit

Permalink
feat(edit-course): implement creation of lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
danilych committed Feb 11, 2024
1 parent 27ecb63 commit 6760e8a
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 22 deletions.
39 changes: 27 additions & 12 deletions app/features/hidden-list-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import toast from 'react-hot-toast'
import { removeBlock } from '~/redux/slices/course'
import { delay } from '~/widgets/helpers/delay'
import { ToasterWithOptions, TransparentButton } from '~/shared'
import { Link } from '@remix-run/react'

interface Props {
header: string
Expand All @@ -16,7 +17,13 @@ interface Props {
items: any
}

export function HiddenListComponent({ header, index, className, id, items }: Props) {
export function HiddenListComponent({
header,
index,
className,
id,
items,
}: Props) {
const [isVisibly, setVisibly] = useState(false)

const dispatch = useDispatch<ThunkDispatch<any, any, any>>()
Expand All @@ -27,12 +34,10 @@ export function HiddenListComponent({ header, index, className, id, items }: Pro

const onClickRemove = async (id: string) => {
try {
let requestData = new FormData();
requestData.append('blockID', id);
let requestData = new FormData()
requestData.append('blockID', id)

const data = await dispatch(
removeBlock(requestData)
)
const data = await dispatch(removeBlock(requestData))

if (data.payload && data.payload.isError == false) {
console.log(true)
Expand Down Expand Up @@ -86,15 +91,25 @@ export function HiddenListComponent({ header, index, className, id, items }: Pro
)}
>
{items.map((item: any, index: number) => (
<div key={index} className='bg-[#D9D9D9] py-3 pl-7 mt-1'>
<p className='text-manrope font-normal text-base text-[#1a1a1b]'>{item.elementName}</p>
<div key={index} className="bg-[#D9D9D9] py-3 pl-7 mt-1">
<p className="text-manrope font-normal text-base text-[#1a1a1b]">
{item.elementName}
</p>
</div>
))}

<div className='mt-2 flex flex-row gap-9'>
<TransparentButton className='border-[#D4D4D4]'>Додати урок</TransparentButton>

<TransparentButton className='border-[#D4D4D4]'>Додати тест</TransparentButton>
<div className="mt-2 flex flex-row gap-9">
<Link className='w-[310px]' to={`/edit-course/create-lesson/${id}`}>
<TransparentButton className="border-[#D4D4D4]">
Додати урок
</TransparentButton>
</Link>

<Link className='w-[310px]' to="/">
<TransparentButton className="border-[#D4D4D4]">
Додати тест
</TransparentButton>
</Link>
</div>
</div>

Expand Down
33 changes: 23 additions & 10 deletions app/redux/slices/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@ export const updateBlock = createAsyncThunk(
)

export const removeBlock = createAsyncThunk(
'course/removeBlock',
async (params: any) => {
const { data } = await instance.post('/CourseBlock/Delete', params)

return data
}
)
'course/removeBlock',
async (params: any) => {
const { data } = await instance.post('/CourseBlock/Delete', params)

return data
}
)

export const createLesson = createAsyncThunk(
'course/createLesson',
async (params: any) => {
const { data } = await instance.post('/Lesson/Apply', params)

return data
}
)

const initialState = {
course: {
Expand Down Expand Up @@ -71,9 +80,13 @@ const courseSlice = createSlice({
state.course.isError = action.payload.isError
})
builder.addCase(removeBlock.fulfilled, (state, action) => {
state.course.status = 'loaded'
state.course.isError = action.payload.isError
})
state.course.status = 'loaded'
state.course.isError = action.payload.isError
})
builder.addCase(createLesson.fulfilled, (state, action) => {
state.course.status = 'loaded'
state.course.isError = action.payload.isError
})
},
})

Expand Down
28 changes: 28 additions & 0 deletions app/routes/edit-course.create-lesson.$blockId.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { type V2_MetaFunction } from '@remix-run/node'
import { useParams } from '@remix-run/react'
import { useEffect } from 'react'
import { useSelector } from 'react-redux'
import { selectIsAuth } from '~/redux/slices/auth'
import { CreateLessonForm } from '~/widgets'

export const meta: V2_MetaFunction = () => {
return [{ title: 'Mentohub | Create Course' }]
}

export default function CreateLesson() {
const isAuth = useSelector(selectIsAuth)

const params = useParams()

useEffect(() => {
if (!isAuth) {
// navigate('/')
}
}, [])

Check warning on line 21 in app/routes/edit-course.create-lesson.$blockId.tsx

View workflow job for this annotation

GitHub Actions / pipeline (21.x)

React Hook useEffect has a missing dependency: 'isAuth'. Either include it or remove the dependency array

return (
<div className="bg-[#ebffd2] pb-[92px] mt-[68px]">
<CreateLessonForm id={params.blockId} />
</div>
)
}
122 changes: 122 additions & 0 deletions app/widgets/create-lesson-form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { AddVideoCover } from 'assets/images'
import { useRef } from 'react'
import { FilledButton, Header3, Text, ToasterWithOptions } from '~/shared'
import { useDispatch, useSelector } from 'react-redux'
import type { ThunkDispatch } from '@reduxjs/toolkit'
import { useForm } from 'react-hook-form'
import { objectToFormData } from './helpers/object-to-form-data'
import toast from 'react-hot-toast'
import { delay } from './helpers/delay'
import { useNavigate } from '@remix-run/react'
import { createLesson } from '~/redux/slices/course'

interface Props {
id: string | undefined
}

export function CreateLessonForm({ id }: Props) {
const inputVideoRef = useRef<HTMLInputElement | null>(null)

const navigate = useNavigate()

// @ts-ignore
const { course } = useSelector(state => state.course)

let video: string | Blob

const dispatch = useDispatch<ThunkDispatch<any, any, any>>()

const handleVideoChange = (event: any) => {
event.preventDefault()

video = event.target.files[0]
}

const { register, handleSubmit } = useForm({
defaultValues: {
CourseID: course.data.id!,
Theme: '',
Description: '',
Body: '',
CourseBlockID: id,
},
mode: 'onChange',
})

const onSubmit = async (values: any) => {
console.log(values)
let formData = objectToFormData(values)

formData.append('VideoFile', video)

console.log(formData)

const data = await dispatch(createLesson(formData))
console.log('data: ', data)

if (data.payload.isError == false) {
toast.success('Lesson was successfully created!')

await delay(3000)

navigate('/my-account/my-courses')
} else {
toast.error(data.payload.message)
}

console.log(data.payload.isError)
}

return (
<form
onSubmit={handleSubmit(onSubmit)}
className="flex gap-9 flex-col w-[992px] mx-auto min-mx-5 font-semibold text-black"
>
<Header3 className="mt-5">Створення нового уроку</Header3>

<div className="flex flex-col gap-2">
<Text className="ml-3">Тема</Text>
<input
{...register('Theme', { required: '' })}
className="mt-2 px-3 text-[#4E4E51] h-[50px] bg-white rounded-[50px] outline-none text-sm font-manrope font-normal w-full border-0"
/>
</div>

<div className="flex flex-col gap-2">
<Text className="ml-3">Опис (до 100 символів)</Text>
<textarea
{...register('Description', { required: '' })}
className="h-[150px] bg-white rounded-[25px] outline-none text-sm font-manrope font-normal w-full border-0 text-[#4E4E51]"
/>
</div>

<div className="flex flex-col gap-2">
<Text className="ml-3">Інформація</Text>
<textarea
{...register('Body', { required: '' })}
className="h-[250px] bg-white rounded-[25px] outline-none text-sm font-manrope font-normal w-full border-0 text-[#4E4E51]"
/>
</div>

<div className="flex flex-col gap-2">
<Text className="ml-3">Відео (формати MP4, MOV, WEBM)</Text>
<input
onChange={handleVideoChange}
ref={inputVideoRef}
type="file"
hidden
/>

<button type="button" onClick={() => inputVideoRef.current?.click()}>
<img src={AddVideoCover} alt="Add image" />
</button>
</div>

<FilledButton type="submit" className="mx-auto px-[50px] mt-9">
<p className="text-white w-[216px] text-2xl">Створити</p>
</FilledButton>

<ToasterWithOptions />
</form>
)
}
1 change: 1 addition & 0 deletions app/widgets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export * from './homepage/join-us'
export * from './homepage/stats'
export * from './homepage/faq'
export * from './create-course-form'
export * from './create-lesson-form'
export * from './my-account/account-menu'

0 comments on commit 6760e8a

Please sign in to comment.