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