Skip to content

Commit

Permalink
fix(front): verify existence of cached data before attempting to upload
Browse files Browse the repository at this point in the history
  • Loading branch information
stdavis committed Nov 3, 2023
1 parent f6a59b8 commit a435ec6
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/front/services/offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,17 @@ export async function clearBaseMapCache() {
const OfflineCacheContext = createContext();

export async function getOfflineSubmission(id, pickupIndex) {
const filePath = `${offlineDataStorageDirectory}/${id}/${dataFileName}`;
try {
const json = await readAsStringAsync(
`${offlineDataStorageDirectory}/${id}/${dataFileName}`,
);
// if data.json doesn't exist, remove the folder
const fileInfo = await getInfoAsync(filePath);
if (!fileInfo.exists) {
await deleteAsync(`${offlineDataStorageDirectory}/${id}`);

throw new Error(`${filePath} does not exist. Folder deleted.`);
}

const json = await readAsStringAsync(filePath);

if (pickupIndex) {
return JSON.parse(json).pickups[pickupIndex];
Expand Down Expand Up @@ -267,14 +274,23 @@ export function OfflineCacheContextProvider({ children }) {

submitValues.offlineStorageId = id;

submitValues.photo = await movePhoto(submitValues.photo, reportDirectory);
try {
submitValues.photo = await movePhoto(submitValues.photo, reportDirectory);

await writeAsStringAsync(
`${reportDirectory}/${dataFileName}`,
JSON.stringify(submitValues),
);
await writeAsStringAsync(
`${reportDirectory}/${dataFileName}`,
JSON.stringify(submitValues),
);

setCachedSubmissionIds((existing) => [...existing, id]);
setCachedSubmissionIds((existing) => [...existing, id]);
} catch (error) {
console.warning(
`error caching report, deleting directory ${reportDirectory}`,
);
await deleteAsync(reportDirectory);

throw error;
}

await showAlert(error);
};
Expand Down

0 comments on commit a435ec6

Please sign in to comment.