Skip to content

Commit

Permalink
fix: offline projects list 🐛 (#1254)
Browse files Browse the repository at this point in the history
## JIRA Ticket

[BSS-548](https://jira.csiro.au/browse/BSS-548)

## Description

Projects list not working while offline.

## Proposed Changes

- Added function to update local database with projects list on both
sync and init

## How to Test

1. Ensure the projects list works without internet

## Additional Information

I believe the context provider was already pulling in the local projects
it was just empty as the update local function was lost in an update.

## Checklist

- [x] I have confirmed all commits have been signed.
- [x] I have added JSDoc style comments to any new functions or classes.
- [x] Relevant documentation such as READMEs, guides, and class comments
are updated.
  • Loading branch information
luke-mcfarlane-rocketlab authored Dec 11, 2024
2 parents b2d15f0 + 1ddbab2 commit 4218a2d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
9 changes: 5 additions & 4 deletions app/src/context/functions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,19 @@ export const getAnyToken = async (): Promise<JWTTokenInfo | undefined> => {
* @returns {Promise<ProjectObject[]>} - A promise that resolves to an array of ProjectObject.
*/
const getProjects = async (url: string, token: string) => {
// fetch the projects but guard against being offline and this failing
const response = await fetch(`${url}/api/directory`, {
headers: {
Authorization: `Bearer ${token}`,
},
});
}).catch(() => null);

if (!response.ok) {
if (response && response.ok) {
return (await response.json()) as ProjectObject[];
} else {
console.error(`Error fetching projects from ${url}`);
return [] as ProjectObject[];
}

return (await response.json()) as ProjectObject[];
};

/**
Expand Down
11 changes: 9 additions & 2 deletions app/src/context/projects-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
activateProjectDB,
getProjectsDB,
setSyncProjectDB,
updateProjectsDB,
} from '../dbs/projects-db';
import {activate_project} from '../sync/process-initialization';
import {ProjectExtended} from '../types/project';
Expand Down Expand Up @@ -70,7 +71,10 @@ export function ProjectsProvider({children}: {children: ReactNode}) {
});
}

setProjects([...newProjectsMap.values()]);
const newProjects = [...newProjectsMap.values()];

updateProjectsDB(newProjects);
setProjects(newProjects);
};

/**
Expand All @@ -96,7 +100,10 @@ export function ProjectsProvider({children}: {children: ReactNode}) {
});
}

setProjects([...newProjectsMap.values()]);
const newProjects = [...newProjectsMap.values()];

updateProjectsDB(newProjects);
setProjects(newProjects);
};

/**
Expand Down

0 comments on commit 4218a2d

Please sign in to comment.