@@ -6,11 +6,11 @@ import { useEffect } from "react";
6
6
import { CarerSelect } from "../../CarerSelect" ;
7
7
import { LanguageSelect } from "../../LanguageSelect" ;
8
8
9
- type FormValues = {
10
- carerId ?: number ;
11
- languageEntries : {
9
+ export type EntryFormValues = {
10
+ carer ?: number ;
11
+ entryLanguages : {
12
12
ratio : number ;
13
- languageId ?: number ;
13
+ language ?: number ;
14
14
} [ ] ;
15
15
startedAt : string ;
16
16
endedAt : string ;
@@ -26,18 +26,18 @@ const messages = i18n("entityForm", {
26
26
} ) ;
27
27
28
28
type EntityFormProps = {
29
- onSave : ( entity : FormValues ) => void ;
30
- entry ?: Partial < FormValues > ;
29
+ onSave : ( entity : EntryFormValues ) => void ;
30
+ entry ?: Partial < EntryFormValues > ;
31
31
actionLabel : string ;
32
32
} ;
33
33
34
34
export function EntityForm ( { onSave, actionLabel, entry } : EntityFormProps ) {
35
35
const t = useStore ( messages ) ;
36
- const f = useForm < FormValues > ( {
36
+ const f = useForm < EntryFormValues > ( {
37
37
initialValues : {
38
38
startedAt : "" ,
39
39
endedAt : "" ,
40
- languageEntries : [
40
+ entryLanguages : [
41
41
{
42
42
ratio : 100 ,
43
43
} ,
@@ -46,16 +46,16 @@ export function EntityForm({ onSave, actionLabel, entry }: EntityFormProps) {
46
46
validate : {
47
47
startedAt : isNotEmpty ( t . validationNotEmpty ) ,
48
48
endedAt : isNotEmpty ( t . validationNotEmpty ) ,
49
- carerId : isNotEmpty ( t . validationNotEmpty ) ,
50
- languageEntries : {
49
+ carer : isNotEmpty ( t . validationNotEmpty ) ,
50
+ entryLanguages : {
51
51
ratio : ( value ) => {
52
52
const fieldError = isInRange ( { min : 1 , max : 100 } , t . validationRatio ) ( value ) ;
53
53
if ( fieldError ) return fieldError ;
54
54
55
55
const listError = getTotalRatio ( ) !== 100 ;
56
56
if ( listError ) return t . validationTotalRatio ;
57
57
} ,
58
- languageId : isNotEmpty ( t . validationNotEmpty ) ,
58
+ language : isNotEmpty ( t . validationNotEmpty ) ,
59
59
} ,
60
60
} ,
61
61
} ) ;
@@ -67,16 +67,16 @@ export function EntityForm({ onSave, actionLabel, entry }: EntityFormProps) {
67
67
}
68
68
} , [ entry ] ) ;
69
69
70
- const getTotalRatio = ( ) => f . getValues ( ) . languageEntries . reduce ( ( acc , cur ) => ( acc += cur . ratio ) , 0 ) ;
70
+ const getTotalRatio = ( ) => f . getValues ( ) . entryLanguages . reduce ( ( acc , cur ) => ( acc += cur . ratio ) , 0 ) ;
71
71
72
72
const updateRatios = ( ) => {
73
- const currentEntries = f . getValues ( ) . languageEntries ;
73
+ const currentEntries = f . getValues ( ) . entryLanguages ;
74
74
const lastEntryIndex = currentEntries . length - 1 ;
75
75
76
76
const avarageRatio = Math . round ( 100 / currentEntries . length ) ;
77
77
78
78
f . setValues ( {
79
- languageEntries : currentEntries . map ( ( entry , index ) => ( {
79
+ entryLanguages : currentEntries . map ( ( entry , index ) => ( {
80
80
...entry ,
81
81
ratio : index === lastEntryIndex ? 100 - lastEntryIndex * avarageRatio : avarageRatio ,
82
82
} ) ) ,
@@ -88,7 +88,7 @@ export function EntityForm({ onSave, actionLabel, entry }: EntityFormProps) {
88
88
< Stack >
89
89
< CarerSelect { ...f . getInputProps ( "carerId" ) } placeholder = { t . labelCarer } />
90
90
91
- { f . getValues ( ) . languageEntries . map ( ( _ , index ) => (
91
+ { f . getValues ( ) . entryLanguages . map ( ( _ , index ) => (
92
92
// TODO: make key either languageId or name of new language entry
93
93
< Group key = { `entry-${ index } ` } justify = "stretch" >
94
94
< NumberInput { ...f . getInputProps ( `languageEntries.${ index } .ratio` ) } max = { 100 } min = { 1 } w = { 80 } rightSection = "%" />
0 commit comments