diff --git a/src/pages/icon/List.tsx b/src/pages/icon/List.tsx index 30fceb3..3c31601 100644 --- a/src/pages/icon/List.tsx +++ b/src/pages/icon/List.tsx @@ -1,23 +1,51 @@ import { useState, type FC } from 'react' -import { useGetIcons } from './hooks/useIconData' +import { useGetIcons, useDeleteIcon } from './hooks/useIconData' import { useTranslation } from 'react-i18next' import Button from '../../components/Button' import { Add } from 'iconsax-react' import CreateIcon from './components/CreateIcon' +import Td from '../../components/Td' +import Input from '../../components/Input' +import PageLoading from '../../components/PageLoading' +import TrashWithConfrim from '../../components/TrashWithConfrim' +import { toast } from 'react-toastify' +import { ErrorType } from '../../helpers/types' +import { IconType } from './types/Types' +import moment from 'moment-jalaali' const IconsList: FC = () => { const { t } = useTranslation('global') const [showModal, setShowModal] = useState(false) - const { refetch } = useGetIcons() + const [search, setSearch] = useState('') + const { data: iconsData, isLoading, refetch } = useGetIcons() + const deleteIcon = useDeleteIcon() const handleCloseModal = () => { setShowModal(false) refetch() } + const handleDelete = (id: string) => { + deleteIcon.mutate(id, { + onSuccess: () => { + refetch() + toast.success(t('success')) + }, + onError: (error: ErrorType) => { + toast.error(error.response?.data?.error?.message[0]) + } + }) + } + + const icons = (iconsData as unknown as { data?: IconType[] })?.data || [] + const filteredIcons = icons.filter((icon: IconType) => + icon.url.toLowerCase().includes(search.toLowerCase()) || + icon.group?.name?.toLowerCase().includes(search.toLowerCase()) + ) + return ( -
+
{t('icon.list_icon')} @@ -36,6 +64,75 @@ const IconsList: FC = () => {
+
+
+
+ setSearch(value)} + /> +
+
+
+ + { + isLoading ? + + : +
+ + + + + + + { + filteredIcons.map((item: IconType) => { + return ( + + + + + ) + }) + } + +
+ + + +
+
+
+ {item.url} { + const target = e.target as HTMLImageElement + target.style.display = 'none' + }} + /> +
+
+ {item.url} +
+
+
+ + +
+ handleDelete(item.id)} + isLoading={deleteIcon.isPending} + /> +
+
+
+ } + { mutationFn: api.createIcon, }); }; + +export const useDeleteIcon = () => { + return useMutation({ + mutationFn: (id: string) => api.deleteIcon(id), + }); +}; diff --git a/src/pages/icon/service/IconService.ts b/src/pages/icon/service/IconService.ts index d08ff19..c3ca213 100644 --- a/src/pages/icon/service/IconService.ts +++ b/src/pages/icon/service/IconService.ts @@ -3,9 +3,10 @@ import { CreateGroupIconType, CreateIconType, GroupIconsResponse, + IconsResponse, } from "../types/Types"; -export const getIcons = async () => { +export const getIcons = async (): Promise => { const { data } = await axios.get("/admin/icons"); return data; }; @@ -29,3 +30,8 @@ export const createIcon = async (params: CreateIconType) => { const { data } = await axios.post("/admin/icons", params); return data; }; + +export const deleteIcon = async (id: string) => { + const { data } = await axios.delete(`/admin/icons/${id}`); + return data; +}; diff --git a/src/pages/icon/types/Types.ts b/src/pages/icon/types/Types.ts index 1c949da..91dcdec 100644 --- a/src/pages/icon/types/Types.ts +++ b/src/pages/icon/types/Types.ts @@ -21,3 +21,24 @@ export type CreateIconType = { url: string; groupId: string; }; + +export type IconGroupType = { + id: string; + createdAt: string; + updatedAt: string; + deletedAt: string | null; + name: string; +}; + +export type IconType = { + id: string; + createdAt: string; + updatedAt: string; + deletedAt: string | null; + url: string; + group: IconGroupType; +}; + +export interface IconsResponse extends IResponse { + statusCode: number; +}