This repository was archived by the owner on Jan 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnewSystem.tsx
More file actions
356 lines (338 loc) · 11.8 KB
/
Copy pathnewSystem.tsx
File metadata and controls
356 lines (338 loc) · 11.8 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import React, { useState, ChangeEvent } from "react";
import { fireStore, systemIndex } from "../../firebase/firebase";
import { AppState } from "../../store";
import { useSelector } from "react-redux";
import Header from "../components/header";
import Footer from "../../user/components/footer";
import styled from "styled-components";
import setting from "../../designSystem/setting";
import Button from "../../designSystem/Button";
import { Container, MainContents, Wrapper } from "../../designSystem/Page";
import { Redirect, RouteComponentProps, withRouter } from "react-router";
import { System, TargetAge, TargetSex, TargetFamily } from "../../types/type";
const Title = styled.h1`
font-size: ${setting.H1};
margin-bottom: 16px;
`;
const Label = styled.label`
display: block;
`;
const ButtonWrapper = styled.div`
margin-top: 16px;
text-align: right;
`;
const NewSystemForm = styled.div`
width: 100%;
background-color: ${setting.White};
border-radius: 4px;
overflow: hidden;
box-sizing: border-box;
margin-top: 32px;
padding: 32px 16px;
text-align: center;
`;
const InputWrapper = styled.div`
max-width: 460px;
margin: 0 auto;
width: 100%;
text-align: left;
* {
box-sizing: border-box;
}
`;
const StyledInput = styled.input`
width: 100%;
margin-bottom: 16px;
padding: 8px;
border-radius: 2px;
border: solid 2px ${setting.Gray3};
font-size: ${setting.P1};
vertical-align: middle;
`;
const Select = styled.select`
width: 100%;
height: 36px;
margin-bottom: 16px;
padding: 8px;
border-radius: 2px;
border: none;
vertical-align: middle;
background-color: ${setting.Gray5};
`;
const Textarea = styled.textarea`
resize: none;
width: 100%;
padding: 8px;
margin-bottom: 16px;
border: solid 2px ${setting.Gray3};
border-radius: 2px;
`;
type historyProps = RouteComponentProps;
declare type TargetFamilyType = keyof typeof TargetFamily
const Input: React.FC<historyProps> = props => {
const user = useSelector((state: AppState) => state.userState);
const [systemName, setSystemName] = useState<string>("");
const [department, setDepartment] = useState<string>("");
const [location, setLocation] = useState<string>(user.address);
const [site, setSite] = useState<string>("");
const [detail, setDetail] = useState<string>("");
const [target, setTarget] = useState<string>("");
const [method, setMethod] = useState<string>("");
const [selectionCategory, setSelectionCategory] = useState<string[]>([]);
const [targetSex, setTargetSex] = useState<number>(0);
const [selectionTargetFamily, setSelectionTargetFamily] = useState<number[]>(
[]
);
const [targetAge, setTargetAge] = useState<TargetAge>(-1);
const categoryList: Array<string> = [
"子育て",
"介護",
"建築",
"病気",
"融資",
"地域",
"高齢者"
];
const targetFamilyList: Array<string> = [
"独身",
"夫婦",
"子持ち",
"二世帯",
"ひとり親",
"介護",
"不明"
]
const post = () => {
setSelectionCategory(selectionCategory);
const systemData: System = {
Name: systemName,
Location: location,
Department: department,
Target: target,
Site: site,
Detail: detail,
Method: [method],
Category: selectionCategory,
CreatedAt: Date.now(),
UpdatedAt: Date.now(),
isDeleted: false,
ExpireAt: 2262025600000,
documentID: "-1",
totalView: 0,
dailyView: 0,
monthlyView: 0,
weeklyView: [0, 0, 0, 0, 0, 0, 0],
ageGroup: [],
targetFamily: selectionTargetFamily,
targetSex: targetSex,
targetAge: targetAge
};
console.log(systemData);
if (systemName === "" ||
department === "" ||
location === "" ||
site === "" ||
target === "" ||
detail === "" ||
method === "" ||
selectionCategory.length === 0 ||
selectionTargetFamily.length === 0 ||
targetAge === -1) {
alert('必須項目が入力されていません')
} else {
const systemCollection = fireStore.collection(systemIndex);
systemCollection
.add(systemData)
.then(ref => {
console.log("Added document with ID: ", ref.id);
const mode = "no-cors";
const method = "POST";
const body = JSON.stringify(systemData);
const headers = {
Accept: "application/json"
};
fetch(
"https://script.google.com/macros/s/AKfycbz4hzx40TvDLIl4MGARBmECM1Gpp3kjb_LUEafA81O3SQ3oC2Pk/exec",
{ mode, method, headers, body }
)
.then(res => {
console.log(res);
alert("登録が完了しました。");
props.history.push("/admin/");
})
.catch(err => console.error(err));
})
.catch(err => console.error(err));
}
};
const handleCategoryChange = (e: ChangeEvent<HTMLInputElement>) => {
const selectionCategoryArray: string[] = selectionCategory.slice();
const position = selectionCategoryArray.indexOf(e.target.value);
if (position === -1) {
selectionCategoryArray.push(e.target.value);
} else {
selectionCategoryArray.splice(position, 1);
}
setSelectionCategory(selectionCategoryArray);
};
const handleTargetFamilyChange = (e: ChangeEvent<HTMLInputElement>) => {
const selectionTargetFamilyArray: number[] = selectionTargetFamily.slice();
const position = selectionTargetFamilyArray.indexOf(
parseInt(e.target.value)
);
if (position === -1) {
selectionTargetFamilyArray.push(parseInt(e.target.value));
} else {
selectionTargetFamilyArray.splice(position, 1);
}
setSelectionTargetFamily(selectionTargetFamilyArray);
};
if (!user.isAdmin) {
return <Redirect to={"/admin/login"} />;
} else
return (
<Wrapper>
<Header newSystem />
<Container>
<MainContents>
<NewSystemForm>
<Title>新制度登録</Title>
<InputWrapper>
<Label>制度名</Label>
<StyledInput
type="text"
onChange={e => setSystemName(e.target.value)}
placeholder="制度名を入力"
/>
<Label>カテゴリ</Label>
{categoryList.map(categoryName => (
<label key={categoryName}>
<input
key={categoryName}
type="checkbox"
value={categoryName}
checked={selectionCategory.indexOf(categoryName) !== -1}
onChange={e => handleCategoryChange(e)}
/>
{categoryName}
</label>
))}
<Label>おおまかな制度対象者</Label>
<div>
<label>
<input
type="radio"
value={TargetSex.male}
checked={targetSex === TargetSex.male}
onChange={e => setTargetSex(parseInt(e.target.value))}
/>
男性
</label>
<label>
<input
type="radio"
value={TargetSex.female}
checked={targetSex === TargetSex.female}
onChange={e => setTargetSex(parseInt(e.target.value))}
/>
女性
</label>
<label>
<input
type="radio"
value={TargetSex.other}
checked={targetSex === TargetSex.other}
onChange={e => setTargetSex(parseInt(e.target.value))}
/>
すべて
</label>
</div>
<div>
{targetFamilyList.map((targetFamilyName, index) => (
<label key={targetFamilyName}>
<input
key={targetFamilyName}
type="checkbox"
value={TargetFamily[targetFamilyName as TargetFamilyType]}
checked={selectionTargetFamily.indexOf(index) !== -1}
onChange={e => handleTargetFamilyChange(e)}
/>
{targetFamilyName}
</label>
))}
</div>
<Select
onChange={e => {
setTargetAge(parseInt(e.target.value));
}}
>
<option value="-1">対象を選択してください</option>
<option value={TargetAge.乳児}>乳児</option>
<option value={TargetAge.幼児}>幼児</option>
<option value={TargetAge.小学生}>小学生</option>
<option value={TargetAge.小学生以下}>小学生以下</option>
<option value={TargetAge.中学生}>中学生</option>
<option value={TargetAge.小中学生}>小中学生</option>
<option value={TargetAge.中学生以下}>中学生以下</option>
<option value={TargetAge.高校生}>高校生</option>
<option value={TargetAge.高校生以下の就学児童}>
高校生以下の就学児童
</option>
<option value={TargetAge.拾八歳未満}>18歳未満</option>
<option value={TargetAge.拾八歳以下}>18歳以下</option>
<option value={TargetAge.未成年}>未成年</option>
<option value={TargetAge.成人}>成人</option>
<option value={TargetAge.老人}>老人</option>
<option value={TargetAge.全年齢}>全年齢</option>
</Select>
<Label>援助対象者</Label>
<StyledInput
type="text"
onChange={e => setTarget(e.target.value)}
placeholder="例:高校生以下のお子様をお持ちのひとり親家庭の方"
/>
<Label>援助方法</Label>
<StyledInput
type="text"
onChange={e => setMethod(e.target.value)}
placeholder="授業料補助など"
/>
<Label>対象地区</Label>
<StyledInput
type="text"
defaultValue={user.address}
onChange={e => setLocation(e.target.value)}
placeholder="対象地区を入力"
/>
<Label>担当部署</Label>
<StyledInput
type="text"
onChange={e => setDepartment(e.target.value)}
placeholder="担当部署を入力"
/>
<Label>詳細</Label>
<Textarea
rows={5}
placeholder="詳細を入力"
onChange={e => setDetail(e.target.value)}
/>
<Label>公式のページ</Label>
<StyledInput
placeholder="公式ページURLを入力"
type="text"
onChange={e => setSite(e.target.value)}
/>
<ButtonWrapper>
<Button blue onClick={post}>
登録
</Button>
</ButtonWrapper>
</InputWrapper>
</NewSystemForm>
</MainContents>
</Container>
<Footer />
</Wrapper>
);
};
export default withRouter<historyProps, React.FC<historyProps>>(Input);