learning crud

This commit is contained in:
hamid zarghami
2026-02-22 14:34:22 +03:30
parent 5208e9e51a
commit 69a8bbc779
7 changed files with 60 additions and 293 deletions
-3
View File
@@ -9,9 +9,6 @@ import CreateCategory from './components/CreateCategory'
const LearningCategory: FC = () => {
// const { t } = useTranslation('global')
const t = (key: string) => key; // Mock translation function
const [page, setPage] = useState<number>(1)
const getCategory = useGetCategory()
+28 -49
View File
@@ -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>
-3
View File
@@ -9,9 +9,6 @@ import { type LearningItemType } from './hooks/useLearningData'
import Table from '../../components/Table'
import { type ColumnType } from '../../components/types/TableTypes'
const LearningList: FC = () => {
// const { t } = useTranslation('global')
// const t = (key: string) => key; // Mock translation function - not needed anymore
const [page, setPage] = useState<number>(1)
const getLearning = useGetLearning(page)
@@ -6,19 +6,9 @@ import { TickCircle } from 'iconsax-react'
import * as Yup from 'yup'
import { useFormik } from 'formik'
import { toast } from 'react-toastify'
// import { ErrorType } from '../../../helpers/types'
// Define ErrorType inline since we're using static data
type ErrorType = {
response?: {
data?: {
error: {
message: string[];
};
};
};
};
import { type CreateCategoryLearningType } from '../hooks/useLearningData'
import { useCreateCategory, useGetCategory } from '../hooks/useLearningData'
import { extractErrorMessage } from '@/config/func'
const CreateCategory: FC = () => {
@@ -42,8 +32,8 @@ const CreateCategory: FC = () => {
getCategory.refetch()
toast.success('با موفقیت ثبت شد')
},
onError: (error: ErrorType) => {
toast.error(error?.response?.data?.error?.message[0])
onError: (error) => {
toast.error(extractErrorMessage(error))
}
})
}
@@ -8,10 +8,6 @@ type Props = {
}
const CreateLearningSidebar: FC<Props> = (props: Props) => {
// const { t } = useTranslation('global')
const t = (key: string) => key; // Mock translation function
return (
<div className='bg-white mt-10 w-sidebar text-xs hidden 2xl:block h-fit px-5 py-7 rounded-3xl'>
+25 -217
View File
@@ -1,230 +1,38 @@
// import { useMutation, useQuery } from "@tanstack/react-query";
// import * as api from "../service/LearningService";
import { useMutation, useQuery } from "@tanstack/react-query";
import * as api from "../service/LearningService";
import type {
CreateCategoryLearningType,
CreateLearningType,
CategoryLearningType,
LearningItemType,
} from "../types/LearningTypes";
// Define types inline to avoid import issues
export type CreateCategoryLearningType = {
name: string;
};
export type CreateLearningType = {
title: string;
description: string;
coverUrl: string;
videoUrl: string;
videoDuration: string;
categoryId: string;
};
export type CategoryLearningType = {
id: number;
name: string;
};
export type LearningItemType = {
id: string;
title: string;
description: string;
coverUrl: string;
videoUrl: string;
videoDuration: string;
category: {
id: string;
createdAt: string;
updatedAt: string;
name: string;
};
createdAt: string;
updatedAt: string;
};
// Static data for categories
const staticCategories: CategoryLearningType[] = [
{ id: 1, name: "برنامه‌نویسی" },
{ id: 2, name: "طراحی UI/UX" },
{ id: 3, name: "مدیریت پروژه" },
{ id: 4, name: "بازاریابی دیجیتال" },
{ id: 5, name: "هوش مصنوعی" },
];
// Static data for learnings
const staticLearnings: LearningItemType[] = [
{
id: "1",
title: "آموزش React برای مبتدیان",
description: "یادگیری مبانی React و ساخت اولین اپلیکیشن",
coverUrl: "/src/assets/images/new_order.png",
videoUrl: "",
videoDuration: "45:30",
category: {
id: "1",
name: "برنامه‌نویسی",
createdAt: "2024-01-01",
updatedAt: "2024-01-01",
},
createdAt: "2024-01-01",
updatedAt: "2024-01-01",
},
{
id: "2",
title: "طراحی رابط کاربری مدرن",
description: "اصول طراحی UI و ساخت رابط‌های کاربری زیبا",
coverUrl: "/src/assets/images/support1.png",
videoUrl: "",
videoDuration: "32:15",
category: {
id: "2",
name: "طراحی UI/UX",
createdAt: "2024-01-02",
updatedAt: "2024-01-02",
},
createdAt: "2024-01-02",
updatedAt: "2024-01-02",
},
{
id: "3",
title: "مدیریت پروژه با Agile",
description: "روش‌های مدیریت پروژه چابک و اسکرام",
coverUrl: "/src/assets/images/new_order.png",
videoUrl: "",
videoDuration: "28:45",
category: {
id: "3",
name: "مدیریت پروژه",
createdAt: "2024-01-03",
updatedAt: "2024-01-03",
},
createdAt: "2024-01-03",
updatedAt: "2024-01-03",
},
{
id: "4",
title: "بازاریابی در شبکه‌های اجتماعی",
description: "استراتژی‌های موثر بازاریابی دیجیتال",
coverUrl: "/src/assets/images/support1.png",
videoUrl: "",
videoDuration: "41:20",
category: {
id: "4",
name: "بازاریابی دیجیتال",
createdAt: "2024-01-04",
updatedAt: "2024-01-04",
},
createdAt: "2024-01-04",
updatedAt: "2024-01-04",
},
{
id: "5",
title: "مقدمه‌ای بر یادگیری ماشین",
description: "مبانی هوش مصنوعی و یادگیری ماشین",
coverUrl: "/src/assets/images/new_order.png",
videoUrl: "",
videoDuration: "55:10",
category: {
id: "5",
name: "هوش مصنوعی",
createdAt: "2024-01-05",
updatedAt: "2024-01-05",
},
createdAt: "2024-01-05",
updatedAt: "2024-01-05",
},
];
export type { CreateCategoryLearningType, CreateLearningType, CategoryLearningType, LearningItemType };
export const useGetCategory = () => {
// Commented out API call
// return useQuery({
// queryKey: ["learning-category"],
// queryFn: api.getCategory,
// });
// Return static data with same structure as API response
return {
data: {
data: {
categories: staticCategories,
pager: {
totalPages: 1,
currentPage: 1,
totalItems: staticCategories.length,
},
},
},
isPending: false,
isLoading: false,
refetch: () => Promise.resolve(),
};
return useQuery({
queryKey: ["learning-category"],
queryFn: api.getCategory,
});
};
export const useCreateCategory = () => {
// Commented out API call
// return useMutation({
// mutationFn: (variables: CreateCategoryLearningType) =>
// api.createCategory(variables),
// });
// Return mock mutation
return {
mutate: (variables: CreateCategoryLearningType, options?: any) => {
// Simulate success
setTimeout(() => {
if (options?.onSuccess) {
options.onSuccess();
}
}, 500);
},
isPending: false,
isLoading: false,
};
return useMutation({
mutationFn: (variables: CreateCategoryLearningType) =>
api.createCategory(variables),
});
};
export const useCreateLearning = () => {
// Commented out API call
// return useMutation({
// mutationFn: (variables: CreateLearningType) =>
// api.CreateLearning(variables),
// });
// Return mock mutation
return {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
mutate: (options?: any) => {
// Simulate success
setTimeout(() => {
if (options?.onSuccess) {
options.onSuccess();
}
}, 500);
},
isPending: false,
isLoading: false,
};
return useMutation({
mutationFn: (variables: CreateLearningType) =>
api.CreateLearning(variables),
});
};
export const useGetLearning = (page: number) => {
// Commented out API call
// return useQuery({
// queryKey: ["learnings", page],
// queryFn: () => api.getLearnings(page),
// });
// Return static data with pagination
const itemsPerPage = 10;
const startIndex = (page - 1) * itemsPerPage;
const endIndex = startIndex + itemsPerPage;
const paginatedLearnings = staticLearnings.slice(startIndex, endIndex);
return {
data: {
data: {
learnings: paginatedLearnings,
pager: {
totalPages: Math.ceil(staticLearnings.length / itemsPerPage),
currentPage: page,
totalItems: staticLearnings.length,
},
},
},
isPending: false,
isLoading: false,
};
return useQuery({
queryKey: ["learnings", page],
queryFn: () => api.getLearnings(page),
});
};
@@ -5,21 +5,21 @@ import type {
} from "../types/LearningTypes";
export const getCategory = async () => {
const { data } = await axios.get(`/learnings/category`);
const { data } = await axios.get(`/admin/learnings/category`);
return data;
};
export const createCategory = async (params: CreateCategoryLearningType) => {
const { data } = await axios.post(`/learnings/category`, params);
const { data } = await axios.post(`/admin/learnings/category`, params);
return data;
};
export const CreateLearning = async (params: CreateLearningType) => {
const { data } = await axios.post(`/learnings`, params);
const { data } = await axios.post(`/admin/learnings`, params);
return data;
};
export const getLearnings = async (page: number) => {
const { data } = await axios.get(`/learnings?page=${page}`);
const { data } = await axios.get(`/admin/learnings?page=${page}`);
return data;
};