summaryrefslogtreecommitdiff
path: root/src/services/transfers/TransfersUpload.tsx
blob: 18a33b8660ed492908c6103a14cae406afeeb7fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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 from '../../containers/Page';
import { Action } from '../../containers/Service/ServiceContext';
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;