11import { useEffect } from 'react' ;
22
33import { useAppSelector , useAppDispatch , setRowData } from 'state' ;
4- import { selectJsonSchema , selectCryptedData , setCryptedData , selectUiSchema , setJsonSchema , setUiSchema , CryptedData } from 'project-state' ;
4+ import { selectJsonSchema , selectCryptedData , setCryptedData , selectUiSchema , setJsonSchema , setUiSchema } from 'project-state' ;
55import { PGPProvider , useDecryptUsingContext } from 'pgp-provider' ;
66import { api } from '@formswizard/api' ;
77
88function useFormId ( ) {
9- const hash = typeof location != 'undefined' ? location . hash . slice ( 1 ) : '' ;
10- const hashParameters = ! hash ? { } : Object . fromEntries ( new URLSearchParams ( hash ) as any ) ;
11- const { formId } = hashParameters ;
12- return formId
9+ if ( typeof location === 'undefined' || ! location . hash ) {
10+ return undefined ;
11+ }
12+
13+ const hash = location . hash . startsWith ( '#' ) ? location . hash . slice ( 1 ) : location . hash ;
14+ const formId = new URLSearchParams ( hash ) . get ( 'formId' ) ;
15+ return formId ?? undefined ;
1316}
1417
1518export function DecryptAndImportLastNewSubmission ( ) {
@@ -19,13 +22,14 @@ export function DecryptAndImportLastNewSubmission() {
1922 const cryptedData = useAppSelector ( selectCryptedData ) ;
2023 useEffect ( ( ) => {
2124 async function loadCryptedData ( ) {
25+ if ( ! formId ) return ;
2226 const { cryptedData } = await api . getProjectStateCryptedData ( formId ) ;
2327 cryptedData ?. map ( cryptedDatum => {
2428 dispatch ( setCryptedData ( cryptedDatum ) ) ; // TODO use a setter for all cryptedData at the same time
2529 } ) ;
2630 }
2731 cryptedData . length || loadCryptedData ( )
28- } , [ cryptedData ] )
32+ } , [ cryptedData , dispatch , formId ] )
2933
3034 const decrypt = useDecryptUsingContext ( ) ;
3135 useEffect ( ( ) => {
@@ -36,7 +40,7 @@ export function DecryptAndImportLastNewSubmission() {
3640 const row = { ...decrypted , id, keyId, armoredPublicKey }
3741 decrypted && dispatch ( setRowData ( { row} ) ) ;
3842 } )
39- } , [ cryptedData ] ) ;
43+ } , [ cryptedData , dispatch , decrypt ] ) ;
4044
4145 return < > </ >
4246}
@@ -49,13 +53,14 @@ export function useSchema() {
4953
5054 useEffect ( ( ) => {
5155 async function loadSchema ( ) {
56+ if ( ! formId ) return ;
5257 const { schema } = await api . getProjectStateSchema ( formId ) ;
5358 const { jsonSchema, uiSchema } = schema || { } ;
5459 jsonSchema && dispatch ( setJsonSchema ( jsonSchema ) )
5560 jsonSchema && dispatch ( setUiSchema ( uiSchema ) )
5661 }
5762 jsonSchema || loadSchema ( )
58- } , [ jsonSchema , uiSchema , dispatch ] )
63+ } , [ jsonSchema , uiSchema , dispatch , formId ] )
5964}
6065
6166export function Import ( ) {
0 commit comments