summaryrefslogtreecommitdiff
path: root/src/services/transfers
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2021-03-25 23:09:49 +0300
committereug-vs <eug-vs@keemail.me>2021-03-25 23:09:49 +0300
commit062f10a25d43b875d187cf582b2ecf96d075ec26 (patch)
treec6b43379325ba22a051827e6a461a8f8fed98402 /src/services/transfers
parent77ac1549e2ab5ac68a1a7464ada9be7e2a2aad92 (diff)
downloadcommercel-ui-062f10a25d43b875d187cf582b2ecf96d075ec26.tar.gz
refactor: move services to separate folder
Diffstat (limited to 'src/services/transfers')
-rw-r--r--src/services/transfers/TransferForm.tsx44
-rw-r--r--src/services/transfers/TransfersUpload.tsx44
-rw-r--r--src/services/transfers/index.ts35
3 files changed, 123 insertions, 0 deletions
diff --git a/src/services/transfers/TransferForm.tsx b/src/services/transfers/TransferForm.tsx
new file mode 100644
index 0000000..3502609
--- /dev/null
+++ b/src/services/transfers/TransferForm.tsx
@@ -0,0 +1,44 @@
+import React from 'react';
+import { Form, FormikProps } from 'formik';
+import moment from 'moment';
+import Input from '../../components/Input';
+import Select from '../../components/Select';
+import hooks from '../../hooks/useAPIClient';
+
+
+const mapper = (item: any) => ({ key: item._id, label: item.name });
+
+
+const TransferForm: React.FC<FormikProps<any>> = ({ setFieldValue, values }) => {
+ const { data: contractors } = hooks.contractors.useList();
+
+ if (!values.date) setFieldValue('date', moment().format('YYYY-MM-DD'));
+ if (!values.contractorId && contractors?.length) setFieldValue('contractorId', contractors[0]._id);
+
+ return (
+ <Form id="form">
+ <div className="grid grid-cols-2">
+ <Select
+ name="contractorId"
+ label="Контрагент"
+ options={contractors?.map(mapper)}
+ required
+ />
+ <Input name="date" type="date" label="Дата" required />
+ </div>
+ <div className="grid grid-cols-2">
+ <Select
+ name="operation"
+ label="Операция"
+ options={[
+ { key: 'in', label: 'Приход' },
+ { key: 'out', label: 'Расход' },
+ ]}
+ />
+ <Input name="amount" type="number" label="Сумма" required />
+ </div>
+ </Form>
+ );
+};
+
+export default TransferForm;
diff --git a/src/services/transfers/TransfersUpload.tsx b/src/services/transfers/TransfersUpload.tsx
new file mode 100644
index 0000000..1c81b08
--- /dev/null
+++ b/src/services/transfers/TransfersUpload.tsx
@@ -0,0 +1,44 @@
+import React from 'react';
+import { useHistory } from 'react-router-dom';
+import { Form, Formik } from 'formik';
+import Button from '../../components/Button';
+import Input from '../../components/Input';
+import Page, { Action } from '../../containers/Page';
+import { post } from '../../requests';
+
+const TransfersUpload: React.FC = () => {
+ const history = useHistory();
+
+ const handleSubmitFile = () => {
+ const reader = new FileReader();
+ const element = document.getElementById('file') as HTMLInputElement;
+ const file = element?.files?.[0];
+ if (file) {
+ reader.readAsDataURL(file);
+ reader.onload = (e: any) => {
+ const uri = e.target.result;
+ post('/uploads', { uri }).then(history.goBack);
+ };
+ }
+ };
+
+ const actions: Action[] = [
+ { name: 'Назад', variant: 'outlined', onClick: history.goBack },
+ { name: 'Загрузить', type: 'submit', form: 'form' },
+ ];
+
+ return (
+ <Page
+ title="Загрузить выписку"
+ actions={actions}
+ >
+ <Formik onSubmit={handleSubmitFile} initialValues={{}}>
+ <Form id="form">
+ <Input name="file" type="file" accept=".pdf" label="Прикрепите файл" id="file" />
+ </Form>
+ </Formik>
+ </Page>
+ );
+};
+
+export default TransfersUpload;
diff --git a/src/services/transfers/index.ts b/src/services/transfers/index.ts
new file mode 100644
index 0000000..09f6e04
--- /dev/null
+++ b/src/services/transfers/index.ts
@@ -0,0 +1,35 @@
+import Form from './TransferForm';
+import UploadPage from './TransfersUpload';
+
+import { operationNames } from '../constants';
+
+const service = {
+ route: 'transfers',
+ name: 'Переводы',
+ nameSingular: 'Перевод',
+ tableFields: [
+ { key: 'date', label: 'Дата', transform: (date: string) => new Date(date).toLocaleDateString() },
+ { key: 'contractor.name', label: 'Контрагент' },
+ { key: 'operation', label: 'Операция', transform: (op: 'in' | 'out') => operationNames[op] },
+ { key: 'amount', label: 'Сумма' },
+ ],
+ actions: [
+ {
+ name: 'Загрузить выписку',
+ route: '/transfers/upload',
+ variant: 'outlined',
+ },
+ {
+ name: 'Добавить',
+ route: '/transfers/add',
+ },
+ ],
+ default: {
+ operation: 'in',
+ records: [],
+ },
+ routes: { upload: UploadPage },
+ Form,
+};
+
+export default service;