@@ -2,7 +2,7 @@ import { useEffect } from 'react';
22
33import { useAppSelector , useAppDispatch , setRowData } from 'state' ;
44import { selectJsonSchema , selectCryptedData , setCryptedData , selectUiSchema , setJsonSchema , setUiSchema , CryptedData } from 'project-state' ;
5- import { PGPProvider , decryptUsingContext } from 'pgp-provider' ;
5+ import { PGPProvider , useDecryptUsingContext } from 'pgp-provider' ;
66import { api } from '@formswizard/api' ;
77
88function useFormId ( ) {
@@ -15,30 +15,28 @@ function useFormId() {
1515export function DecryptAndImportLastNewSubmission ( ) {
1616 const dispatch = useAppDispatch ( ) ;
1717 const formId = useFormId ( ) ;
18- const jsonSchema = useAppSelector ( selectJsonSchema ) ;
1918
2019 const cryptedData = useAppSelector ( selectCryptedData ) ;
2120 useEffect ( ( ) => {
2221 async function loadCryptedData ( ) {
2322 const { cryptedData } = await api . getProjectStateCryptedData ( formId ) ;
24- const latestCryptedDatum = cryptedData ?. length && cryptedData [ cryptedData . length - 1 ] ;
25- latestCryptedDatum && dispatch ( setCryptedData ( latestCryptedDatum ) ) ;
23+ cryptedData ?. map ( cryptedDatum => {
24+ dispatch ( setCryptedData ( cryptedDatum ) ) ; // TODO use a setter for all cryptedData at the same time
25+ } ) ;
2626 }
27- console . log ( { cryptedData} )
2827 cryptedData . length || loadCryptedData ( )
2928 } , [ cryptedData ] )
3029
31- /** TODO: Delete decrypted dataset after import and loop over submissions **/
32-
33- const cryptedDatum = cryptedData [ cryptedData . length - 1 ] || { } as CryptedData ;
34- const { id, data, keyId, armoredPublicKey } = cryptedDatum
35- const decrypted_str = decryptUsingContext ( data ) ;
36- const decrypted = decrypted_str && JSON . parse ( decrypted_str ) ;
37-
30+ const decrypt = useDecryptUsingContext ( ) ;
3831 useEffect ( ( ) => {
39- const row = { ...decrypted , id, keyId, armoredPublicKey }
40- decrypted && dispatch ( setRowData ( { row} ) ) ;
41- } , [ decrypted ] ) ;
32+ cryptedData . map ( async cryptedDatum => {
33+ const { id, data, keyId, armoredPublicKey } = cryptedDatum
34+ const decrypted_str = await decrypt ( data ) ;
35+ const decrypted = decrypted_str && JSON . parse ( decrypted_str ) ;
36+ const row = { ...decrypted , id, keyId, armoredPublicKey }
37+ decrypted && dispatch ( setRowData ( { row} ) ) ;
38+ } )
39+ } , [ cryptedData ] ) ;
4240
4341 return < > </ >
4442}
0 commit comments