summaryrefslogtreecommitdiff
path: root/src/services/transfers/TransfersUpload.tsx
blob: 3a867aa81c94bb79d5df86bfcc75a7f181271396 (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
46
47
48
49
50
51
52
53
import React, { useState } 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 [file, setFile] = useState<File>();

  const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    setFile(event.target?.files?.[0]);
  };

  const handleSubmitFile = () => {
    const reader = new FileReader();
    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="Прикрепите файл"
            onChange={handleChange}
          />
        </Form>
      </Formik>
    </Page>
  );
};

export default TransfersUpload;