1- import React from 'react' ;
1+ import React , { useState } from 'react' ;
22import { Form , validate , FieldProps , renderFields } from '@tpr/forms' ;
3+ import { Link , Flex } from '@tpr/core' ;
34import { useInsurerContext } from '../../context' ;
45import { Footer } from '../../../components/card' ;
56import { Content } from '../../../components/content' ;
@@ -15,18 +16,37 @@ const getFields = (
1516 name : 'insurerCompanyReference' ,
1617 label : fields . insurerCompanyReference . label ,
1718 inputWidth : 5 ,
18- error : fields . insurerCompanyReference . error ,
19+ error : ( value : string ) => {
20+ if ( ! value ) {
21+ return fields . insurerCompanyReference . errorIfEmpty ;
22+ } else if ( value . length < 1 ) {
23+ return fields . insurerCompanyReference . errorIfTooShort ;
24+ } else if ( value . length > 100 ) {
25+ return fields . insurerCompanyReference . errorIfTooLong ;
26+ }
27+ return undefined ;
28+ } ,
1929 cfg : { mb : 3 } ,
2030 } ,
2131] ;
2232
2333export const Reference : React . FC = ( ) => {
24- const { current, send, i18n } = useInsurerContext ( ) ;
34+ const [ loading , setLoading ] = useState ( false ) ;
35+ const { current, send, i18n, onSaveRef } = useInsurerContext ( ) ;
2536 const { insurer } = current . context ;
2637 const fields = getFields ( i18n ?. reference ?. fields ) ;
2738
28- const onSubmit = ( values ) => {
29- send ( 'SAVE' , { values } ) ;
39+ const onSubmit = async ( values ) => {
40+ setLoading ( true ) ;
41+ try {
42+ await onSaveRef ( values , insurer ) ;
43+ send ( 'SAVE' , { values } ) ;
44+ setLoading ( false ) ;
45+ } catch ( error ) {
46+ // todo: maybe an error message from the server?
47+ console . log ( error ) ;
48+ setLoading ( false ) ;
49+ }
3050 } ;
3151
3252 return (
@@ -47,14 +67,25 @@ export const Reference: React.FC = () => {
4767 < form onSubmit = { handleSubmit } >
4868 { renderFields ( fields ) }
4969 < Footer >
50- < ArrowButton
51- intent = "special"
52- pointsTo = "up"
53- iconSide = "right"
54- type = "submit"
55- title = "Save and close"
56- disabled = { false }
57- />
70+ < Flex >
71+ < ArrowButton
72+ intent = "special"
73+ pointsTo = "up"
74+ iconSide = "right"
75+ type = "submit"
76+ title = "Save and close"
77+ disabled = { loading }
78+ />
79+
80+ < Link
81+ cfg = { { m : 3 } }
82+ disabled = { loading }
83+ underline
84+ onClick = { ( ) => send ( 'CANCEL' ) }
85+ >
86+ Cancel
87+ </ Link >
88+ </ Flex >
5889 </ Footer >
5990 </ form >
6091 ) }
0 commit comments