1
1
import { useState } from "react" ;
2
2
import styles from "../styles/RhythmEdit.module.css" ;
3
+ import { denominatorTerm } from "../utils/denominator-term" ;
4
+ import { numeratorTerm } from '../utils/numerator-term' ;
3
5
4
- export default function RhythmEdit ( { rhythm, onClose, onUpdate } ) {
6
+ const generateNumeratorSelect = ( selected , onChange ) => {
7
+ const options = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] . map ( ( number ) => {
8
+ return < option key = { number } value = { number } > { numeratorTerm ( number ) } </ option > ;
9
+ } ) ;
10
+
11
+ return < select onChange = { onChange } aria-label = "Rhythm action count" value = { selected } > {
12
+ options
13
+ } </ select > ;
14
+ } ;
15
+
16
+ const generateDenominatorSelect = ( selected , onChange ) => {
17
+ const options = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 ] . map ( ( number ) => {
18
+ return < option key = { number } value = { number } > { denominatorTerm ( number ) } </ option > ;
19
+ } ) ;
20
+
21
+ return < select onChange = { onChange } aria-label = "Rhythm action count time span" value = { selected } >
22
+ { options }
23
+ </ select > ;
24
+ } ;
25
+
26
+ export default function RhythmEdit ( { rhythm, onClose, onSubmit } ) {
5
27
const [ rhythmAction , setRhythmAction ] = useState ( rhythm . action ) ;
6
28
const [ rhythmFrequency , setRhythmFrequency ] = useState ( rhythm . frequency ) ;
29
+ const [ rhythmNumerator , rhythmDenominator ] = rhythmFrequency ;
7
30
const [ rhythmReason , setRhythmReason ] = useState ( rhythm . reason ) ;
8
31
32
+ const submitHandler = ( e ) => {
33
+ e . preventDefault ( ) ;
34
+
35
+ onSubmit ( {
36
+ action : rhythmAction ,
37
+ reason : rhythmReason ,
38
+ frequency : rhythmFrequency ,
39
+ } ) ;
40
+ } ;
41
+
9
42
return (
10
- < div className = { styles [ "rhythm-edit" ] } >
11
- < button onClick = { ( ) => onClose ( ) } className = { styles . close } >
43
+ < form className = { styles [ "rhythm-edit" ] } onSubmit = { submitHandler } >
44
+ < button type = "button" onClick = { ( ) => onClose ( ) } className = { styles . close } >
12
45
Close
13
46
</ button >
14
47
< div className = { styles . action } >
@@ -21,25 +54,13 @@ export default function RhythmEdit({ rhythm, onClose, onUpdate }) {
21
54
/>
22
55
</ div >
23
56
< div className = { styles . frequency } >
24
- < select aria-label = "Rhythm action count" >
25
- < option > once</ option >
26
- < option > twice</ option >
27
- < option > thrice</ option >
28
- < option > four times</ option >
29
- < option > five times</ option >
30
- < option > six times</ option >
31
- < option > seven times</ option >
32
- </ select >
57
+ { generateNumeratorSelect ( rhythmNumerator , ( e ) =>
58
+ setRhythmFrequency ( [ Number ( e . target . value ) , rhythmDenominator ] )
59
+ ) }
33
60
< span className = { styles . every } > every</ span >
34
- < select aria-label = "Rhythm action count time span" >
35
- < option > day</ option >
36
- < option > two days</ option >
37
- < option > three days</ option >
38
- < option > four days</ option >
39
- < option > five days</ option >
40
- < option > six days</ option >
41
- < option > seven days</ option >
42
- </ select >
61
+ { generateDenominatorSelect ( rhythmDenominator , ( e ) =>
62
+ setRhythmFrequency ( [ rhythmNumerator , Number ( e . target . value ) ] )
63
+ ) }
43
64
</ div >
44
65
< div className = "styles.reason" >
45
66
< div className = { styles . action } >
@@ -52,7 +73,7 @@ export default function RhythmEdit({ rhythm, onClose, onUpdate }) {
52
73
/>
53
74
</ div >
54
75
</ div >
55
- < button onClick = { ( ) => onUpdate ( { action : rhythmAction , reason : rhythmReason , frequency : rhythm . frequency } ) } > Update Rhythm</ button >
56
- </ div >
76
+ < button type = "submit" > Update Rhythm</ button >
77
+ </ form >
57
78
) ;
58
79
}
0 commit comments