From c7a39642f3e4d7a754886aa90e2a8c5dbcb36668 Mon Sep 17 00:00:00 2001 From: Mantri Venkata Sai Nischal Date: Thu, 6 Nov 2025 22:42:36 +0530 Subject: [PATCH] added function to save the document to local storage and get the document from local storage --- src/api.ts | 41 ++++++++++++++++++++++++++++++++++++++++- src/types.ts | 4 ++++ 2 files changed, 44 insertions(+), 1 deletion(-) 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