Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
@@ -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<string, DocType>
Expand Down Expand Up @@ -74,3 +74,42 @@ export function extractFields(docType: DocType): RawField[] {
options: field.options,
}));
}

export async function saveDocumentToLocal(
documentName: string,
document: documentList
): Promise<boolean> {
try {
const existingDocument = await AsyncStorage.getItem('downloadedDocuments');
let allDocumentStorage: Record<string, documentList> = 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<documentList | null> {
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;
}
}
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,7 @@ export interface Permission {
if_owner: number;
docType: string;
}

export interface documentList {
name: string;
}