diff --git a/dashboard/src/app.js b/dashboard/src/app.js index 926c65d87..e5c6fac89 100644 --- a/dashboard/src/app.js +++ b/dashboard/src/app.js @@ -195,8 +195,10 @@ export default function ContextedApp() { const [recoilKey, setRecoilKey] = useState(0); return ( - - setRecoilKey((k) => k + 1)} /> + CHAAARGEMENT}> + + setRecoilKey((k) => k + 1)} /> + ); } diff --git a/dashboard/src/components/DataLoader.js b/dashboard/src/components/DataLoader.js index f15223954..f72a83cdf 100644 --- a/dashboard/src/components/DataLoader.js +++ b/dashboard/src/components/DataLoader.js @@ -1,5 +1,5 @@ import { useEffect } from 'react'; -import { atom, useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'; +import { atom, selector, useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'; import { toast } from 'react-toastify'; import { personsState } from '../recoil/persons'; @@ -17,7 +17,7 @@ import { consultationsState, formatConsultation } from '../recoil/consultations' import { commentsState } from '../recoil/comments'; import { organisationState, userState } from '../recoil/auth'; -import { clearCache, dashboardCurrentCacheKey, getCacheItem, getCacheItemDefaultValue, setCacheItem } from '../services/dataManagement'; +import { clearCache, dashboardCurrentCacheKey, getCacheItemDefaultValue, setCacheItem } from '../services/dataManagement'; import API from '../services/api'; import { RandomPicture, RandomPicturePreloader } from './LoaderRandomPicture'; import ProgressBar from './LoaderProgressBar'; @@ -35,7 +35,13 @@ export const initialLoadingTextState = 'En attente de chargement'; export const loadingTextState = atom({ key: 'loadingTextState', default: initialLoadingTextState }); export const lastLoadState = atom({ key: 'lastLoadState', - default: null, + default: selector({ + key: 'lastLoadState/default', + get: async () => { + const cache = await getCacheItemDefaultValue(dashboardCurrentCacheKey, 0); + return cache; + }, + }), effects: [ ({ onSet }) => { onSet(async (newValue) => { @@ -80,28 +86,28 @@ export function useDataLoader(options = { refreshOnMount: false }) { const [isLoading, setIsLoading] = useRecoilState(isLoadingState); const [initialLoad, setInitialLoad] = useRecoilState(initialLoadState); const setLoadingText = useSetRecoilState(loadingTextState); - const setLastLoad = useSetRecoilState(lastLoadState); + const [lastLoadValue, setLastLoad] = useRecoilState(lastLoadState); + const setProgress = useSetRecoilState(progressState); + const setTotal = useSetRecoilState(totalState); const setUser = useSetRecoilState(userState); const setOrganisation = useSetRecoilState(organisationState); const { migrateData } = useDataMigrator(); const setPersons = useSetRecoilState(personsState); - const setActions = useSetRecoilState(actionsState); - const setConsultations = useSetRecoilState(consultationsState); - const setTreatments = useSetRecoilState(treatmentsState); - const setMedicalFiles = useSetRecoilState(medicalFileState); + const setGroups = useSetRecoilState(groupsState); + const setReports = useSetRecoilState(reportsState); const setPassages = useSetRecoilState(passagesState); const setRencontres = useSetRecoilState(rencontresState); - const setReports = useSetRecoilState(reportsState); + const setActions = useSetRecoilState(actionsState); const setTerritories = useSetRecoilState(territoriesState); const setPlaces = useSetRecoilState(placesState); const setRelsPersonPlace = useSetRecoilState(relsPersonPlaceState); const setTerritoryObservations = useSetRecoilState(territoryObservationsState); const setComments = useSetRecoilState(commentsState); - const setGroups = useSetRecoilState(groupsState); - const setProgress = useSetRecoilState(progressState); - const setTotal = useSetRecoilState(totalState); + const setConsultations = useSetRecoilState(consultationsState); + const setTreatments = useSetRecoilState(treatmentsState); + const setMedicalFiles = useSetRecoilState(medicalFileState); useEffect(function refreshOnMountEffect() { if (options.refreshOnMount && !isLoading) loadOrRefreshData(false); @@ -131,9 +137,6 @@ export function useDataLoader(options = { refreshOnMount: false }) { if (initialLoad) { await migrateData(); } - const lastLoadValueCached = await getCacheItem(dashboardCurrentCacheKey); - const lastLoadValue = lastLoadValueCached || 0; - setLastLoad(lastLoadValue); const statsResponse = await API.get({ path: '/organisation/stats', @@ -151,19 +154,19 @@ export function useDataLoader(options = { refreshOnMount: false }) { let itemsCount = 0 + stats.persons + - stats.consultations + - stats.actions + - stats.treatments + - stats.medicalFiles + + stats.groups + + stats.reports + stats.passages + stats.rencontres + - stats.reports + + stats.actions + stats.territories + stats.places + stats.relsPersonPlace + stats.territoryObservations + stats.comments + - stats.groups; + stats.consultations + + stats.treatments + + stats.medicalFiles; console.log('stats', stats, 'itemsCount', itemsCount); @@ -177,206 +180,212 @@ export function useDataLoader(options = { refreshOnMount: false }) { withDeleted: Boolean(lastLoadValue), }; - setLoadingText('Chargement des personnes'); - const cachedPersons = await getCacheItemDefaultValue('person', []); - setPersons([...cachedPersons]); if (stats.persons > 0) { + let newItems = []; + setLoadingText('Chargement des personnes'); async function loadPersons(page = 0) { const res = await API.get({ path: '/person', query: { ...query, page: String(page) } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); console.log('Persons', page, res.data.length); setProgress((p) => p + res.data.length); - setPersons((items) => mergeItems(items, res.decryptedData)); + newItems.push(...res.decryptedData); if (res.hasMore) return loadPersons(page + 1); + setPersons((items) => mergeItems(items, newItems)); return true; } const personSuccess = await loadPersons(0); if (!personSuccess) return false; } - setLoadingText('Chargement des familles'); - const cachedGroups = await getCacheItemDefaultValue('group', []); - setGroups([...cachedGroups]); if (stats.groups > 0) { + let newItems = []; + setLoadingText('Chargement des familles'); async function loadGroups(page = 0) { const res = await API.get({ path: '/group', query: { ...query, page: String(page) } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setGroups((items) => mergeItems(items, res.decryptedData)); + newItems.push(...res.decryptedData); if (res.hasMore) return loadGroups(page + 1); + setGroups((items) => mergeItems(items, newItems)); return true; } const groupsSuccess = await loadGroups(0); if (!groupsSuccess) return false; } - setLoadingText('Chargement des comptes-rendus'); - const cachedReports = await getCacheItemDefaultValue('report', []); - setReports([...cachedReports]); if (stats.reports > 0) { + let newItems = []; + setLoadingText('Chargement des comptes-rendus'); async function loadReports(page = 0) { const res = await API.get({ path: '/report', query: { ...query, page: String(page) } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setReports((items) => mergeItems(items, res.decryptedData, { filterNewItemsFunction: (r) => !!r.team && !!r.date })); + newItems.push(...res.decryptedData); if (res.hasMore) return loadReports(page + 1); + setReports((items) => mergeItems(items, newItems, { filterNewItemsFunction: (r) => !!r.team && !!r.date })); return true; } const reportsSuccess = await loadReports(0); if (!reportsSuccess) return false; } - setLoadingText('Chargement des passages'); - const cachedPassages = await getCacheItemDefaultValue('passage', []); - setPassages([...cachedPassages]); if (stats.passages > 0) { + let newItems = []; + setLoadingText('Chargement des passages'); async function loadPassages(page = 0) { const res = await API.get({ path: '/passage', query: { ...query, page: String(page) } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setPassages((items) => mergeItems(items, res.decryptedData)); + newItems.push(...res.decryptedData); if (res.hasMore) return loadPassages(page + 1); + setPassages((items) => mergeItems(items, newItems)); return true; } const passagesSuccess = await loadPassages(0); if (!passagesSuccess) return false; } - setLoadingText('Chargement des rencontres'); - const cachedRencontres = await getCacheItemDefaultValue('rencontre', []); - setRencontres([...cachedRencontres]); if (stats.rencontres > 0) { + let newItems = []; + setLoadingText('Chargement des rencontres'); async function loadRencontres(page = 0) { const res = await API.get({ path: '/rencontre', query: { ...query, page: String(page) } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setRencontres((items) => mergeItems(items, res.decryptedData)); + newItems.push(...res.decryptedData); if (res.hasMore) return loadRencontres(page + 1); + setRencontres((items) => mergeItems(items, newItems)); return true; } const rencontresSuccess = await loadRencontres(0); if (!rencontresSuccess) return false; } - setLoadingText('Chargement des actions'); - const cachedActions = await getCacheItemDefaultValue('action', []); - setActions([...cachedActions]); if (stats.actions > 0) { + let newItems = []; + setLoadingText('Chargement des actions'); async function loadActions(page = 0) { const res = await API.get({ path: '/action', query: { ...query, page: String(page) } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setActions((items) => mergeItems(items, res.decryptedData)); + newItems.push(...res.decryptedData); if (res.hasMore) return loadActions(page + 1); + setActions((items) => mergeItems(items, newItems)); return true; } const actionsSuccess = await loadActions(0); if (!actionsSuccess) return false; } - setLoadingText('Chargement des territoires'); - const cachedTerritories = await getCacheItemDefaultValue('territory', []); - setTerritories([...cachedTerritories]); if (stats.territories > 0) { + let newItems = []; + setLoadingText('Chargement des territoires'); async function loadTerritories(page = 0) { const res = await API.get({ path: '/territory', query: { ...query, page: String(page) } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setTerritories((items) => mergeItems(items, res.decryptedData)); + newItems.push(...res.decryptedData); if (res.hasMore) return loadTerritories(page + 1); + setTerritories((items) => mergeItems(items, newItems)); return true; } const territoriesSuccess = await loadTerritories(0); if (!territoriesSuccess) return false; } - setLoadingText('Chargement des lieux'); - const cachedPlaces = await getCacheItemDefaultValue('place', []); - setPlaces([...cachedPlaces]); if (stats.places > 0) { + let newItems = []; + setLoadingText('Chargement des lieux'); async function loadPlaces(page = 0) { const res = await API.get({ path: '/place', query: { ...query, page: String(page) } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setPlaces((items) => mergeItems(items, res.decryptedData)); + newItems.push(...res.decryptedData); if (res.hasMore) return loadPlaces(page + 1); + setPlaces((items) => mergeItems(items, newItems)); return true; } const placesSuccess = await loadPlaces(0); if (!placesSuccess) return false; } - setLoadingText('Chargement des relations personne-lieu'); - const cachedRelPersonPlace = await getCacheItemDefaultValue('relPersonPlace', []); - setRelsPersonPlace([...cachedRelPersonPlace]); if (stats.relsPersonPlace > 0) { + let newItems = []; + setLoadingText('Chargement des relations personne-lieu'); async function loadRelPersonPlaces(page = 0) { const res = await API.get({ path: '/relPersonPlace', query: { ...query, page: String(page) } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setRelsPersonPlace((items) => mergeItems(items, res.decryptedData)); + newItems.push(...res.decryptedData); if (res.hasMore) return loadRelPersonPlaces(page + 1); + setRelsPersonPlace((items) => mergeItems(items, newItems)); return true; } const relsPersonPlacesSuccess = await loadRelPersonPlaces(0); if (!relsPersonPlacesSuccess) return false; } - setLoadingText('Chargement des observations de territoire'); - const cachedObservations = await getCacheItemDefaultValue('territory-observation', []); - setTerritoryObservations([...cachedObservations]); if (stats.territoryObservations > 0) { + let newItems = []; + setLoadingText('Chargement des observations de territoire'); async function loadObservations(page = 0) { const res = await API.get({ path: '/territory-observation', query: { ...query, page: String(page) } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setTerritoryObservations((items) => mergeItems(items, res.decryptedData)); + newItems.push(...res.decryptedData); if (res.hasMore) return loadObservations(page + 1); + setTerritoryObservations((items) => mergeItems(items, newItems)); return true; } const territoryObservationsSuccess = await loadObservations(0); if (!territoryObservationsSuccess) return false; } - setLoadingText('Chargement des commentaires'); - const cachedComments = await getCacheItemDefaultValue('comment', []); - setComments([...cachedComments]); if (stats.comments > 0) { + let newItems = []; + setLoadingText('Chargement des commentaires'); async function loadComments(page = 0) { const res = await API.get({ path: '/comment', query: { ...query, page: String(page) } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setComments((items) => mergeItems(items, res.decryptedData)); + newItems.push(...res.decryptedData); if (res.hasMore) return loadComments(page + 1); + setComments((items) => mergeItems(items, newItems)); return true; } const commentsSuccess = await loadComments(0); if (!commentsSuccess) return false; } - setLoadingText('Chargement des consultations'); if (stats.consultations > 0) { + let newItems = []; + setLoadingText('Chargement des consultations'); async function loadConsultations(page = 0) { const res = await API.get({ path: '/consultation', query: { ...query, page: String(page), after: initialLoad ? 0 : lastLoadValue } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setConsultations((items) => mergeItems(items, res.decryptedData, { formatNewItemsFunction: formatConsultation })); + newItems.push(...res.decryptedData); if (res.hasMore) return loadConsultations(page + 1); + setConsultations((items) => mergeItems(items, newItems, { formatNewItemsFunction: formatConsultation })); return true; } const consultationsSuccess = await loadConsultations(0); if (!consultationsSuccess) return false; } - setLoadingText('Chargement des traitements'); if (stats.treatments > 0) { + let newItems = []; + setLoadingText('Chargement des traitements'); async function loadTreatments(page = 0) { const res = await API.get({ path: '/treatment', query: { ...query, page: String(page), after: initialLoad ? 0 : lastLoadValue } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setTreatments((items) => mergeItems(items, res.decryptedData)); + newItems.push(...res.decryptedData); if (res.hasMore) return loadTreatments(page + 1); + setTreatments((items) => mergeItems(items, newItems)); return true; } const treatmentsSuccess = await loadTreatments(0); if (!treatmentsSuccess) return false; } - setLoadingText('Chargement des fichiers médicaux'); if (stats.medicalFiles > 0) { + let newItems = []; + setLoadingText('Chargement des fichiers médicaux'); async function loadMedicalFiles(page = 0) { const res = await API.get({ path: '/medical-file', query: { ...query, page: String(page), after: initialLoad ? 0 : lastLoadValue } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setMedicalFiles((items) => mergeItems(items, res.decryptedData)); + newItems.push(...res.decryptedData); if (res.hasMore) return loadMedicalFiles(page + 1); + setMedicalFiles((items) => mergeItems(items, newItems)); return true; } const medicalFilesSuccess = await loadMedicalFiles(0); diff --git a/dashboard/src/recoil/actions.js b/dashboard/src/recoil/actions.js index 5be0c19ac..5646c8cec 100644 --- a/dashboard/src/recoil/actions.js +++ b/dashboard/src/recoil/actions.js @@ -1,4 +1,4 @@ -import { setCacheItem } from '../services/dataManagement'; +import { getCacheItemDefaultValue, setCacheItem } from '../services/dataManagement'; import { atom, selector } from 'recoil'; import { organisationState } from './auth'; import { looseUuidRegex } from '../utils'; @@ -8,7 +8,13 @@ import { capture } from '../services/sentry'; const collectionName = 'action'; export const actionsState = atom({ key: collectionName, - default: [], + default: selector({ + key: 'action/default', + get: async () => { + const cache = await getCacheItemDefaultValue('action', []); + return cache; + }, + }), effects: [({ onSet }) => onSet(async (newValue) => setCacheItem(collectionName, newValue))], }); diff --git a/dashboard/src/recoil/comments.js b/dashboard/src/recoil/comments.js index 52378399b..6c692f06e 100644 --- a/dashboard/src/recoil/comments.js +++ b/dashboard/src/recoil/comments.js @@ -1,5 +1,5 @@ -import { setCacheItem } from '../services/dataManagement'; -import { atom } from 'recoil'; +import { getCacheItemDefaultValue, setCacheItem } from '../services/dataManagement'; +import { atom, selector } from 'recoil'; import { looseUuidRegex } from '../utils'; import { toast } from 'react-toastify'; import { capture } from '../services/sentry'; @@ -7,7 +7,13 @@ import { capture } from '../services/sentry'; const collectionName = 'comment'; export const commentsState = atom({ key: collectionName, - default: [], + default: selector({ + key: 'comment/default', + get: async () => { + const cache = await getCacheItemDefaultValue('comment', []); + return cache; + }, + }), effects: [({ onSet }) => onSet(async (newValue) => setCacheItem(collectionName, newValue))], }); diff --git a/dashboard/src/recoil/groups.ts b/dashboard/src/recoil/groups.ts index 03524dc29..e2d9a7293 100644 --- a/dashboard/src/recoil/groups.ts +++ b/dashboard/src/recoil/groups.ts @@ -1,12 +1,18 @@ -import { setCacheItem } from '../services/dataManagement'; -import { atom, selectorFamily } from 'recoil'; +import { getCacheItemDefaultValue, setCacheItem } from '../services/dataManagement'; +import { atom, selector, selectorFamily } from 'recoil'; import type { GroupInstance } from '../types/group'; import type { UUIDV4 } from '../types/uuid'; const collectionName = 'group'; export const groupsState = atom({ key: collectionName, - default: [], + default: selector({ + key: 'group/default', + get: async () => { + const cache = await getCacheItemDefaultValue('group', []); + return cache; + }, + }), effects: [({ onSet }) => onSet(async (newValue) => setCacheItem(collectionName, newValue))], }); diff --git a/dashboard/src/recoil/passages.js b/dashboard/src/recoil/passages.js index 599d29985..3f93e5399 100644 --- a/dashboard/src/recoil/passages.js +++ b/dashboard/src/recoil/passages.js @@ -1,5 +1,5 @@ -import { setCacheItem } from '../services/dataManagement'; -import { atom } from 'recoil'; +import { getCacheItemDefaultValue, setCacheItem } from '../services/dataManagement'; +import { atom, selector } from 'recoil'; import { looseUuidRegex } from '../utils'; import { toast } from 'react-toastify'; import { capture } from '../services/sentry'; @@ -7,7 +7,13 @@ import { capture } from '../services/sentry'; const collectionName = 'passage'; export const passagesState = atom({ key: collectionName, - default: [], + default: selector({ + key: 'passage/default', + get: async () => { + const cache = await getCacheItemDefaultValue('passage', []); + return cache; + }, + }), effects: [({ onSet }) => onSet(async (newValue) => setCacheItem(collectionName, newValue))], }); diff --git a/dashboard/src/recoil/persons.ts b/dashboard/src/recoil/persons.ts index 146ab1d32..3b3b6d6e7 100644 --- a/dashboard/src/recoil/persons.ts +++ b/dashboard/src/recoil/persons.ts @@ -1,4 +1,4 @@ -import { setCacheItem } from '../services/dataManagement'; +import { getCacheItemDefaultValue, setCacheItem } from '../services/dataManagement'; import { atom, selector, useRecoilValue } from 'recoil'; import { organisationState } from './auth'; import { toast } from 'react-toastify'; @@ -9,7 +9,13 @@ import type { PredefinedField, CustomField } from '../types/field'; const collectionName = 'person'; export const personsState = atom({ key: collectionName, - default: [], + default: selector({ + key: 'person/default', + get: async () => { + const cache = await getCacheItemDefaultValue('person', []); + return cache; + }, + }), effects: [({ onSet }) => onSet(async (newValue) => setCacheItem(collectionName, newValue))], }); diff --git a/dashboard/src/recoil/places.js b/dashboard/src/recoil/places.js index d869eb37d..45f73106f 100644 --- a/dashboard/src/recoil/places.js +++ b/dashboard/src/recoil/places.js @@ -1,5 +1,5 @@ -import { setCacheItem } from '../services/dataManagement'; -import { atom } from 'recoil'; +import { getCacheItemDefaultValue, setCacheItem } from '../services/dataManagement'; +import { atom, selector } from 'recoil'; import { looseUuidRegex } from '../utils'; import { toast } from 'react-toastify'; import { capture } from '../services/sentry'; @@ -7,7 +7,13 @@ import { capture } from '../services/sentry'; const collectionName = 'place'; export const placesState = atom({ key: collectionName, - default: [], + default: selector({ + key: 'place/default', + get: async () => { + const cache = await getCacheItemDefaultValue('place', []); + return cache; + }, + }), effects: [({ onSet }) => onSet(async (newValue) => setCacheItem(collectionName, newValue))], }); diff --git a/dashboard/src/recoil/relPersonPlace.js b/dashboard/src/recoil/relPersonPlace.js index ec6d72c23..4a972be4f 100644 --- a/dashboard/src/recoil/relPersonPlace.js +++ b/dashboard/src/recoil/relPersonPlace.js @@ -1,5 +1,5 @@ -import { setCacheItem } from '../services/dataManagement'; -import { atom } from 'recoil'; +import { getCacheItemDefaultValue, setCacheItem } from '../services/dataManagement'; +import { atom, selector } from 'recoil'; import { looseUuidRegex } from '../utils'; import { toast } from 'react-toastify'; import { capture } from '../services/sentry'; @@ -7,7 +7,13 @@ import { capture } from '../services/sentry'; const collectionName = 'relPersonPlace'; export const relsPersonPlaceState = atom({ key: collectionName, - default: [], + default: selector({ + key: 'relPersonPlace/default', + get: async () => { + const cache = await getCacheItemDefaultValue('relPersonPlace', []); + return cache; + }, + }), effects: [({ onSet }) => onSet(async (newValue) => setCacheItem(collectionName, newValue))], }); diff --git a/dashboard/src/recoil/rencontres.js b/dashboard/src/recoil/rencontres.js index a1705d963..d664b0d58 100644 --- a/dashboard/src/recoil/rencontres.js +++ b/dashboard/src/recoil/rencontres.js @@ -1,5 +1,5 @@ -import { setCacheItem } from '../services/dataManagement'; -import { atom } from 'recoil'; +import { getCacheItemDefaultValue, setCacheItem } from '../services/dataManagement'; +import { atom, selector } from 'recoil'; import { looseUuidRegex } from '../utils'; import { toast } from 'react-toastify'; import { capture } from '../services/sentry'; @@ -7,7 +7,13 @@ import { capture } from '../services/sentry'; const collectionName = 'rencontre'; export const rencontresState = atom({ key: collectionName, - default: [], + default: selector({ + key: 'rencontre/default', + get: async () => { + const cache = await getCacheItemDefaultValue('rencontre', []); + return cache; + }, + }), effects: [({ onSet }) => onSet(async (newValue) => setCacheItem(collectionName, newValue))], }); diff --git a/dashboard/src/recoil/reports.js b/dashboard/src/recoil/reports.js index b918f1090..7916e53e1 100644 --- a/dashboard/src/recoil/reports.js +++ b/dashboard/src/recoil/reports.js @@ -1,4 +1,4 @@ -import { setCacheItem } from '../services/dataManagement'; +import { getCacheItemDefaultValue, setCacheItem } from '../services/dataManagement'; import { atom, selector } from 'recoil'; import { capture } from '../services/sentry'; import { organisationState } from './auth'; @@ -8,7 +8,13 @@ import { toast } from 'react-toastify'; const collectionName = 'report'; export const reportsState = atom({ key: collectionName, - default: [], + default: selector({ + key: 'report/default', + get: async () => { + const cache = await getCacheItemDefaultValue('report', []); + return cache; + }, + }), effects: [ ({ onSet }) => onSet(async (newValue) => { diff --git a/dashboard/src/recoil/territory.js b/dashboard/src/recoil/territory.js index 9f054cbd3..8a5736250 100644 --- a/dashboard/src/recoil/territory.js +++ b/dashboard/src/recoil/territory.js @@ -1,5 +1,5 @@ -import { setCacheItem } from '../services/dataManagement'; -import { atom } from 'recoil'; +import { getCacheItemDefaultValue, setCacheItem } from '../services/dataManagement'; +import { atom, selector } from 'recoil'; import { looseUuidRegex } from '../utils'; import { toast } from 'react-toastify'; import { capture } from '../services/sentry'; @@ -7,7 +7,13 @@ import { capture } from '../services/sentry'; const collectionName = 'territory'; export const territoriesState = atom({ key: collectionName, - default: [], + default: selector({ + key: 'territory/default', + get: async () => { + const cache = await getCacheItemDefaultValue('territory', []); + return cache; + }, + }), effects: [({ onSet }) => onSet(async (newValue) => setCacheItem(collectionName, newValue))], }); diff --git a/dashboard/src/recoil/territoryObservations.js b/dashboard/src/recoil/territoryObservations.js index a9faf3c64..bcbc5fc05 100644 --- a/dashboard/src/recoil/territoryObservations.js +++ b/dashboard/src/recoil/territoryObservations.js @@ -1,6 +1,6 @@ import { organisationState } from './auth'; import { atom, selector } from 'recoil'; -import { setCacheItem } from '../services/dataManagement'; +import { getCacheItemDefaultValue, setCacheItem } from '../services/dataManagement'; import { looseUuidRegex } from '../utils'; import { toast } from 'react-toastify'; import { capture } from '../services/sentry'; @@ -8,7 +8,13 @@ import { capture } from '../services/sentry'; const collectionName = 'territory-observation'; export const territoryObservationsState = atom({ key: collectionName, - default: [], + default: selector({ + key: 'territory-observation/default', + get: async () => { + const cache = await getCacheItemDefaultValue('territory-observation', []); + return cache; + }, + }), effects: [({ onSet }) => onSet(async (newValue) => setCacheItem(collectionName, newValue))], });