summaryrefslogtreecommitdiff
path: root/src/services/transfers/TransfersUpload.tsx
blob: 56bcd7e0076b4336c8f757205bb70cfde55d13a1 (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
import React, { useRef } from 'react';
import { useHistory } from 'react-router-dom';
import { Form, Formik } from 'formik';
import Input from '../../components/Input';
import Page from '../../containers/Page';
import { Action } from '../../lib/ServiceContext';
import { post } from '../../requests';

const TransfersUpload: React.FC = () => {
  const history = useHistory();
  const inputRef = useRef<HTMLInputElement>(null);

  const handleSubmitFile = () => {
    const reader = new FileReader();
    const file = inputRef?.current?.files?.[0];
    if (file) {
      reader.readAsDataURL(file);
      reader.onload = (event: ProgressEvent<FileReader>) => {
        const uri = event.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="Прикрепите файл" ref={inputRef} />
        </Form>
      </Formik>
    </Page>
  );
};

export default TransfersUpload;