11import { useEffect } from 'react' ;
22
33import { useAppSelector , useAppDispatch , setRowData } from 'state' ;
4- import { selectJsonSchema , selectCryptedData } from 'project-state' ;
4+ import { selectJsonSchema , selectCryptedData , setCryptedData , selectUiSchema , setJsonSchema , setUiSchema } from 'project-state' ;
55import { PGPProvider , decryptUsingContext } from 'pgp-provider' ;
6+ import { DefaultService , OpenAPI } from '@formswizard/api' ;
7+
8+ OpenAPI . BASE = 'http://localhost:4000' ;
9+ const { getProjectStateCryptedData, getProjectStateSchema } = DefaultService ;
610
711export function DecryptAndImportLastNewSubmission ( ) {
12+ const dispatch = useAppDispatch ( ) ;
813 const jsonSchema = useAppSelector ( selectJsonSchema ) ;
14+
915 const cryptedData = useAppSelector ( selectCryptedData ) ;
16+ useEffect ( ( ) => {
17+ async function loadCryptedData ( ) {
18+ const { cryptedData } = await getProjectStateCryptedData ( ) ;
19+ const latestCryptedDatum = cryptedData ?. length && cryptedData [ cryptedData . length - 1 ] ;
20+ latestCryptedDatum && dispatch ( setCryptedData ( latestCryptedDatum ) ) ;
21+ }
22+ console . log ( { cryptedData} )
23+ cryptedData . length || loadCryptedData ( )
24+ } , [ cryptedData ] )
1025
1126 /** TODO: Delete decrypted dataset after import and loop over submissions **/
1227
1328 const { data, uuid, keyId, armoredPublicKey } = cryptedData [ cryptedData . length - 1 ] || { } ;
1429 const decrypted_str = decryptUsingContext ( data ) ;
1530 const decrypted = decrypted_str && JSON . parse ( decrypted_str ) ;
1631
17- const dispatch = useAppDispatch ( ) ;
1832 useEffect ( ( ) => {
1933 const row = { ...decrypted , id : uuid , uuid, keyId, armoredPublicKey }
2034 decrypted && dispatch ( setRowData ( { row} ) ) ;
@@ -23,7 +37,24 @@ export function DecryptAndImportLastNewSubmission() {
2337 return < > </ >
2438}
2539
40+ export function useSchema ( ) {
41+ const dispatch = useAppDispatch ( ) ;
42+ const jsonSchema = useAppSelector ( selectJsonSchema ) ;
43+ const uiSchema = useAppSelector ( selectUiSchema ) ;
44+ useEffect ( ( ) => {
45+ async function loadSchema ( ) {
46+ const { schema } = await getProjectStateSchema ( ) ;
47+ const { jsonSchema, uiSchema } = schema || { } ;
48+ jsonSchema && dispatch ( setJsonSchema ( jsonSchema ) )
49+ jsonSchema && dispatch ( setUiSchema ( uiSchema ) )
50+ }
51+ jsonSchema || loadSchema ( )
52+ } , [ jsonSchema , uiSchema , dispatch ] )
53+ }
54+
2655export function Import ( ) {
56+ useSchema ( )
57+
2758 return < PGPProvider >
2859 < DecryptAndImportLastNewSubmission />
2960 </ PGPProvider >
0 commit comments