227 lines
7.8 KiB
TypeScript
227 lines
7.8 KiB
TypeScript
import { Input, Modal } from 'antd';
|
|
import { Fragment, useEffect, useState } from 'react';
|
|
import { useUserStore } from '../store';
|
|
import { useShallow } from 'zustand/react/shallow';
|
|
import { Form } from 'antd';
|
|
import EditOutlined from '@ant-design/icons/EditOutlined';
|
|
import SaveOutlined from '@ant-design/icons/SaveOutlined';
|
|
import DeleteOutlined from '@ant-design/icons/DeleteOutlined';
|
|
import LeftOutlined from '@ant-design/icons/LeftOutlined';
|
|
import PlusOutlined from '@ant-design/icons/PlusOutlined';
|
|
import clsx from 'clsx';
|
|
import { isObjectNull } from '@/utils/is-null';
|
|
import { CardBlank } from '@kevisual/center-components/card/CardBlank.tsx';
|
|
import { Dialog, ButtonGroup, Button, DialogContent, DialogTitle } from '@mui/material';
|
|
import { IconButton } from '@kevisual/center-components/button/index.tsx';
|
|
import { useTranslation } from 'react-i18next';
|
|
const FormModal = () => {
|
|
const [form] = Form.useForm();
|
|
const userStore = useUserStore(
|
|
useShallow((state) => {
|
|
return {
|
|
showEdit: state.showEdit,
|
|
setShowEdit: state.setShowEdit,
|
|
formData: state.formData,
|
|
updateData: state.updateData,
|
|
setFormData: state.setFormData,
|
|
};
|
|
}),
|
|
);
|
|
useEffect(() => {
|
|
const open = userStore.showEdit;
|
|
if (open) {
|
|
const isNull = isObjectNull(userStore.formData);
|
|
if (isNull) {
|
|
form.setFieldsValue({});
|
|
} else form.setFieldsValue(userStore.formData);
|
|
}
|
|
}, [userStore.showEdit]);
|
|
const onFinish = async (values: any) => {
|
|
userStore.updateData(values);
|
|
};
|
|
const onClose = () => {
|
|
userStore.setShowEdit(false);
|
|
form.setFieldsValue({});
|
|
userStore.setFormData({});
|
|
};
|
|
const isEdit = userStore.formData.id;
|
|
const { t } = useTranslation();
|
|
return (
|
|
<Dialog
|
|
open={userStore.showEdit}
|
|
onClose={() => userStore.setShowEdit(false)}
|
|
sx={{
|
|
'& .MuiDialog-paper': {
|
|
width: '800px',
|
|
},
|
|
}}>
|
|
<DialogTitle>{isEdit ? t('Edit') : t('Add')}</DialogTitle>
|
|
<DialogContent>
|
|
<Form
|
|
form={form}
|
|
onFinish={onFinish}
|
|
labelCol={{
|
|
span: 4,
|
|
}}
|
|
wrapperCol={{
|
|
span: 20,
|
|
}}>
|
|
<Form.Item name='id' hidden>
|
|
<Input />
|
|
</Form.Item>
|
|
<Form.Item name='username' label='username'>
|
|
<Input />
|
|
</Form.Item>
|
|
<Form.Item name='description' label='description'>
|
|
<Input.TextArea rows={4} />
|
|
</Form.Item>
|
|
<Form.Item label=' ' colon={false}>
|
|
<Button type='submit' variant='contained'>
|
|
{t('Submit')}
|
|
</Button>
|
|
<Button className='ml-2' type='reset' onClick={onClose}>
|
|
{t('Cancel')}
|
|
</Button>
|
|
</Form.Item>
|
|
</Form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
};
|
|
export const List = () => {
|
|
const [modal, contextHolder] = Modal.useModal();
|
|
const userStore = useUserStore(
|
|
useShallow((state) => {
|
|
return {
|
|
setFormData: state.setFormData,
|
|
setShowEdit: state.setShowEdit,
|
|
list: state.list,
|
|
deleteData: state.deleteData,
|
|
getList: state.getList,
|
|
loading: state.loading,
|
|
updateData: state.updateData,
|
|
formData: state.formData,
|
|
};
|
|
}),
|
|
);
|
|
const [codeEdit, setCodeEdit] = useState(false);
|
|
const [code, setCode] = useState('');
|
|
useEffect(() => {
|
|
userStore.getList();
|
|
}, []);
|
|
|
|
const onAdd = () => {
|
|
userStore.setFormData({});
|
|
userStore.setShowEdit(true);
|
|
};
|
|
return (
|
|
<div className='w-full h-full flex'>
|
|
<div className='p-2'>
|
|
<IconButton onClick={onAdd} variant='contained'>
|
|
<PlusOutlined />
|
|
</IconButton>
|
|
</div>
|
|
<div className='flex grow overflow-hidden h-full'>
|
|
<div className='grow overflow-auto scrollbar bg-gray-100'>
|
|
<div className='flex flex-wrap gap-x-10 gap-y-4 rounded pt-10 justify-center'>
|
|
{userStore.list.length > 0 &&
|
|
userStore.list.map((item) => {
|
|
return (
|
|
<Fragment key={item.id}>
|
|
<div
|
|
className='flex text-sm gap flex-col w-[400px] max-h-[400px] bg-white p-4 rounded-lg'
|
|
key={item.id}
|
|
onClick={() => {
|
|
setCode(item.code);
|
|
userStore.setFormData(item);
|
|
setCodeEdit(true);
|
|
}}>
|
|
<div className='px-4 cursor-pointer'>
|
|
<div
|
|
className='font-bold'
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
// message.success('copy code success');
|
|
}}>
|
|
{item.username || '-'}
|
|
</div>
|
|
<div className='font-light text-xs mt-2'>{item.description ? item.description : '-'}</div>
|
|
</div>
|
|
<div className='flex mt-2 '>
|
|
<ButtonGroup
|
|
variant='contained'
|
|
color='primary'
|
|
sx={{ color: 'white', '& .MuiButton-root': { color: 'white', minWidth: '32px', width: '32px', height: '32px', padding: '6px' } }}>
|
|
<Button
|
|
onClick={(e) => {
|
|
userStore.setFormData(item);
|
|
userStore.setShowEdit(true);
|
|
setCodeEdit(false);
|
|
e.stopPropagation();
|
|
}}>
|
|
<EditOutlined />
|
|
</Button>
|
|
<Button
|
|
onClick={(e) => {
|
|
modal.confirm({
|
|
title: 'Delete',
|
|
content: 'Are you sure delete this data?',
|
|
onOk: () => {
|
|
userStore.deleteData(item.id);
|
|
},
|
|
});
|
|
e.stopPropagation();
|
|
}}>
|
|
<DeleteOutlined />
|
|
</Button>
|
|
</ButtonGroup>
|
|
</div>
|
|
</div>
|
|
</Fragment>
|
|
);
|
|
})}
|
|
<CardBlank className='w-[400px]' />
|
|
{userStore.list.length == 0 && (
|
|
<div className='text-center' key={'no-data'}>
|
|
No Data
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
<div className={clsx('bg-gray-100 border-l-gray-200 border-bg-slate-300 w-[600px] shark-0', !codeEdit && 'hidden', 'hidden')}>
|
|
<div className='bg-white p-2'>
|
|
<div className='mt-2 ml-2 flex gap-2'>
|
|
<Button
|
|
onClick={() => {
|
|
setCodeEdit(false);
|
|
userStore.setFormData({});
|
|
}}>
|
|
<LeftOutlined />
|
|
</Button>
|
|
<Button
|
|
onClick={() => {
|
|
console.log('save', userStore.formData);
|
|
userStore.updateData({ ...userStore.formData, code });
|
|
}}>
|
|
<SaveOutlined />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
<div className='h-[94%] p-2 rounded-2 shadow-xs'>
|
|
<Input.TextArea
|
|
value={code}
|
|
onChange={(value) => {
|
|
// setCode(value);
|
|
}}
|
|
className='h-full max-h-full scrollbar'
|
|
style={{ overflow: 'auto' }}
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<FormModal />
|
|
{contextHolder}
|
|
</div>
|
|
);
|
|
};
|