ModalControl.js
4.67 KB
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
import React, { useState } from "react";
import { makeStyles } from "@material-ui/core/styles";
import Modal from "@material-ui/core/Modal";
import IconButton from "@material-ui/core/IconButton";
import TextField from "@material-ui/core/TextField";
import Button from "@material-ui/core/Button";
import Typography from "@material-ui/core/Typography";
import AddIcon from "@material-ui/icons/Add";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import Checkbox from "@material-ui/core/Checkbox";
const useStyles = makeStyles((theme) => ({
iconButton: {
margin: "0",
padding: "0",
position: "fixed",
bottom: "3rem",
right: "3rem",
},
icon: {
fontSize: 70,
color: "black",
},
addButton: {
fontSize: 11,
float: "right",
margin: 0,
marginTop: "1rem",
marginRight: "1rem",
},
buttonGroup: {
clear: "both",
"& > *": {
margin: theme.spacing(1),
float: "right",
},
},
title: {
float: "left",
width: "13rem",
marginLeft: "0.5rem",
marginTop: "0.3rem",
padding: 0,
},
isPublic: {
float: "left",
},
paper: {
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
position: "absolute",
width: "90%",
maxWidth: 320,
maxHeight: 500,
msOverflowStyle: "none",
backgroundColor: "white",
boxShadow: theme.shadows[5],
padding: theme.spacing(2, 4, 2),
},
input: {
"& > *": {
float: "left",
margin: theme.spacing(1),
marginRight: 0,
width: "14.5rem",
},
},
}));
export default function AddButton({ handleClose }) {
const classes = useStyles();
const [title, setTitle] = useState("");
const [date, setDate] = useState(new Date());
const [isPublic, setIsPublic] = useState(1);
const [textList, setTextList] = useState([]);
const [textFieldBody, setTextFieldBody] = useState([
<TextField required label="To do 1" onChange={(e) => handleText(e, 0)} />,
]);
const addApi = (data) => {
return fetch("/api/addcard", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
}).then((response) => response.json());
};
const checkAndClose = () => {
if (title === "") {
alert("Please enter a title!");
} else if (checkEemptyList(textList)) {
alert("Please fill in the blank!");
} else {
const initCK = Array.apply(null, Array(textList.length)).map(Number.prototype.valueOf,0);
addApi({
isPublic: isPublic,
name: localStorage["userName"],
date: date.toLocaleDateString(),
time: date.toLocaleTimeString(),
title: title,
todo: textList.join(","),
ck: initCK.join(","),
});
handleClose();
}
};
const checkEemptyList = (arr) => {
if (arr.length === 0) {
return 1;
}
for (let idx = 0; idx < arr.length; idx++) {
if (arr[idx] === undefined || arr[idx] === "") {
return 1;
}
}
return 0;
};
const handleTitle = (e) => {
setTitle(e.target.value);
};
const handlePublic = () => {
setIsPublic(isPublic ? 0 : 1);
};
const handleText = (e, idx) => {
let tempArr = textList;
tempArr[idx] = e.target.value;
setTextList(tempArr);
};
const handleAdd = () => {
if (textFieldBody.length < 5) {
const idx = textFieldBody.length;
setTextFieldBody([
...textFieldBody,
<TextField
required
label={"To do " + (idx + 1)}
onChange={(e) => handleText(e, idx)}
/>,
]);
} else {
alert("You can register up to five.");
}
};
return (
<>
<Modal open={true}>
<div className={classes.paper}>
<Typography className={classes.title} variant="h5">
ADD TODO LIST
</Typography>
<FormControlLabel
className={classes.isPublic}
control={<Checkbox onClick={handlePublic} />}
checked={isPublic}
label="Public"
/>
<form className={classes.input} noValidate autoComplete="off">
<TextField required label="Title" onChange={handleTitle} />
{textFieldBody.map((field) => field)}
</form>
<IconButton className={classes.addButton} onClick={handleAdd}>
<AddIcon />
</IconButton>
<form className={classes.buttonGroup}>
<Button variant="contained" color="primary" onClick={checkAndClose}>
확인
</Button>
<Button variant="contained" color="secondary" onClick={handleClose}>
취소
</Button>
</form>
</div>
</Modal>
</>
);
}