diff --git a/auto-bid-admin/src/apis/admin.ts b/auto-bid-admin/src/apis/admin.ts new file mode 100644 index 0000000..d438bea --- /dev/null +++ b/auto-bid-admin/src/apis/admin.ts @@ -0,0 +1,109 @@ +import { generateNestParams, handleError, handleSuccess } from '.'; +import axios from '../lib/axios'; +import { IAdmin } from '../system/type'; +import { removeFalsyValues } from '../utils'; + +export const getAdmins = async (params: Record) => { + return await axios({ + url: 'admins', + params: generateNestParams(params), + withCredentials: true, + method: 'GET', + }); +}; + +export const updateAdmin = async (admin: Partial) => { + const { email, fullname, permissions } = removeFalsyValues(admin); + + try { + const { data } = await axios({ + url: 'admins/' + admin.id, + withCredentials: true, + method: 'PUT', + data: { email, fullname, permissions }, + }); + + handleSuccess(data); + + return data; + } catch (error) { + handleError(error); + } +}; + +export const grantNewPasswordAdmin = async (admin: Partial) => { + const { password } = removeFalsyValues(admin); + + try { + const { data } = await axios({ + url: 'admins/grant-new-password/' + admin.id, + withCredentials: true, + method: 'POST', + data: { password }, + }); + + handleSuccess(data); + + return data; + } catch (error) { + handleError(error); + } +}; + +export const createAdmin = async (admin: Omit) => { + const newData = removeFalsyValues(admin); + + try { + const { data } = await axios({ + url: 'admins', + withCredentials: true, + method: 'POST', + data: newData, + }); + + handleSuccess(data); + + return data; + } catch (error) { + handleError(error); + } +}; + +export const deleteAdmin = async (admin: IAdmin) => { + try { + const { data } = await axios({ + url: 'admins/' + admin.id, + withCredentials: true, + method: 'DELETE', + }); + + handleSuccess(data); + + return data; + } catch (error) { + handleError(error); + } +}; + +export const deletesAdmin = async (admins: IAdmin[]) => { + const ids = admins.reduce((prev, cur) => { + prev.push(cur.id); + return prev; + }, [] as number[]); + try { + const { data } = await axios({ + url: 'admins/deletes', + withCredentials: true, + method: 'POST', + data: { + ids, + }, + }); + + handleSuccess(data); + + return data; + } catch (error) { + handleError(error); + } +}; diff --git a/auto-bid-admin/src/apis/permission.ts b/auto-bid-admin/src/apis/permission.ts new file mode 100644 index 0000000..ec80409 --- /dev/null +++ b/auto-bid-admin/src/apis/permission.ts @@ -0,0 +1,11 @@ +import { generateNestParams } from '.'; +import axios from '../lib/axios'; + +export const getPermissions = async (params: Record) => { + return await axios({ + url: 'permissions', + params: generateNestParams(params), + withCredentials: true, + method: 'GET', + }); +}; diff --git a/auto-bid-admin/src/components/web-bid/web-account-modal.tsx b/auto-bid-admin/src/components/web-bid/web-account-modal.tsx index e544b31..a0eae98 100644 --- a/auto-bid-admin/src/components/web-bid/web-account-modal.tsx +++ b/auto-bid-admin/src/components/web-bid/web-account-modal.tsx @@ -4,11 +4,9 @@ import { useForm, zodResolver } from '@mantine/form'; import _ from 'lodash'; import { useEffect, useRef } from 'react'; import { z } from 'zod'; -import { updateBid } from '../../apis/bid'; -import { createWebBid, updateWebBid } from '../../apis/web-bid'; +import { updateWebBid } from '../../apis/web-bid'; import { useConfirmStore } from '../../lib/zustand/use-confirm'; import { IWebBid } from '../../system/type'; -import { extractDomain } from '../../utils'; export interface IWebBidModelProps extends ModalProps { data: IWebBid | null; onUpdated?: () => void; diff --git a/auto-bid-admin/src/components/web-bid/web-bid-modal.tsx b/auto-bid-admin/src/components/web-bid/web-bid-modal.tsx index 1583a9e..30f0897 100644 --- a/auto-bid-admin/src/components/web-bid/web-bid-modal.tsx +++ b/auto-bid-admin/src/components/web-bid/web-bid-modal.tsx @@ -4,7 +4,6 @@ import { useForm, zodResolver } from '@mantine/form'; import _ from 'lodash'; import { useEffect, useRef } from 'react'; import { z } from 'zod'; -import { updateBid } from '../../apis/bid'; import { createWebBid, updateWebBid } from '../../apis/web-bid'; import { useConfirmStore } from '../../lib/zustand/use-confirm'; import { IWebBid } from '../../system/type'; diff --git a/auto-bid-admin/src/hooks/index.ts b/auto-bid-admin/src/hooks/index.ts index 284f374..9f719f0 100644 --- a/auto-bid-admin/src/hooks/index.ts +++ b/auto-bid-admin/src/hooks/index.ts @@ -1,3 +1 @@ export { default as usePermissions } from './use-permissions'; -export { default as useCategories } from './use-categories'; -export { default as useTags } from './use-tags'; diff --git a/auto-bid-admin/src/hooks/use-categories.tsx b/auto-bid-admin/src/hooks/use-categories.tsx deleted file mode 100644 index b126693..0000000 --- a/auto-bid-admin/src/hooks/use-categories.tsx +++ /dev/null @@ -1,28 +0,0 @@ -import { useEffect, useState } from 'react'; -import { getCategories } from '../apis/category'; -import { ICategory } from '../system/type'; -import { ComboboxData } from '@mantine/core'; - -export default function usePermissions() { - const [data, setData] = useState([]); - - const handleGetData = async () => { - const result = await getCategories({ per_page: 100 }); - - if (!result || !result.data?.data) return; - - setData(result.data.data); - }; - - useEffect(() => { - handleGetData(); - }, []); - - return { - data, - select: data.map((item) => { - return { label: item.name, value: String(item.id) }; - }) as ComboboxData, - refresh: handleGetData, - }; -} diff --git a/auto-bid-admin/src/hooks/use-tags.tsx b/auto-bid-admin/src/hooks/use-tags.tsx deleted file mode 100644 index 76403ad..0000000 --- a/auto-bid-admin/src/hooks/use-tags.tsx +++ /dev/null @@ -1,28 +0,0 @@ -import { ComboboxData } from '@mantine/core'; -import { useEffect, useState } from 'react'; -import { getTags } from '../apis/tag'; -import { ITag } from '../system/type'; - -export default function useTags() { - const [data, setData] = useState([]); - - const handleGetData = async () => { - const result = await getTags({ per_page: 100 }); - - if (!result || !result.data?.data) return; - - setData(result.data.data); - }; - - useEffect(() => { - handleGetData(); - }, []); - - return { - data, - select: data.map((item) => { - return { label: item.name, value: String(item.id) }; - }) as ComboboxData, - refresh: handleGetData, - }; -} diff --git a/auto-bid-admin/src/system/constants/index.ts b/auto-bid-admin/src/system/constants/index.ts index 72f90f0..86a2505 100644 --- a/auto-bid-admin/src/system/constants/index.ts +++ b/auto-bid-admin/src/system/constants/index.ts @@ -3,3 +3,10 @@ export const mappingStatusColors = { 'out-bid': 'red', 'win-bid': 'green', }; + +export const mappingPermissionsColors = { + POST: 'blue', + DELETE: 'red', + GET: 'green', + PUT: 'yellow', +}; diff --git a/auto-bid-admin/src/system/type/index.ts b/auto-bid-admin/src/system/type/index.ts index 17a1560..6dc1603 100644 --- a/auto-bid-admin/src/system/type/index.ts +++ b/auto-bid-admin/src/system/type/index.ts @@ -54,3 +54,9 @@ export interface IWebBid extends ITimestamp { active: boolean; children: IBid[]; } + +export interface IPermission extends ITimestamp { + id: number; + name: string; + description: string; +}