summaryrefslogtreecommitdiff
path: root/src/services/waybills/WaybillPanel.tsx
blob: 1b0d75d2590b90e91280a264236b7db9592e1262 (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
import React from 'react';
import { useHistory } from 'react-router-dom';
import Button from '../../components/Button';
import { patch, baseURL } from '../../requests';
import { PanelProps } from '../../lib/ServiceContext';
import { Waybill } from '../types';


const WaybillPanel: React.FC<PanelProps<Waybill>> = ({ item, mutate }) => {
  const history = useHistory();

  const handleChangeStatus = (status: Waybill['status']) => patch(`/waybills/${item._id}`, { status })
    .then(() => {
      history.push('/waybills');
      mutate({ ...item, status });
    });

  const handlePrint = () => window.open(`${baseURL}/spreadsheets/${item._id}`, '_blank');

  const handleExecute = () => handleChangeStatus('executed');
  const handleCancel = () => handleChangeStatus('cancelled');

  const executed = item.status === 'executed';

  return (
    <div className="lg:m-4 p-4 flex flex-col lg:pl-16 lg:border-l">
      <div className="grid lg:grid-cols-2">
        <Button route={`/contractors/${item.contractorId}`} variant="outlined">
          Перейти к контрагенту
        </Button>
        <Button onClick={handlePrint} variant="outlined">
          Печать
        </Button>
        <span className="text-lg text-center mt-4">Итоговая сумма: {item.total}р</span>
        <Button onClick={executed ? handleCancel : handleExecute} size="lg">
          {executed ? 'Откатить' : 'Провести'}
        </Button>
      </div>
    </div>
  );
};

export default WaybillPanel;