Passed
Push — master ( b327de...e0a125 )
by Guangyu
19:12 queued 11s
created

web/src/components/calendar/AddScheduleModal.js   A

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 165
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 143
mnd 3
bc 3
fnc 0
dl 0
loc 165
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React, { useState, useEffect } from 'react';
2
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
3
import {
4
  Button,
5
  Modal,
6
  ModalHeader,
7
  ModalBody,
8
  ModalFooter,
9
  Form,
10
  FormGroup,
11
  Label,
12
  Input,
13
  CustomInput
14
} from 'reactstrap';
15
import Datetime from 'react-datetime';
16
import { v4 as uuid } from 'uuid';
17
import Flex from '../common/Flex';
18
19
const AddScheduleModal = ({
20
  setIsOpenScheduleModal,
21
  isOpenScheduleModal,
22
  setInitialEvents,
23
  initialEvents,
24
  addScheduleStartDate,
25
  setAddScheduleStartDate
26
}) => {
27
  const toggle = () => setIsOpenScheduleModal(!isOpenScheduleModal);
28
29
  const [formObj, setFormObj] = useState({ id: uuid() });
30
  const [endDate, setEndDate] = useState();
31
  const [startDate, setStartDate] = useState();
32
  const closeBtn = (
33
    <button className="close font-weight-normal" onClick={toggle}>
34
      &times;
35
    </button>
36
  );
37
38
  const handleChange = target => {
39
    let name = target.name;
40
    let value = name === 'allDay' ? target.checked : target.value;
41
    setFormObj({ ...formObj, [name]: value });
42
  };
43
44
  useEffect(() => {
45
    !isOpenScheduleModal && setAddScheduleStartDate(null);
46
    !isOpenScheduleModal && setEndDate(null);
47
    !isOpenScheduleModal && setStartDate(null);
48
    setFormObj({ ...formObj, start: addScheduleStartDate });
49
    // eslint-disable-next-line
50
  }, [isOpenScheduleModal, addScheduleStartDate]);
51
52
  return (
53
    <Modal isOpen={isOpenScheduleModal} toggle={toggle} modalClassName="theme-modal" contentClassName="border">
54
      <Form
55
        onSubmit={e => {
56
          e.preventDefault();
57
          setIsOpenScheduleModal(false);
58
          setInitialEvents([...initialEvents, formObj]);
59
        }}
60
      >
61
        <ModalHeader toggle={toggle} className="bg-light d-flex flex-between-center border-bottom-0" close={closeBtn}>
62
          Create Schedule
63
        </ModalHeader>
64
        <ModalBody>
65
          <FormGroup>
66
            <Label className="fs-0" for="eventTitle">
67
              Title
68
            </Label>
69
            <Input name="title" id="eventTitle" required onChange={({ target }) => handleChange(target)} />
70
          </FormGroup>
71
          <FormGroup>
72
            <Label className="fs-0" for="eventStart">
73
              Start Date
74
            </Label>
75
            <Datetime
76
              timeFormat={true}
77
              value={startDate || addScheduleStartDate}
78
              onChange={dateTime => {
79
                if (dateTime._isValid) {
80
                  setStartDate(dateTime);
81
                  let date = {};
82
                  date.value = dateTime.format();
83
                  date.name = 'start';
84
                  handleChange(date);
85
                }
86
              }}
87
              dateFormat="MM-DD-YYYY"
88
              inputProps={{ placeholder: 'MM-DD-YYYY H:M', id: 'eventStart' }}
89
            />
90
          </FormGroup>
91
          <FormGroup>
92
            <Label className="fs-0" for="eventEnd">
93
              End Date
94
            </Label>
95
            <Datetime
96
              value={endDate}
97
              timeFormat={true}
98
              onChange={dateTime => {
99
                if (dateTime._isValid) {
100
                  setEndDate(dateTime);
101
                  let date = {};
102
                  date.value = dateTime.format();
103
                  date.name = 'end';
104
                  handleChange(date);
105
                }
106
              }}
107
              dateFormat="MM-DD-YYYY"
108
              inputProps={{ placeholder: 'MM-DD-YYYY H:M', id: 'eventEnd' }}
109
            />
110
          </FormGroup>
111
          <FormGroup>
112
            <CustomInput
113
              name="allDay"
114
              type="checkbox"
115
              id="allDay"
116
              label="All Day"
117
              onChange={({ target }) => handleChange(target)}
118
            />
119
          </FormGroup>
120
          <FormGroup>
121
            <Label className="fs-0">Schedule Meeting</Label>
122
            <div>
123
              <Button color="" type="button" className="text-left bg-soft-info text-info">
124
                <FontAwesomeIcon icon="video" className="mr-2" />
125
                <span>Add video conference link</span>
126
              </Button>
127
            </div>
128
          </FormGroup>
129
          <FormGroup>
130
            <Label className="fs-0" for="eventDescription">
131
              Description
132
            </Label>
133
            <Input
134
              type="textarea"
135
              name="description"
136
              id="eventDescription"
137
              onChange={({ target }) => handleChange(target)}
138
            />
139
          </FormGroup>
140
          <FormGroup>
141
            <Label for="eventLabel">Label</Label>
142
            <CustomInput type="select" id="eventLabel" name="className" onChange={({ target }) => handleChange(target)}>
143
              <option>None</option>
144
              <option value="bg-soft-info">Business</option>
145
              <option value="bg-soft-danger">Important</option>
146
              <option value="bg-soft-warning">Personal</option>
147
              <option value="bg-soft-success">Must Attend</option>
148
            </CustomInput>
149
          </FormGroup>
150
        </ModalBody>
151
        <ModalFooter tag={Flex} justify="end" align="center" className="bg-light border-top-0">
152
          <Button color="link" tag="a" href="pages/event-create" className="text-600">
153
            More Option
154
          </Button>
155
          <Button color="primary" type="submit" className="px-4">
156
            Save
157
          </Button>
158
        </ModalFooter>
159
      </Form>
160
    </Modal>
161
  );
162
};
163
164
export default AddScheduleModal;
165