(ui) WIP trying to get all cache scenarios working smoothly, fix assets

This commit is contained in:
Mary Hipp
2023-08-11 16:14:47 -04:00
committed by psychedelicious
parent 0a71d6baa1
commit 767a612746
4 changed files with 67 additions and 47 deletions

View File

@ -399,15 +399,18 @@ export const imagesApi = api.injectEndpoints({
method: 'PATCH',
body: { pinned },
}),
invalidatesTags: (result, error, { imageDTO }) => [
{
type: 'ImageList',
id: getListImagesUrl({
board_id: imageDTO.board_id,
categories: IMAGE_CATEGORIES,
}),
},
],
invalidatesTags: (result, error, { imageDTO }) => {
const categories = getCategories(imageDTO);
return [
{
type: 'ImageList',
id: getListImagesUrl({
board_id: imageDTO.board_id,
categories,
}),
},
]
},
async onQueryStarted(
{ imageDTO, pinned },
{ dispatch, queryFulfilled, getState }
@ -465,27 +468,14 @@ export const imagesApi = api.injectEndpoints({
const isCacheFullyPopulated =
currentCache.data && currentCache.data.ids.length >= (total ?? 0);
const isInDateRangeForPinnedState = getIsImageInDateRange(
const isInDateRange = getIsImageInDateRange(
currentCache.data,
updatedImage
);
if (!isInDateRangeForPinnedState) {
// if newly pinned or unpinned image is not in date range for its new state, remove from cache
patches.push(
dispatch(
imagesApi.util.updateQueryData(
'listImages',
queryArgs,
(draft) => {
imagesAdapter.removeOne(draft, updatedImage.image_name);
}
)
)
);
}
// should we remove images from cache if _not_ in date range? ie you are showing 100 of 101 pinned images and you unpin one. technically it should disappear from list.
if (isCacheFullyPopulated || isInDateRangeForPinnedState) {
if (isCacheFullyPopulated || isInDateRange) {
// *upsert* to $cache
patches.push(
dispatch(

View File

@ -21,21 +21,35 @@ export const getIsImageInDateRange = (
return true;
}
const cacheImageDTOSForPinnedState = totalCachedImageDtos.filter((image) => image.pinned === imageDTO.pinned);
const cachedPinnedImages = [];
const cachedUnpinnedImages = [];
if (cacheImageDTOSForPinnedState.length > 1) {
// Images are sorted by `pinned` DESC and then `created_at` DESC
// check if the image is newer than the oldest image in the cache for either the pinned group or unpinned group
for (let index = 0; index < totalCachedImageDtos.length; index++) {
const image = totalCachedImageDtos[index];
if (image?.pinned) cachedPinnedImages.push(image)
if (!image?.pinned) cachedUnpinnedImages.push(image)
}
const lastPinnedImage = cachedPinnedImages[cachedPinnedImages.length - 1];
const lastUnpinnedImage = cachedUnpinnedImages[cachedUnpinnedImages.length - 1];
if (!lastPinnedImage || !lastUnpinnedImage) {
// satisfy TS gods, we already confirmed the array has more than one image
return false;
}
if (imageDTO.pinned) {
// if pinning or already pinned, want to look in list of pinned images
const createdDate = new Date(imageDTO.created_at);
const oldestImage = cacheImageDTOSForPinnedState[cacheImageDTOSForPinnedState.length - 1];
if (!oldestImage) {
// satisfy TS gods, we already confirmed the array has more than one image
return false;
}
const oldestDate = new Date(oldestImage.created_at);
const oldestDate = new Date(lastPinnedImage.created_at);
return createdDate >= oldestDate;
} else {
// if unpinning or already unpinned, want to look in list of unpinned images
const createdDate = new Date(imageDTO.created_at);
const oldestDate = new Date(lastUnpinnedImage.created_at);
return createdDate >= oldestDate;
}
return false;
};
export const getCategories = (imageDTO: ImageDTO) => {