Files
FastGPT/src/pages/number/setting.tsx
2023-04-06 18:30:47 +08:00

176 lines
4.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { useCallback, useState } from 'react';
import {
Card,
Box,
Flex,
Button,
Table,
Thead,
Tbody,
Tr,
Th,
Td,
TableContainer,
Select,
Input,
IconButton
} from '@chakra-ui/react';
import { DeleteIcon } from '@chakra-ui/icons';
import { useForm, useFieldArray } from 'react-hook-form';
import { UserUpdateParams } from '@/types/user';
import { putUserInfo } from '@/api/user';
import { useToast } from '@/hooks/useToast';
import { useGlobalStore } from '@/store/global';
import { useUserStore } from '@/store/user';
import { UserType } from '@/types/user';
import { useQuery } from '@tanstack/react-query';
import dynamic from 'next/dynamic';
const PayRecordTable = dynamic(() => import('./components/PayRecordTable'));
const BilTable = dynamic(() => import('./components/BillTable'));
const PayModal = dynamic(() => import('./components/PayModal'));
const NumberSetting = () => {
const { userInfo, updateUserInfo, initUserInfo } = useUserStore();
const { setLoading } = useGlobalStore();
const { register, handleSubmit, control } = useForm<UserUpdateParams>({
defaultValues: userInfo as UserType
});
const [showPay, setShowPay] = useState(false);
const { toast } = useToast();
const {
fields: accounts,
append: appendAccount,
remove: removeAccount
} = useFieldArray({
control,
name: 'accounts'
});
const onclickSave = useCallback(
async (data: UserUpdateParams) => {
setLoading(true);
try {
await putUserInfo(data);
updateUserInfo(data);
toast({
title: '更新成功',
status: 'success'
});
} catch (error) {}
setLoading(false);
},
[setLoading, toast, updateUserInfo]
);
useQuery(['init'], initUserInfo);
return (
<>
{/* 核心信息 */}
<Card px={6} py={4}>
<Box fontSize={'xl'} fontWeight={'bold'}>
</Box>
<Box mt={6}>
<Flex alignItems={'center'}>
<Box flex={'0 0 60px'}>:</Box>
<Box>{userInfo?.email}</Box>
</Flex>
</Box>
<Box mt={6}>
<Flex alignItems={'center'}>
<Box flex={'0 0 60px'}>:</Box>
<Box>
<strong>{userInfo?.balance}</strong>
</Box>
<Button size={'sm'} w={'80px'} ml={5} onClick={() => setShowPay(true)}>
</Button>
</Flex>
<Box fontSize={'xs'} color={'blackAlpha.500'}>
openai
</Box>
</Box>
</Card>
{/* 账号信息 */}
<Card mt={6} px={6} py={4}>
<Flex mb={5} justifyContent={'space-between'}>
<Box fontSize={'xl'} fontWeight={'bold'}>
</Box>
<Box>
{accounts.length === 0 && (
<Button
mr={5}
variant="outline"
onClick={() =>
appendAccount({
type: 'openai',
value: ''
})
}
>
</Button>
)}
<Button onClick={handleSubmit(onclickSave)}></Button>
</Box>
</Flex>
<TableContainer>
<Table>
<Thead>
<Tr>
<Th></Th>
<Th></Th>
<Th></Th>
</Tr>
</Thead>
<Tbody>
{accounts.map((item, i) => (
<Tr key={item.id}>
<Td minW={'200px'}>
<Select
{...register(`accounts.${i}.type`, {
required: '类型不能为空'
})}
>
<option value="openai">openai</option>
</Select>
</Td>
<Td minW={'200px'} whiteSpace="pre-wrap" wordBreak={'break-all'}>
<Input
{...register(`accounts.${i}.value`, {
required: '账号不能为空'
})}
></Input>
</Td>
<Td>
<IconButton
aria-label="删除账号"
icon={<DeleteIcon />}
colorScheme={'red'}
onClick={() => {
removeAccount(i);
handleSubmit(onclickSave)();
}}
/>
</Td>
</Tr>
))}
</Tbody>
</Table>
</TableContainer>
</Card>
{/* 充值记录 */}
<PayRecordTable />
{/* 账单表 */}
<BilTable />
{showPay && <PayModal onClose={() => setShowPay(false)} />}
</>
);
};
export default NumberSetting;