Skip to content

Commit

Permalink
feat(edit-course): create block and send request
Browse files Browse the repository at this point in the history
  • Loading branch information
danilych committed Feb 11, 2024
1 parent f31eade commit cc29ae0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
5 changes: 4 additions & 1 deletion app/redux/slices/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ const courseSlice = createSlice({
builder.addCase(fetchCourse.fulfilled, (state, action) => {
state.course.status = 'loaded'
state.course.data = action.payload.data
state.course.isError = action.payload.isError
})
builder.addCase(fetchCourse.rejected, (state, action) => {
state.course.status = 'error'

// @ts-ignore
state.course.data = null
})
builder.addCase(updateBlock.fulfilled, (state, action) => {
state.course.status = 'loaded'
state.course.isError = action.payload.isError
})
},
})

Expand Down
66 changes: 62 additions & 4 deletions app/routes/edit-course.$course.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import type { ThunkDispatch } from '@reduxjs/toolkit'
import { useParams } from '@remix-run/react'
import { Spinner } from 'flowbite-react'
import { useEffect, useState } from 'react'
import { useForm } from 'react-hook-form'
import toast, { Toaster } from 'react-hot-toast'
import { useDispatch, useSelector } from 'react-redux'
import { fetchCourse } from '~/redux/slices/course'
import { fetchCourse, updateBlock } from '~/redux/slices/course'
import {
Header2,

Check failure on line 10 in app/routes/edit-course.$course.tsx

View workflow job for this annotation

GitHub Actions / pipeline (21.x)

'Header2' is defined but never used

Check failure on line 10 in app/routes/edit-course.$course.tsx

View workflow job for this annotation

GitHub Actions / pipeline (21.x)

'Header2' is defined but never used
Header3,
Expand All @@ -15,13 +17,24 @@ import {
export default function Course() {
const params = useParams()

const { register, handleSubmit } = useForm({
defaultValues: {
Name: '',
ID: '-1',
CourseID: params.course as string,
},
mode: 'onChange',
})

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

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

const [isPostsLoading, setIsPostLoading] = useState(true)

console.log(course.data)

useEffect(() => {
let formData = new FormData()
formData.append('CourseID', params.course as string)
Expand All @@ -35,8 +48,28 @@ export default function Course() {
if (course.status === 'loaded') setIsPostLoading(false)

if (course.status === 'loading') setIsPostLoading(true)

if (course.status === 'error') setIsPostLoading(true)
})

const onSubmit = async (values: any) => {
try {
const data = await dispatch(updateBlock(values))

if (data.payload && data.payload.isError == false) {
console.log(true)

toast.success('Block was successfully created!')
} else {
toast.error('Something went wrong!')

return
}
} catch (error) {
toast.error('Something went wrong!')
}
}

return (
<div className="w-[1500px] min-h-screen py-12 my-[56px] mx-auto">
{isPostsLoading ? (
Expand Down Expand Up @@ -65,18 +98,43 @@ export default function Course() {
<div>
<Header4 className="ml-4">Додайти інформаційний блок</Header4>

<div className="mt-2 flex flex-row gap-[26px]">
<RoundedInput placeholder="Назва розділу" className="w-[656px]" />
<form
onSubmit={handleSubmit(onSubmit)}
className="mt-2 flex flex-row gap-[26px]"
>
<input
placeholder='Назва розділу'
className="pl-9 text-[#4E4E51] w-[656px] h-[50px] bg-[#F6F6F6] rounded-[50px] outline-none text-sm font-manrope font-normal border-0"
{...register('Name', { required: 'Enter name' })}
/>

<div className=" w-[310px]">
<TransparentButton className="text-[#454BE9]">
Додати
</TransparentButton>
</div>
</div>
</form>
</div>
</div>
)}

<Toaster
toastOptions={{
duration: 5000,
success: {
style: {
background: 'green',
color: 'white',
},
},
error: {
style: {
background: 'red',
color: 'white',
},
},
}}
/>
</div>
)
}

0 comments on commit cc29ae0

Please sign in to comment.