@@ -2,46 +2,43 @@ import {useMapStore} from '@/app/store/mapStore';
22import { document , metadata } from '../mutations' ;
33import { handleCreateBlankMetadataObject } from '../../helpers' ;
44import { DocumentMetadata } from '../apiHandlers/types' ;
5- import { useRouter } from 'next/navigation' ;
65
76export const saveMap = async ( latestMetadata : DocumentMetadata | null ) => {
87 const { mapDocument, mapStatus, setMapStatus, upsertUserMap, setShareMapMessage} =
98 useMapStore . getState ( ) ;
10- const router = useRouter ( ) ;
119 if ( ! mapDocument ?. document_id ) return ;
1210
1311 if ( mapStatus ?. status === 'locked' || mapStatus ?. access === 'read' ) {
1412 // atp doesn't matter that it's locked, even with pw; should be able to copy map
1513 // what we need is a pw entry field to open if there's a pw required in the url
16- await document
17- . mutate ( {
14+ try {
15+ const newDocumentData = await document . mutate ( {
1816 districtr_map_slug : mapDocument ?. districtr_map_slug ?? '' ,
1917 metadata : latestMetadata ?? handleCreateBlankMetadataObject ( ) ,
2018 user_id : useMapStore . getState ( ) . userID ,
2119 copy_from_doc :
2220 mapDocument ?. access === 'read' && mapDocument ?. public_id
2321 ? mapDocument ?. public_id
2422 : mapDocument ?. document_id ,
25- } )
26- . then ( data => {
27- const updatedMetadata = latestMetadata ?? handleCreateBlankMetadataObject ( ) ;
28- metadata . mutate ( { document_id : data . document_id , metadata : updatedMetadata } ) ;
29- const updatedMapDoc = { ...data , map_metadata : { ...updatedMetadata } } ;
30-
31- upsertUserMap ( { documentId : data . document_id , mapDocument : updatedMapDoc } ) ;
32- // TODO Neither of these two settings seem to properly tell the client that the document can be edited
33- setMapStatus ( {
34- access : updatedMapDoc . access ,
35- status : updatedMapDoc . status ,
36- } ) ;
23+ } ) ;
3724
38- router . push ( `/map/edit/${ data . document_id } ` ) ;
39- } )
40- . catch ( err => {
41- setShareMapMessage (
42- `Unable to copy map. Reference: ${ mapDocument ?. document_id } |${ mapDocument ?. districtr_map_slug } `
43- ) ;
25+ const updatedMetadata = latestMetadata ?? handleCreateBlankMetadataObject ( ) ;
26+ metadata . mutate ( { document_id : newDocumentData . document_id , metadata : updatedMetadata } ) ;
27+ const updatedMapDoc = { ...newDocumentData , map_metadata : { ...updatedMetadata } } ;
28+ upsertUserMap ( { documentId : newDocumentData . document_id , mapDocument : updatedMapDoc } ) ;
29+ // TODO Neither of these two settings seem to properly tell the client that the document can be edited
30+ setMapStatus ( {
31+ access : updatedMapDoc . access ,
32+ status : updatedMapDoc . status ,
4433 } ) ;
34+ return newDocumentData ;
35+ } catch ( err ) {
36+ console . error ( 'Error saving map: ' , err ) ;
37+ setShareMapMessage (
38+ `Unable to copy map. Reference: ${ mapDocument ?. document_id } |${ mapDocument ?. districtr_map_slug } `
39+ ) ;
40+ return null ;
41+ }
4542 } else {
4643 await metadata . mutate ( {
4744 document_id : mapDocument ?. document_id ,
@@ -54,5 +51,6 @@ export const saveMap = async (latestMetadata: DocumentMetadata | null) => {
5451 map_metadata : latestMetadata ?? handleCreateBlankMetadataObject ( ) ,
5552 } ,
5653 } ) ;
54+ return mapDocument ;
5755 }
5856} ;
0 commit comments