@@ -64,22 +64,21 @@ function CalendarSingleBooking({
6464 // Get values from client localstorage.
6565 const token = localStorage . getItem ( "apiToken" ) ;
6666 const tenantKey = localStorage . getItem ( "tenantKey" ) ;
67- const apiUrl = localStorage . getItem ( "apiUrl" ) ;
6867
6968 const [ bookableIntervals , setBookableIntervals ] = useState ( [ ] ) ;
7069 const [ fetchingIntervals , setFetchingIntervals ] = useState ( false ) ;
7170 const [ currentTime , setCurrentTime ] = useState ( dayjs ( ) ) ;
7271 const [ bookingResult , setBookingResult ] = useState ( null ) ;
7372 const [ processingBooking , setProcessingBooking ] = useState ( false ) ;
7473 const [ secondsUntilNextEvent , setSecondsUntilNextEvent ] = useState ( null ) ;
75- const [ bookingError , setBookingError ] = useState ( false ) ;
74+ const [ bookingError , setBookingError ] = useState ( null ) ;
7675
7776 const fetchBookingIntervals = ( ) => {
7877 if ( ! instantBookingEnabled ) {
7978 return ;
8079 }
8180
82- if ( ! apiUrl || ! slide || ! token || ! tenantKey ) {
81+ if ( ! slide || ! token || ! tenantKey ) {
8382 setFetchingIntervals ( false ) ;
8483 return ;
8584 }
@@ -93,7 +92,7 @@ function CalendarSingleBooking({
9392 if ( resources . length === 1 ) {
9493 setFetchingIntervals ( true ) ;
9594
96- fetch ( `${ apiUrl } ${ slide [ "@id" ] } /action` , {
95+ fetch ( `${ slide [ "@id" ] } /action` , {
9796 method : "POST" ,
9897 headers : {
9998 authorization : `Bearer ${ token } ` ,
@@ -173,7 +172,7 @@ function CalendarSingleBooking({
173172 } ;
174173
175174 const clickInterval = ( interval ) => {
176- if ( ! apiUrl || ! slide || ! token || ! tenantKey ) {
175+ if ( ! slide || ! token || ! tenantKey ) {
177176 return ;
178177 }
179178
@@ -183,7 +182,7 @@ function CalendarSingleBooking({
183182
184183 setProcessingBooking ( true ) ;
185184
186- fetch ( `${ apiUrl } ${ slide [ "@id" ] } /action` , {
185+ fetch ( `${ slide [ "@id" ] } /action` , {
187186 method : "POST" ,
188187 headers : {
189188 authorization : `Bearer ${ token } ` ,
@@ -198,14 +197,21 @@ function CalendarSingleBooking({
198197 } ,
199198 } ) ,
200199 } )
201- . then ( ( r ) => r . json ( ) )
200+ . then ( ( r ) => {
201+ if ( ! r . ok ) {
202+ const error = new Error ( `Booking failed with status ${ r . status } ` ) ;
203+ error . status = r . status ;
204+ throw error ;
205+ }
206+ return r . json ( ) ;
207+ } )
202208 . then ( ( data ) => {
203209 setBookingResult ( data ) ;
204210 setInstantBookingFromLocalStorage ( slide [ "@id" ] , data ) ;
205211 } )
206- . catch ( ( ) => {
207- setBookingError ( true ) ;
208- setTimeout ( ( ) => setBookingError ( false ) , 10000 ) ;
212+ . catch ( ( err ) => {
213+ setBookingError ( err ?. status === 409 ? "conflict" : "generic" ) ;
214+ setTimeout ( ( ) => setBookingError ( null ) , 10000 ) ;
209215 } )
210216 . finally ( ( ) => {
211217 setProcessingBooking ( false ) ;
@@ -217,7 +223,8 @@ function CalendarSingleBooking({
217223 dayjs . extend ( localizedFormat ) ;
218224
219225 intervalChecking ( ) ;
220- const interval = setInterval ( intervalChecking , 5000 ) ;
226+ // Check every minute if the current time has changed.
227+ const interval = setInterval ( intervalChecking , 60000 ) ;
221228
222229 return ( ) => {
223230 if ( interval !== null ) {
@@ -385,7 +392,15 @@ function CalendarSingleBooking({
385392 />
386393 </ p >
387394 ) }
388- { bookingError && (
395+ { bookingError === "conflict" && (
396+ < p >
397+ < FormattedMessage
398+ id = "instant_booking_conflict"
399+ defaultMessage = "Straksbooking fejlede. Intervallet er optaget."
400+ />
401+ </ p >
402+ ) }
403+ { bookingError && bookingError !== "conflict" && (
389404 < p >
390405 < FormattedMessage
391406 id = "instant_booking_error"
0 commit comments