All files / src/components Administrator.tsx

16.98% Statements 9/53
0% Branches 0/14
0% Functions 0/9
16.98% Lines 9/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204                1x               1x                                 1x             1x               1x               1x           1x         1x                             1x                                                                                                                                                                                                                                                  
import { format } from 'date-fns';
import React, { useEffect, useState } from 'react';
import styled from 'styled-components/macro';
import { axiosInstance } from '../utils/axiosInstance';
import { useSnackbar } from 'notistack';
import CloseIcon from '@material-ui/icons/Close';
import { SyncLoader } from 'react-spinners';
 
const StyledCloseIcon = styled(CloseIcon)`
  color: #000000;
  &:hover {
    color: white;
    cursor: pointer;
  }
`;
 
const SaveButton = styled.button`
  display: flex;
  justify-content: center;
  align-items: center;
  user-select: none;
  background-color: #c7a424;
  border: none;
  font-weight: bold;
  cursor: pointer;
  height: 40px;
  &:hover {
    color: #ffffff;
    box-shadow: 0px 5px 4px 0px rgba(0, 0, 0, 0.24);
  }
 
  width: 150px;
`;
const AdministratorWrapper = styled.div`
  display: flex;
  flex-direction: column;
  margin: 0 auto;
  height: 100vh;
`;
 
const Wrap = styled.div`
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: center;
  align-items: center;
`;
 
const LogoWrapper = styled.div`
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: center;
  align-items: center;
`;
 
const Text = styled.div`
  font-family: 'Roboto', sans-serif;
  font-size: 5rem;
  user-select: none;
`;
 
const Logo = styled.img`
  width: 400px;
  height: 400px;
`;
 
const Form = styled.form`
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
 
  input {
    padding: 5px;
    margin-top: 10px;
    margin-bottom: 10px;
    width: 210px;
  }
`;
 
export const Administrator = () => {
  const { enqueueSnackbar } = useSnackbar();
  const { closeSnackbar } = useSnackbar();
 
  const today = new Date();
  const dd = String(today.getDate()).padStart(2, '0');
  const mm = String(today.getMonth() + 1).padStart(2, '0');
  const yyyy = today.getFullYear();
 
  const date = yyyy + '-' + mm + '-' + dd;
 
  const [selectedFile, setSelectedFile] = useState<File | null>(null);
  const [startFirstDate, setStartFirstDate] = useState<Date | null>(null);
  const [endFirstDate, setEndFirstDate] = useState<Date | null>(null);
  const [startSecondDate, setStartSecondDate] = useState<Date | null>(null);
  const [endSecondDate, setEndSecondDate] = useState<Date | null>(null);
  const [loading, setLoading] = useState(false);
 
 
  const uploadFile = async (event: React.FormEvent<HTMLFormElement>) => {
    const action = (key: any) => (
      <>
        <StyledCloseIcon
          onClick={() => {
            closeSnackbar(key);
          }}
        ></StyledCloseIcon>
      </>
    );
    event.preventDefault();
    const formData = new FormData();
    formData.append('file', selectedFile as Blob);
    if (startFirstDate !== null) {
      formData.append('firstTourBegin', format(startFirstDate, 'dd.MM.yyyy'));
    }
    if (endFirstDate !== null) {
      formData.append('firstTourEnd', format(endFirstDate, 'dd.MM.yyyy'));
    }
    if (startSecondDate !== null) {
      formData.append('secondTourBegin', format(startSecondDate, 'dd.MM.yyyy'));
    }
    if (endSecondDate !== null) {
      formData.append('secondTourEnd', format(endSecondDate, 'dd.MM.yyyy'));
    }
 
    const config = {
      headers: {
        'content-type': 'multipart/form-data',
      },
    };
 
    try {
      setLoading(true);
      const response = await axiosInstance.post(
        `${process.env.REACT_APP_API_URL}/api/v1/configurator/config/`,
        formData,
        config,
      );
      enqueueSnackbar('Ustawienia zostały zapisane', {
        variant: 'success',
        action,
      });
    } catch (e) {
      enqueueSnackbar('Ustawienia nie zostały zapisane', {
        variant: 'error',
        action,
      });
      console.log(e);
    }
    setLoading(false);
  };
 
  return (
    <AdministratorWrapper>
      <Wrap>
        <LogoWrapper>
          <Logo alt="logo" src="https://plannaplan.pl/img/logo.svg" />
          <Text> plan na plan </Text>
        </LogoWrapper>
        <Form onSubmit={uploadFile}>
          <div>
            <div>Start pierwszej tury:</div>{' '}
            <div>
              <input type="date" min={date} onChange={(e) => setStartFirstDate(e.target.valueAsDate)} />
            </div>
            <div>Koniec pierwszej tury:</div>{' '}
            <div>
              <input type="date" min={date} onChange={(e) => setEndFirstDate(e.target.valueAsDate)} />
            </div>
          </div>
          <div>
            <div>Start drugiej tury:</div>{' '}
            <div>
              <input type="date" min={date} onChange={(e) => setStartSecondDate(e.target.valueAsDate)} />
            </div>
          </div>
          <div>
            <div>Koniec drugiej tury:</div>{' '}
            <div>
              <input type="date" min={date} onChange={(e) => setEndSecondDate(e.target.valueAsDate)} />
            </div>
          </div>
          <div>
            <input
              type="file"
              onChange={(e) => {
                if (e.target.files && e.target.files[0]) {
                  const file = e.target.files[0];
                  setSelectedFile(file);
                }
              }}
            />
          </div>
          <div>
            <SaveButton type="submit">{loading === false ? 'Zapisz' : <SyncLoader />} </SaveButton>
          </div>
        </Form>
      </Wrap>
    </AdministratorWrapper>
  );
};