invalidate board total when images deleted, only run date range logic if board has less than 20 images

This commit is contained in:
Mary Hipp 2023-08-07 10:23:02 -04:00 committed by psychedelicious
parent fe924daee3
commit 734a9e4271

View File

@ -4,6 +4,7 @@ import {
ASSETS_CATEGORIES,
BoardId,
IMAGE_CATEGORIES,
IMAGE_LIMIT,
} from 'features/gallery/store/types';
import { keyBy } from 'lodash';
import { ApiFullTagDescription, LIST_TAG, api } from '..';
@ -167,7 +168,14 @@ export const imagesApi = api.injectEndpoints({
},
};
},
invalidatesTags: (result, error, imageDTOs) => [],
invalidatesTags: (result, error, { imageDTOs }) => {
// for now, assume bulk delete is all on one board
const boardId = imageDTOs[0]?.board_id
return [
{ type: 'BoardImagesTotal', id: boardId ?? 'none' },
{ type: 'BoardAssetsTotal', id: boardId ?? 'none' },
]
},
async onQueryStarted({ imageDTOs }, { dispatch, queryFulfilled }) {
/**
* Cache changes for `deleteImages`:
@ -288,11 +296,11 @@ export const imagesApi = api.injectEndpoints({
imageDTO.image_category
)
? boardsApi.endpoints.getBoardImagesTotal.select(
imageDTO.board_id ?? 'none'
)(getState())
imageDTO.board_id ?? 'none'
)(getState())
: boardsApi.endpoints.getBoardAssetsTotal.select(
imageDTO.board_id ?? 'none'
)(getState());
imageDTO.board_id ?? 'none'
)(getState());
// IF it eligible for insertion into existing $cache
// "eligible" means either:
@ -718,11 +726,11 @@ export const imagesApi = api.injectEndpoints({
imageDTO.image_category
)
? boardsApi.endpoints.getBoardImagesTotal.select(
imageDTO.board_id ?? 'none'
)(getState())
imageDTO.board_id ?? 'none'
)(getState())
: boardsApi.endpoints.getBoardAssetsTotal.select(
imageDTO.board_id ?? 'none'
)(getState());
imageDTO.board_id ?? 'none'
)(getState());
const isCacheFullyPopulated =
currentCache.data && currentCache.data.ids.length >= (total ?? 0);
@ -838,11 +846,11 @@ export const imagesApi = api.injectEndpoints({
imageDTO.image_category
)
? boardsApi.endpoints.getBoardImagesTotal.select(
imageDTO.board_id ?? 'none'
)(getState())
imageDTO.board_id ?? 'none'
)(getState())
: boardsApi.endpoints.getBoardAssetsTotal.select(
imageDTO.board_id ?? 'none'
)(getState());
imageDTO.board_id ?? 'none'
)(getState());
const isCacheFullyPopulated =
currentCache.data && currentCache.data.ids.length >= (total ?? 0);
@ -889,18 +897,25 @@ export const imagesApi = api.injectEndpoints({
board_id,
},
}),
invalidatesTags: (result, error, { board_id }) => [
// update the destination board
{ type: 'Board', id: board_id ?? 'none' },
// update old board totals
{ type: 'BoardImagesTotal', id: board_id ?? 'none' },
{ type: 'BoardAssetsTotal', id: board_id ?? 'none' },
// update the no_board totals
{ type: 'BoardImagesTotal', id: 'none' },
{ type: 'BoardAssetsTotal', id: 'none' },
],
invalidatesTags: (result, error, { imageDTOs, board_id }) => {
//assume all images are being moved from one board for now
const oldBoardId = imageDTOs[0]?.board_id;
return [
// update the destination board
{ type: 'Board', id: board_id ?? 'none' },
// update new board totals
{ type: 'BoardImagesTotal', id: board_id ?? 'none' },
{ type: 'BoardAssetsTotal', id: board_id ?? 'none' },
// update old board totals
{ type: 'BoardImagesTotal', id: oldBoardId ?? 'none' },
{ type: 'BoardAssetsTotal', id: oldBoardId ?? 'none' },
// update the no_board totals
{ type: 'BoardImagesTotal', id: 'none' },
{ type: 'BoardAssetsTotal', id: 'none' },
]
},
async onQueryStarted(
{ board_id, imageDTOs },
{ board_id: new_board_id, imageDTOs },
{ dispatch, queryFulfilled, getState }
) {
try {
@ -920,7 +935,7 @@ export const imagesApi = api.injectEndpoints({
'getImageDTO',
image_name,
(draft) => {
draft.board_id = board_id;
draft.board_id = new_board_id;
}
)
);
@ -946,7 +961,7 @@ export const imagesApi = api.injectEndpoints({
);
const queryArgs = {
board_id,
board_id: new_board_id,
categories,
};
@ -954,25 +969,27 @@ export const imagesApi = api.injectEndpoints({
queryArgs
)(getState());
const { data: total } = IMAGE_CATEGORIES.includes(
const { data: previousTotal } = IMAGE_CATEGORIES.includes(
imageDTO.image_category
)
? boardsApi.endpoints.getBoardImagesTotal.select(
imageDTO.board_id ?? 'none'
)(getState())
new_board_id ?? 'none'
)(getState())
: boardsApi.endpoints.getBoardAssetsTotal.select(
imageDTO.board_id ?? 'none'
)(getState());
new_board_id ?? 'none'
)(getState());
const isCacheFullyPopulated =
currentCache.data && currentCache.data.ids.length >= (total ?? 0);
currentCache.data && currentCache.data.ids.length >= (previousTotal ?? 0);
const isInDateRange = getIsImageInDateRange(
const isInDateRange = (previousTotal || 0) >= IMAGE_LIMIT ? getIsImageInDateRange(
currentCache.data,
imageDTO
);
) : true;
if (isCacheFullyPopulated || isInDateRange) {
console.log("upserting")
// *upsert* to $cache
dispatch(
imagesApi.util.updateQueryData(
@ -981,7 +998,7 @@ export const imagesApi = api.injectEndpoints({
(draft) => {
imagesAdapter.upsertOne(draft, {
...imageDTO,
board_id,
board_id: new_board_id,
});
}
)
@ -1088,19 +1105,19 @@ export const imagesApi = api.injectEndpoints({
imageDTO.image_category
)
? boardsApi.endpoints.getBoardImagesTotal.select(
imageDTO.board_id ?? 'none'
)(getState())
imageDTO.board_id ?? 'none'
)(getState())
: boardsApi.endpoints.getBoardAssetsTotal.select(
imageDTO.board_id ?? 'none'
)(getState());
imageDTO.board_id ?? 'none'
)(getState());
const isCacheFullyPopulated =
currentCache.data && currentCache.data.ids.length >= (total ?? 0);
const isInDateRange = getIsImageInDateRange(
const isInDateRange = (total || 0) >= IMAGE_LIMIT ? getIsImageInDateRange(
currentCache.data,
imageDTO
);
) : true;
if (isCacheFullyPopulated || isInDateRange) {
// *upsert* to $cache
@ -1111,7 +1128,7 @@ export const imagesApi = api.injectEndpoints({
(draft) => {
imagesAdapter.upsertOne(draft, {
...imageDTO,
board_id: undefined,
board_id: "none",
});
}
)