diff --git a/src/services/fetchCache.ts b/src/services/fetchCache.ts index 00631cb3..2c14cbaa 100644 --- a/src/services/fetchCache.ts +++ b/src/services/fetchCache.ts @@ -4,21 +4,23 @@ const cache = {}; const fetchCache = isBrowser() ? { - get: (key) => sessionStorage.getItem(key), - remove: (key) => sessionStorage.removeItem(key), - put: (key, value) => sessionStorage.setItem(key, value), - clear: () => sessionStorage.clear(), + get: (key: string) => sessionStorage.getItem(key), + remove: (key: string) => sessionStorage.removeItem(key), + put: (key: string, value: string) => sessionStorage.setItem(key, value), + clear: () => sessionStorage.clear(), // this is little dirty, but we use sessionStorage only for this } : { - get: (key) => cache[key], - remove: (key) => delete cache[key], - put: (key, value) => { + get: (key: string) => cache[key], + remove: (key: string) => delete cache[key], + put: (key: string, value: string) => { cache[key] = value; }, - clear: () => {}, + clear: () => { + Object.keys(cache).forEach((key) => delete cache[key]); + }, }; -export const getKey = (url, opts) => { +export const getKey = (url: string, opts: Record) => { if (['POST', 'PUT', 'DELETE'].includes(opts.method)) { return false; } @@ -26,17 +28,23 @@ export const getKey = (url, opts) => { return url + JSON.stringify(opts); }; -export const getCache = (key) => { +export const getCache = (key: string | false) => { if (key) { return fetchCache.get(key); } }; -export const removeFetchCache = (url, opts = {}) => { - fetchCache.remove(getKey(url, opts)); +export const removeFetchCache = ( + url: string, + opts: Record = {}, +) => { + const key = getKey(url, opts); + if (key) { + fetchCache.remove(key); + } }; -export const writeCacheSafe = (key, value) => { +export const writeCacheSafe = (key: string | false, value: string) => { if (!key) return; try { @@ -48,3 +56,7 @@ export const writeCacheSafe = (key, value) => { console.warn(`Item ${key} was not saved to cache: `, e); // eslint-disable-line no-console } }; + +export const clearFetchCache = () => { + fetchCache.clear(); +}; diff --git a/src/services/osm/osmApiAuth.ts b/src/services/osm/osmApiAuth.ts index 3e3d4340..5aa13043 100644 --- a/src/services/osm/osmApiAuth.ts +++ b/src/services/osm/osmApiAuth.ts @@ -23,7 +23,7 @@ import { Xml2JsSingleDoc, } from '../helpers'; import { join } from '../../utils'; -import { clearFeatureCache } from './osmApi'; +import { clearFetchCache } from '../fetchCache'; import { isBrowser } from '../../components/helpers'; import { getLabel } from '../../helpers/featureLabel'; import { @@ -389,7 +389,8 @@ export const saveChanges = async ( const ids = [...savedNodesIds, ...savedWaysIds, ...savedRelationsIds]; const redirectId = original.point ? ids[0] : original.osmMeta; - // TODO invalidate all changed items in browser AND server ! + // TODO invalidate all changed also in server (?) + clearFetchCache(); return { type: 'edit', @@ -452,6 +453,8 @@ export const editCrag = async ( ); await putChangesetClose(changesetId); + clearFetchCache(); + return { type: 'edit', text: changesetComment,