質問編集履歴

2

最後に全コードを貼り付けてみました

2021/02/23 08:03

投稿

moyong
moyong

スコア19

test CHANGED
File without changes
test CHANGED
@@ -101,3 +101,445 @@
101
101
  みたいな構造のtrainingRecordsが、Twitterでいう一つのつぶやきみたいになるようなイメージです。
102
102
 
103
103
  わざわざこういうデータ構造にしているのは、1日のトレーニングの中で複数のトレーニングを色々な重量で複数セットやる場合を考慮しているためです。
104
+
105
+
106
+
107
+ 念の為、このコンポーネントを全て貼り付けておきます。
108
+
109
+ ```ここに言語を入力
110
+
111
+ import React, { useState } from "react";
112
+
113
+ import styles from "./PostInput.module.scss";
114
+
115
+ import { storage, db, auth } from "../firebase";
116
+
117
+ import firebase from "firebase/app";
118
+
119
+ import { useSelector } from "react-redux";
120
+
121
+ import { selectUser } from "../features/userSlice";
122
+
123
+ import AddPhotoAlternateIcon from '@material-ui/icons/AddPhotoAlternate';
124
+
125
+ import AddCircleIcon from '@material-ui/icons/AddCircle';
126
+
127
+ import {
128
+
129
+ Avatar,
130
+
131
+ Button,
132
+
133
+ IconButton,
134
+
135
+ TextField,
136
+
137
+ MenuItem,
138
+
139
+ createStyles,
140
+
141
+ makeStyles,
142
+
143
+ Theme,
144
+
145
+ } from "@material-ui/core";
146
+
147
+
148
+
149
+ interface trainingRecords {
150
+
151
+ trainingName: string;
152
+
153
+ trainingWeight: string;
154
+
155
+ trainingReps: string;
156
+
157
+ }
158
+
159
+ let trainingRecords = [{}];
160
+
161
+ const weightList = [
162
+
163
+ {value: 'none', label: 'none'},
164
+
165
+ {value: '10', label: '10lbs | 4.5kg'},
166
+
167
+ {value: '20', label: '20lbs | 9kg'},
168
+
169
+ {value: '30', label: '30lbs | 14kg'},
170
+
171
+ {value: '40', label: '40lbs | 18kg'},
172
+
173
+ {value: '50', label: '50lbs | 23kg'},
174
+
175
+ {value: '60', label: '60lbs | 27kg'},
176
+
177
+ {value: '70', label: '70lbs | 32kg'},
178
+
179
+ {value: '80', label: '80lbs | 36kg'},
180
+
181
+ {value: '90', label: '90lbs | 41kg'},
182
+
183
+ {value: '100', label: '100lbs | 45kg'},
184
+
185
+ {value: '110', label: '110lbs | 50kg'},
186
+
187
+ {value: '120', label: '120lbs | 54kg'},
188
+
189
+ {value: '130', label: '130lbs | 59kg'},
190
+
191
+ {value: '140', label: '140lbs | 64kg'},
192
+
193
+ {value: '150', label: '150lbs | 68kg'},
194
+
195
+ {value: '160', label: '160lbs | 73kg'},
196
+
197
+ {value: '170', label: '170lbs | 77kg'},
198
+
199
+ {value: '180', label: '180lbs | 82kg'},
200
+
201
+ {value: '190', label: '190lbs | 86kg'},
202
+
203
+ {value: '200', label: '200lbs | 91kg'},
204
+
205
+ ];
206
+
207
+
208
+
209
+ // const useStyles = makeStyles((theme: Theme) =>
210
+
211
+ // createStyles({
212
+
213
+ // trainingNameInput: {
214
+
215
+ // '& > *': {
216
+
217
+ // margin: theme.spacing(1),
218
+
219
+ // width: '25ch',
220
+
221
+ // },
222
+
223
+ // },
224
+
225
+ // trainingWeightSelect: {
226
+
227
+ // '& .MuiTextField-root': {
228
+
229
+ // margin: theme.spacing(1),
230
+
231
+ // width: '25ch',
232
+
233
+ // },
234
+
235
+ // },
236
+
237
+ // }),
238
+
239
+ // );
240
+
241
+ const TweetInput: React.FC = () => {
242
+
243
+ const user = useSelector(selectUser);
244
+
245
+ // const classes = useStyles();
246
+
247
+ const [ image, setImage] = useState<File | null>(null);
248
+
249
+ const [ trainingRecord, setTrainingRecord ] = useState({
250
+
251
+ trainingName: "",
252
+
253
+ trainingWeight: "none",
254
+
255
+ trainingReps: "0",
256
+
257
+ })
258
+
259
+ const onChangeImageHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
260
+
261
+ if (e.target.files![0]) {
262
+
263
+ setImage(e.target.files![0]);
264
+
265
+ e.target.value = "";
266
+
267
+ }
268
+
269
+ };
270
+
271
+ const saveTrainingRecord = () => {
272
+
273
+ if (!trainingRecords.length) {
274
+
275
+ trainingRecords.push(trainingRecords);
276
+
277
+ trainingRecords.shift();
278
+
279
+ } else {
280
+
281
+ trainingRecords.push(trainingRecord);
282
+
283
+ }
284
+
285
+ setTrainingRecord({
286
+
287
+ trainingName: "",
288
+
289
+ trainingWeight: "none",
290
+
291
+ trainingReps: "0",
292
+
293
+ });
294
+
295
+ console.log(trainingRecords);
296
+
297
+ };
298
+
299
+ // const sendTrainingPost = (e: React.FormEvent<HTMLFormElement>) => {
300
+
301
+ // e.preventDefault();
302
+
303
+ // if (image) {
304
+
305
+ // const S =
306
+
307
+ // "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
308
+
309
+ // const N = 16;
310
+
311
+ // const randomChar = Array.from(crypto.getRandomValues(new Uint32Array(N)))
312
+
313
+ // .map((n) => S[n % S.length])
314
+
315
+ // .join("");
316
+
317
+ // const fileName = randomChar + "_" + image.name;
318
+
319
+ // const uploadImg = storage.ref(`images/${fileName}`).put(image);
320
+
321
+ // uploadImg.on(
322
+
323
+ // firebase.storage.TaskEvent.STATE_CHANGED,
324
+
325
+ // () => {},
326
+
327
+ // (err) => {
328
+
329
+ // alert(err.message);
330
+
331
+ // },
332
+
333
+ // async () => {
334
+
335
+ // await storage
336
+
337
+ // .ref("images")
338
+
339
+ // .child(fileName)
340
+
341
+ // .getDownloadURL()
342
+
343
+ // .then(async (url) => {
344
+
345
+ // await db.collection('training_posts').add({
346
+
347
+ // avatar: user.photoUrl,
348
+
349
+ // image: url,
350
+
351
+ // training_name: trainingName,
352
+
353
+ // training_weight: trainingWeight,
354
+
355
+ // training_reps: trainingReps,
356
+
357
+ // timestamp: firebase.firestore.FieldValue.serverTimestamp(),
358
+
359
+ // username: user.displayName,
360
+
361
+ // uid: user.uid
362
+
363
+ // });
364
+
365
+ // });
366
+
367
+ // }
368
+
369
+ // );
370
+
371
+ // } else {
372
+
373
+ // db.collection('training_posts').add({
374
+
375
+ // avatar: user.photoUrl,
376
+
377
+ // image: "",
378
+
379
+ // training_name: trainingName,
380
+
381
+ // training_weight: trainingWeight,
382
+
383
+ // training_reps: trainingReps,
384
+
385
+ // timestamp: firebase.firestore.FieldValue.serverTimestamp(),
386
+
387
+ // username: user.displayName,
388
+
389
+ // uid: user.uid
390
+
391
+ // });
392
+
393
+ // }
394
+
395
+ // setImage(null);
396
+
397
+ // setTrainingName("");
398
+
399
+ // setTrainingWeight("none");
400
+
401
+ // setTrainingReps("0");
402
+
403
+ // };
404
+
405
+ return (
406
+
407
+ <>
408
+
409
+ <form>
410
+
411
+ <div className={styles.tweet_form}>
412
+
413
+ <Avatar
414
+
415
+ className={styles.tweet_avatar}
416
+
417
+ src={user.photoUrl}
418
+
419
+ />
420
+
421
+ <input
422
+
423
+ className={styles.trainingName}
424
+
425
+ placeholder="What kind of training?"
426
+
427
+ type="text"
428
+
429
+ value={trainingRecord.trainingName}
430
+
431
+ onChange={(e) => setTrainingRecord({...trainingRecord, trainingName: e.target.value})}
432
+
433
+ />
434
+
435
+ <select
436
+
437
+ className={styles.trainingWeight}
438
+
439
+ value={trainingRecord.trainingWeight}
440
+
441
+ onChange={(e) => setTrainingRecord({...trainingRecord, trainingWeight: e.target.value})}
442
+
443
+ >
444
+
445
+ {weightList.map((weight) => (
446
+
447
+ <option key={weight.value} value={weight.value}>
448
+
449
+ {weight.label}
450
+
451
+ </option>
452
+
453
+ ))}
454
+
455
+ </select>
456
+
457
+ <input
458
+
459
+ min="0"
460
+
461
+ className={styles.trainingReps}
462
+
463
+ placeholder="reps"
464
+
465
+ type="number"
466
+
467
+ value={trainingRecord.trainingReps}
468
+
469
+ onChange={(e) => setTrainingRecord({...trainingRecord, trainingReps: e.target.value})}
470
+
471
+ />
472
+
473
+ <AddCircleIcon
474
+
475
+ className={styles.saveTrainingRecord}
476
+
477
+ onClick={() => saveTrainingRecord()}
478
+
479
+ />
480
+
481
+ <IconButton>
482
+
483
+ <label>
484
+
485
+ <AddPhotoAlternateIcon
486
+
487
+ className={
488
+
489
+ image ? styles.tweet_addIconLoaded : styles.tweet_addIcon
490
+
491
+ }
492
+
493
+ />
494
+
495
+ <input
496
+
497
+ className={styles.tweet_hiddenIcon}
498
+
499
+ type="file"
500
+
501
+ onChange={onChangeImageHandler}
502
+
503
+ />
504
+
505
+ </label>
506
+
507
+ </IconButton>
508
+
509
+ </div>
510
+
511
+ {/* <Button
512
+
513
+ type="submit"
514
+
515
+ disabled={!trainingName}
516
+
517
+ className={
518
+
519
+ trainingName ? styles.tweet_sendBtn : styles.tweet_sendDisableBtn
520
+
521
+ }
522
+
523
+ >
524
+
525
+ Tweet
526
+
527
+ </Button> */}
528
+
529
+ </form>
530
+
531
+
532
+
533
+ </>
534
+
535
+ );
536
+
537
+ }
538
+
539
+
540
+
541
+ export default TweetInput;
542
+
543
+
544
+
545
+ ```

1

言葉を変えて分かりやすくしました

2021/02/23 08:03

投稿

moyong
moyong

スコア19

test CHANGED
File without changes
test CHANGED
@@ -54,7 +54,7 @@
54
54
 
55
55
  ![イメージ説明](a67a81bc06505b770a5183ae9a9b9785.png)
56
56
 
57
- 上の画像のコンソールにあるように、先頭オブジェクトが削除できていません。
57
+ 上の画像のコンソールにあるように、0番目{}という不要なものが削除できていません。
58
58
 
59
59
  (次々とオブジェクトを追加していくことはできています。)
60
60