@@ -32,7 +32,6 @@ import {
3232 ReservationUnitsReservationUnitReservationStartIntervalChoices ,
3333 ReservationUnitImageCreateMutationInput ,
3434 ReservationUnitsReservationUnitAuthenticationChoices ,
35- ReservationUnitByPkType ,
3635 UnitByPkType ,
3736 ReservationState ,
3837 ReservationUnitState ,
@@ -251,21 +250,21 @@ const ReservationUnitEditor = (): JSX.Element | null => {
251250 createReservationUnitMutation ( { variables : { input } } ) ;
252251
253252 const createOrUpdateReservationUnit = async (
254- publish : boolean ,
255- archive = false
253+ publish ? : boolean ,
254+ archive ?: boolean
256255 ) : Promise < number | undefined > => {
257256 const input = pick (
258257 {
259258 ...omitBy ( state . reservationUnitEdit , ( v ) => v === "" ) ,
260259 surfaceArea : Number ( state . reservationUnitEdit ?. surfaceArea ) ,
261- isDraft : ! publish ,
260+ ... ( publish != null ? { isDraft : ! publish } : { } ) ,
262261 reservationStartInterval :
263262 state . reservationUnitEdit ?. reservationStartInterval ?. toLocaleLowerCase ( ) , /// due to api inconsistency
264263 maxReservationsPerUser : state . reservationUnitEdit
265264 ?. maxReservationsPerUser
266265 ? Number ( state . reservationUnitEdit ?. maxReservationsPerUser )
267266 : null ,
268- isArchived : archive ,
267+ ... ( archive != null ? { isArchived : archive } : { } ) ,
269268 pricings : state . reservationUnitEdit . pricings ?. map ( ( pricing ) =>
270269 omitBy ( pricing , isNull )
271270 ) ,
@@ -510,10 +509,10 @@ const ReservationUnitEditor = (): JSX.Element | null => {
510509 return true ;
511510 } ;
512511
513- const saveReservationUnit = async ( publish : boolean ) => {
512+ const publishReservationUnit = async ( publish : boolean ) => {
514513 setSaving ( true ) ;
515514 try {
516- const resUnitPk = await createOrUpdateReservationUnit ( publish ) ;
515+ const resUnitPk = await createOrUpdateReservationUnit ( publish , false ) ;
517516 if ( resUnitPk ) {
518517 // res unit is saved, we can save changes to images
519518 const success = await reconcileImageChanges ( resUnitPk ) ;
@@ -623,17 +622,16 @@ const ReservationUnitEditor = (): JSX.Element | null => {
623622 ) ; // default is 20 if no spaces selected
624623
625624 const onlyForDirect =
626- [ "DIRECT_AND_SEASON" , "DIRECT" ] . includes (
627- state . reservationUnitEdit . reservationKind as string
628- ) || false ;
625+ state . reservationUnitEdit . reservationKind === "DIRECT" ||
626+ state . reservationUnitEdit . reservationKind === "DIRECT_AND_SEASON" ;
629627
630628 const handleSaveAsDraft = ( ) => {
631629 const validationErrors = draftSchema . validate ( state . reservationUnitEdit ) ;
632630
633631 if ( validationErrors . error ) {
634632 dispatch ( { type : "setValidationErrors" , validationErrors } ) ;
635633 } else {
636- saveReservationUnit ( false ) ;
634+ publishReservationUnit ( false ) ;
637635 dispatch ( {
638636 type : "setValidationErrors" ,
639637 validationErrors : null ,
@@ -647,14 +645,41 @@ const ReservationUnitEditor = (): JSX.Element | null => {
647645 if ( validationErrors . error ) {
648646 dispatch ( { type : "setValidationErrors" , validationErrors } ) ;
649647 } else {
650- saveReservationUnit ( true ) ;
648+ publishReservationUnit ( true ) ;
651649 dispatch ( {
652650 type : "setValidationErrors" ,
653651 validationErrors : null ,
654652 } ) ;
655653 }
656654 } ;
657655
656+ const handleAcceptArchive = async ( ) => {
657+ try {
658+ const r = await createOrUpdateReservationUnit ( undefined , true ) ;
659+
660+ if ( r ) {
661+ setModalContent ( null ) ;
662+ notifySuccess ( t ( "ArchiveReservationUnitDialog.success" ) ) ;
663+ history ( - 1 ) ;
664+ }
665+ } catch ( e ) {
666+ // noop
667+ }
668+ } ;
669+
670+ const handleArchiveButtonClick = async ( ) => {
671+ if ( state . reservationUnit ) {
672+ setModalContent (
673+ < ArchiveDialog
674+ reservationUnit = { state . reservationUnit }
675+ onAccept = { handleAcceptArchive }
676+ onClose = { ( ) => setModalContent ( null ) }
677+ /> ,
678+ true
679+ ) ;
680+ }
681+ } ;
682+
658683 return (
659684 < Wrapper key = { JSON . stringify ( state . validationErrors ) } >
660685 < MainMenuWrapper >
@@ -1907,35 +1932,7 @@ const ReservationUnitEditor = (): JSX.Element | null => {
19071932 ) }
19081933 </ Accordion >
19091934 < ArchiveButton
1910- onClick = { ( ) =>
1911- setModalContent (
1912- < ArchiveDialog
1913- reservationUnit = {
1914- state . reservationUnit as ReservationUnitByPkType
1915- }
1916- onAccept = { async ( ) => {
1917- try {
1918- const r = await createOrUpdateReservationUnit (
1919- state . reservationUnit ?. isDraft || false ,
1920- true
1921- ) ;
1922-
1923- if ( r ) {
1924- setModalContent ( null ) ;
1925- notifySuccess (
1926- t ( "ArchiveReservationUnitDialog.success" )
1927- ) ;
1928- history ( "/reservation-units" ) ;
1929- }
1930- } catch ( e ) {
1931- // noop
1932- }
1933- } }
1934- onClose = { ( ) => setModalContent ( null ) }
1935- /> ,
1936- true
1937- )
1938- }
1935+ onClick = { handleArchiveButtonClick }
19391936 variant = "secondary"
19401937 >
19411938 { t ( "ReservationUnitEditor.archive" ) }
0 commit comments