@@ -34,8 +34,7 @@ const ActionButtons = styled(Dialog.ActionButtons)`
3434 justify-content: end;
3535` ;
3636
37- const parseNumber = ( n : string ) : number => Number ( n . replace ( "," , "." ) ) ;
38- const calcPriceNet = ( price : string , taxPercentageValue ?: number | null ) => {
37+ const calcPriceNet = ( price : number , taxPercentageValue ?: number | null ) => {
3938 const priceNet =
4039 taxPercentageValue != null && taxPercentageValue > 0
4140 ? Number ( price ) / ( ( 1 + taxPercentageValue ) / 100 )
@@ -46,7 +45,7 @@ const calcPriceNet = (price: string, taxPercentageValue?: number | null) => {
4645
4746const DialogContent = ( {
4847 reservation,
49- isFree,
48+ isFree : isReservationUnitFree ,
5049 onClose,
5150 onAccept,
5251} : {
@@ -85,23 +84,28 @@ const DialogContent = ({
8584 const approveReservation = ( input : ReservationApproveMutationInput ) =>
8685 approveReservationMutation ( { variables : { input } } ) ;
8786
88- const [ price , setPrice ] = useState < string > ( String ( reservation . price || 0 ) ) ;
87+ const [ price , setPrice ] = useState ( reservation . price ?? 0 ) ;
8988 const [ handlingDetails , setHandlingDetails ] = useState < string > (
9089 reservation . handlingDetails
9190 ) ;
92- const hasPrice = Boolean ( reservation . price !== undefined ) ;
93- const priceIsValid = ! hasPrice || ! Number . isNaN ( parseNumber ( price ) ) ;
91+ const hasPrice = reservation . price != null && reservation . price > 0 ;
92+ const priceIsValid = ! hasPrice || ! Number . isNaN ( price ) ;
9493
9594 const handleApprove = ( ) => {
9695 const taxP = reservation . taxPercentageValue ?? "0" ;
9796 approveReservation ( {
9897 pk : reservation . pk ,
99- price : parseNumber ( price ) ,
98+ price,
10099 priceNet : calcPriceNet ( price , parseFloat ( taxP ) ) ,
101100 handlingDetails,
102101 } ) ;
103102 } ;
104103
104+ // the reservation has a price and the reservation unit is paid
105+ // might be extrenous checks (the reservation price is what is important here)
106+ // a free reservation should never be allowed to be approved with a price
107+ const shouldDisplayPrice = ! isReservationUnitFree && hasPrice ;
108+
105109 return (
106110 < >
107111 < Dialog . Content >
@@ -121,31 +125,39 @@ const DialogContent = ({
121125 </ Notification >
122126 </ >
123127 ) }
124- { ! isFree && hasPrice ? (
128+ { shouldDisplayPrice && (
125129 < >
126130 < Checkbox
127131 label = { t ( "RequestedReservation.ApproveDialog.clearPrice" ) }
128132 id = "clearPrice"
129- type = "button"
130- checked = { Number ( price ) === 0 }
133+ checked = { price === 0 }
131134 onClick = { ( ) => {
132- if ( Number ( price ) !== 0 ) {
133- setPrice ( "0" ) ;
135+ if ( price !== 0 ) {
136+ setPrice ( 0 ) ;
134137 }
135138 } }
136139 />
137140 < div style = { { width : "14em" } } >
138141 < NumberInput
139142 type = "number"
140- value = { price ? Number ( price ) : "" }
143+ value = { price }
141144 required
142145 min = { 0 }
143146 minusStepButtonAriaLabel = { t ( "common:subtract" ) }
144147 plusStepButtonAriaLabel = { t ( "common:add" ) }
145148 step = { 1 }
146149 id = "approvalPrice"
147150 label = { t ( "RequestedReservation.ApproveDialog.price" ) }
148- onChange = { ( e ) => setPrice ( e . target . value ) }
151+ onChange = { ( e ) => {
152+ if ( e . target . value === "" ) {
153+ setPrice ( 0 ) ;
154+ return ;
155+ }
156+ if ( Number . isNaN ( Number ( e . target . value ) ) ) {
157+ return ;
158+ }
159+ setPrice ( Number ( e . target . value ) ) ;
160+ } }
149161 errorText = {
150162 ! priceIsValid
151163 ? t ( "RequestedReservation.ApproveDialog.missingPrice" )
@@ -154,7 +166,7 @@ const DialogContent = ({
154166 />
155167 </ div >
156168 </ >
157- ) : null }
169+ ) }
158170 < TextArea
159171 value = { handlingDetails }
160172 onChange = { ( e ) => setHandlingDetails ( e . target . value ) }
0 commit comments