|
|
import { atom } from 'jotai' import { IApiResult, IPage } from '@/global' import { atomWithMutation, atomWithQuery, queryClientAtom } from 'jotai-tanstack-query' import { message } from 'antd' import { t } from 'i18next' import websitesServ from '@/service/websites' import { IWebsiteDnsRecords } from '@/types/website/record'
const i18nPrifx = 'websites.record'
type SearchParams = IPage & { key?: string [key: string]: any }
export const explainTypes = [ { label: 'A', value: 'A' }, { label: 'CNAME', value: 'CNAME' }, { label: 'AAAA', value: 'AAAA' }, { label: 'NS', value: 'NS' }, { label: 'MX', value: 'MX' }, { label: 'SRV', value: 'SRV' }, { label: 'TXT', value: 'TXT' }, { label: 'CAA', value: 'CAA' }, { label: 'PTR', value: 'PTR' }, { label: t('websites.record.redirect_url'), value: 'REDIRECT_URL' }, { label: t('websites.record.forward_url'), value: 'FORWARD_URL' } ]
export const ttlOptions = [ { label: t(`${i18nPrifx}.ttl.1`, '自动'), value: 1 }, { label: t(`${i18nPrifx}.ttl.10`, '10分钟'), value: 10 }, { label: t(`${i18nPrifx}.ttl.30`, '30分钟'), value: 30 }, { label: t(`${i18nPrifx}.ttl.60`, '1小时'), value: 60 }, { label: t(`${i18nPrifx}.ttl.60`, '12小时'), value: 60 * 12 }, { label: t(`${i18nPrifx}.ttl.60`, '1天'), value: 60 * 24 }, ]
export const websiteDnsRecordsIdAtom = atom(0)
export const websiteDnsRecordsIdsAtom = atom<number[]>([])
export const websiteDnsRecordsAtom = atom<IWebsiteDnsRecords>(undefined as unknown as IWebsiteDnsRecords)
export const websiteDnsRecordsSearchAtom = atom<SearchParams>({ // key: '',
pageSize: 10, page: 1, } as SearchParams)
export const websiteDnsDomainIdAtom = atom<number>(0)
export const websiteDnsRecordssAtom = atomWithQuery((get) => { return { queryKey: [ 'websiteDnsRecordss', get(websiteDnsRecordsSearchAtom), get(websiteDnsDomainIdAtom) ], queryFn: async ({ queryKey: [ , params, id ] }) => { if (!id) { return { data: { rows: [], total: 0 } } } return await websitesServ.record.list({ ...(params as any), domain_id: id }) }, select: res => { const data = res.data data.rows = data.rows?.map(row => { return { ...row, //status: convertToBool(row.status)
} }) return data } } })
//saveOrUpdateAtom
export const saveOrUpdateWebsiteDnsRecordsAtom = atomWithMutation<IApiResult, IWebsiteDnsRecords>((get) => {
return { mutationKey: [ 'updateWebsiteDnsRecords' ], mutationFn: async (data) => { //data.status = data.status ? '1' : '0'
if (data.id === 0) { return await websitesServ.record.add(data) } return await websitesServ.record.update(data) }, onSuccess: (res) => { const isAdd = !!res.data?.id message.success(t(isAdd ? 'message.saveSuccess' : 'message.editSuccess', '保存成功'))
//更新列表
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore fix
get(queryClientAtom).invalidateQueries({ queryKey: [ 'websiteDnsRecordss', get(websiteDnsRecordsSearchAtom) ] })
return res } } })
export const deleteWebsiteDnsRecordsAtom = atomWithMutation((get) => { return { mutationKey: [ 'deleteWebsiteDnsRecords' ], mutationFn: async (ids: number[]) => { return await websitesServ.record.batchDelete(ids ?? get(websiteDnsRecordsIdsAtom)) }, onSuccess: (res) => { message.success('message.deleteSuccess') //更新列表
get(queryClientAtom).invalidateQueries({ queryKey: [ 'websiteDnsRecordss', get(websiteDnsRecordsSearchAtom) ] }) return res } } })
|