diff --git a/src/api.ts b/src/api.ts index a6ad807..5cb818f 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,5 +1,5 @@ import AsyncStorage from '@react-native-async-storage/async-storage'; -import { DocType, FormItem, RawField } from './types'; +import { DocType, FormItem, RawField, documentList } from './types'; export async function getAllDoctypesFromLocal(): Promise< Record @@ -74,3 +74,42 @@ export function extractFields(docType: DocType): RawField[] { options: field.options, })); } + +export async function saveDocumentToLocal( + documentName: string, + document: documentList +): Promise { + try { + const existingDocument = await AsyncStorage.getItem('downloadedDocuments'); + let allDocumentStorage: Record = existingDocument + ? JSON.parse(existingDocument) + : {}; + allDocumentStorage[documentName] = document; + await AsyncStorage.setItem( + 'downlaodedDocuments', + JSON.stringify(allDocumentStorage) + ); + } + catch (error) { + console.error(`Error saving local document: ${documentName}:`, error); + throw error as Error; + } + return true; +} + + +export async function getDocumentFromLocal( + documentName: string +): Promise { + try { + const stored = await AsyncStorage.getItem('downloadedDocuments'); + if (!stored) { + return null; + } + const documentData = JSON.parse(stored)[documentName] as documentList; + return documentData; + } catch (error) { + console.error(`Error fetching local document: ${documentName}:`, error); + throw error as Error; + } +} diff --git a/src/types.ts b/src/types.ts index 4ad054d..afbea95 100644 --- a/src/types.ts +++ b/src/types.ts @@ -145,3 +145,7 @@ export interface Permission { if_owner: number; docType: string; } + +export interface documentList { + name: string; +} \ No newline at end of file