learning category

This commit is contained in:
hamid zarghami
2026-02-22 14:41:23 +03:30
parent 69a8bbc779
commit fcab80bb0b
4 changed files with 61 additions and 45 deletions
+43 -42
View File
@@ -1,16 +1,45 @@
import { type FC, useState } from 'react' import { type FC, useState } from 'react'
// import { useTranslation } from 'react-i18next' // import { useTranslation } from 'react-i18next'
import Td from '../../components/Td' import Table from '../../components/Table'
import DefaultTableSkeleton from '../../components/DefaultTableSkeleton' import TrashWithConfrim from '../../components/TrashWithConfrim'
import Pagination from '../../components/Pagination' import { type ColumnType } from '../../components/types/TableTypes'
import { useGetCategory } from './hooks/useLearningData' import { useGetCategory, useDeleteCategory } from './hooks/useLearningData'
import { type CategoryLearningType } from './hooks/useLearningData' import { type CategoryLearningType } from './hooks/useLearningData'
import CreateCategory from './components/CreateCategory' import CreateCategory from './components/CreateCategory'
import { toast } from 'react-toastify'
import { extractErrorMessage } from '../../config/func'
const LearningCategory: FC = () => { const LearningCategory: FC = () => {
const [page, setPage] = useState<number>(1) const [page, setPage] = useState<number>(1)
const getCategory = useGetCategory() const getCategory = useGetCategory()
const { mutate, isPending } = useDeleteCategory()
const handleDelete = (id: string | number) => {
mutate(id, {
onError: (error) => {
toast.error(extractErrorMessage(error))
},
})
}
const columns: ColumnType<CategoryLearningType>[] = [
{
title: 'عنوان',
key: 'name',
},
{
title: '',
key: 'action',
render: (item) => (
<TrashWithConfrim
onDelete={() => handleDelete(item.id)}
isloading={isPending}
colorIcon='red'
/>
),
},
]
return ( return (
<div className='mt-4'> <div className='mt-4'>
@@ -20,48 +49,20 @@ const LearningCategory: FC = () => {
<div className='flex gap-6 mt-8'> <div className='flex gap-6 mt-8'>
<div className='flex-1'> <div className='flex-1'>
<div className='flex-1 bg-white py-2 xl:px-10 px-5 rounded-3xl'> <div className='flex-1 bg-white py-2 xl:px-10 px-5 rounded-3xl'>
{ <Table<CategoryLearningType>
getCategory.isPending ? columns={columns}
<div className='mt-4'> data={getCategory.data?.data?.categories}
<DefaultTableSkeleton tdCount={2} /> isLoading={getCategory.isPending}
</div> pagination={{
: currentPage: page,
<div className='relative overflow-x-auto rounded-3xl w-full'> totalPages: getCategory.data?.data?.pager?.totalPages || 1,
<table className='w-full text-sm '> onPageChange: setPage,
<thead className='thead'> }}
<tr> noDataMessage="هیچ دسته‌بندی یافت نشد"
<Td text='عنوان' />
<Td text={''} />
</tr>
</thead>
<tbody>
{
getCategory.data?.data?.categories?.map((item: CategoryLearningType) => {
return (
<tr key={item.id} className='tr'>
<Td text={item.name} />
<Td text={''} />
</tr>
)
})
}
</tbody>
</table>
<div className='flex justify-end pb-3'>
<Pagination
currentPage={page}
totalPages={getCategory.data?.data?.pager?.totalPages}
onPageChange={setPage}
/> />
</div> </div>
</div> </div>
}
</div>
</div>
<CreateCategory /> <CreateCategory />
</div> </div>
@@ -13,7 +13,6 @@ import { extractErrorMessage } from '@/config/func'
const CreateCategory: FC = () => { const CreateCategory: FC = () => {
// const { t } = useTranslation('global') // const { t } = useTranslation('global')
const t = (key: string) => key; // Mock translation function
const createCategory = useCreateCategory() const createCategory = useCreateCategory()
const getCategory = useGetCategory() const getCategory = useGetCategory()
@@ -22,7 +21,7 @@ const CreateCategory: FC = () => {
name: '' name: ''
}, },
validationSchema: Yup.object({ validationSchema: Yup.object({
name: Yup.string().required(t('errors.required')), name: Yup.string().required('این فیلد اجباری است'),
}), }),
onSubmit: (values) => { onSubmit: (values) => {
@@ -1,4 +1,5 @@
import { useMutation, useQuery } from "@tanstack/react-query"; import { useMutation, useQuery } from "@tanstack/react-query";
import { useQueryClient } from "@tanstack/react-query";
import * as api from "../service/LearningService"; import * as api from "../service/LearningService";
import type { import type {
CreateCategoryLearningType, CreateCategoryLearningType,
@@ -23,6 +24,16 @@ export const useCreateCategory = () => {
}); });
}; };
export const useDeleteCategory = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (id: string | number) => api.deleteCategory(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["learning-category"] });
},
});
};
export const useCreateLearning = () => { export const useCreateLearning = () => {
return useMutation({ return useMutation({
mutationFn: (variables: CreateLearningType) => mutationFn: (variables: CreateLearningType) =>
@@ -14,6 +14,11 @@ export const createCategory = async (params: CreateCategoryLearningType) => {
return data; return data;
}; };
export const deleteCategory = async (id: string | number) => {
const { data } = await axios.delete(`/admin/learnings/category/${id}`);
return data;
};
export const CreateLearning = async (params: CreateLearningType) => { export const CreateLearning = async (params: CreateLearningType) => {
const { data } = await axios.post(`/admin/learnings`, params); const { data } = await axios.post(`/admin/learnings`, params);
return data; return data;