learning crud
This commit is contained in:
@@ -5,46 +5,25 @@ import { useFormik } from 'formik'
|
||||
import { type CategoryLearningType, type CreateLearningType } from './hooks/useLearningData'
|
||||
import * as Yup from 'yup'
|
||||
import { useCreateLearning, useGetCategory } from './hooks/useLearningData'
|
||||
// import { useSingleUpload } from '../service/hooks/useServiceData'
|
||||
import { useSingleUpload } from '@/pages/uploader/hooks/useUploader'
|
||||
import { TickCircle } from 'iconsax-react'
|
||||
import Input from '../../components/Input'
|
||||
import UploadBox from '../../components/UploadBox'
|
||||
import Select from '../../components/Select'
|
||||
import Textarea from '../../components/Textarea'
|
||||
import DefaultTableSkeleton from '../../components/DefaultTableSkeleton'
|
||||
import CreateLearningSidebar from './components/CreateLearningSidebar'
|
||||
// import { ErrorType } from '../../helpers/types'
|
||||
// Define ErrorType inline since we're using static data
|
||||
type ErrorType = {
|
||||
response?: {
|
||||
data?: {
|
||||
error: {
|
||||
message: string[];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
import { toast } from 'react-toastify'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { Paths } from '../../config/Paths'
|
||||
import { extractErrorMessage } from '@/config/func'
|
||||
|
||||
const CreateLearning: FC = () => {
|
||||
|
||||
const navigate = useNavigate()
|
||||
// const { t } = useTranslation('global')
|
||||
const t = (key: string) => key; // Mock translation function
|
||||
// const singleUpload = useSingleUpload()
|
||||
|
||||
// Mock upload hook
|
||||
const singleUpload = {
|
||||
mutateAsync: (formData: FormData) => {
|
||||
return Promise.resolve({
|
||||
data: {
|
||||
url: "/src/assets/images/new_order.png" // Mock URL
|
||||
}
|
||||
});
|
||||
},
|
||||
isPending: false
|
||||
};
|
||||
const singleUpload = useSingleUpload()
|
||||
|
||||
const createLearning = useCreateLearning()
|
||||
const getCategory = useGetCategory()
|
||||
@@ -67,44 +46,26 @@ const CreateLearning: FC = () => {
|
||||
categoryId: Yup.string().required(t('errors.required'))
|
||||
}),
|
||||
onSubmit: async (values) => {
|
||||
return /*
|
||||
if (videoFile && coverFile) {
|
||||
|
||||
const formData = new FormData()
|
||||
formData.append('file', videoFile)
|
||||
await singleUpload.mutateAsync(formData, {
|
||||
onSuccess: (data) => {
|
||||
values.videoUrl = data?.data?.url
|
||||
},
|
||||
onError: (error: ErrorType) => {
|
||||
toast.error(error.response?.data?.error?.message[0])
|
||||
}
|
||||
})
|
||||
const videoResult = await singleUpload.mutateAsync(videoFile)
|
||||
values.videoUrl = videoResult?.data?.url ?? ''
|
||||
|
||||
const formDataCover = new FormData()
|
||||
formDataCover.append('file', coverFile)
|
||||
await singleUpload.mutateAsync(formDataCover, {
|
||||
onSuccess: (data) => {
|
||||
values.coverUrl = data?.data?.url
|
||||
},
|
||||
onError: (error: ErrorType) => {
|
||||
toast.error(error.response?.data?.error?.message[0])
|
||||
}
|
||||
})
|
||||
const coverResult = await singleUpload.mutateAsync(coverFile)
|
||||
values.coverUrl = coverResult?.data?.url ?? ''
|
||||
|
||||
createLearning.mutate(values, {
|
||||
onSuccess: () => {
|
||||
toast.success('با موفقیت ثبت شد')
|
||||
navigate(Paths.learning.list)
|
||||
},
|
||||
onError: (error: ErrorType) => {
|
||||
toast.error(error.response?.data?.error?.message[0])
|
||||
onError: (error) => {
|
||||
toast.error(extractErrorMessage(error))
|
||||
}
|
||||
})
|
||||
} else {
|
||||
toast.error('لطفا ویدیو و کاور را انتخاب کنید')
|
||||
}
|
||||
*/
|
||||
}
|
||||
})
|
||||
|
||||
@@ -146,6 +107,22 @@ const CreateLearning: FC = () => {
|
||||
error_text={formik.touched.title && formik.errors.title ? formik.errors.title : ''}
|
||||
/>
|
||||
|
||||
<div className='rowTwoInput mt-5'>
|
||||
<div className='flex-1'>
|
||||
<UploadBox
|
||||
label='آپلود کاور'
|
||||
onChange={(files: File[]) => setCoverFile(files[0])}
|
||||
/>
|
||||
</div>
|
||||
<div className='flex-1'>
|
||||
|
||||
<UploadBox
|
||||
label='آپلود ویدیو'
|
||||
onChange={(files: File[]) => setVideoFile(files[0])}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className='rowTwoInput mt-5'>
|
||||
<Select
|
||||
items={getCategory.data?.data?.categories?.map((item: CategoryLearningType) => ({ label: item.name, value: item.id.toString() }))}
|
||||
@@ -176,6 +153,8 @@ const CreateLearning: FC = () => {
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user