fix(ui): clear selection when deleting last image in board

This commit is contained in:
psychedelicious 2024-07-14 21:08:33 +10:00
parent 3ecd14f394
commit 5cbe9fafb2

View File

@ -136,7 +136,12 @@ export const addImageDeletionListeners = (startAppListening: AppStartListening)
if (data) { if (data) {
const deletedImageIndex = data.items.findIndex((i) => i.image_name === imageDTO.image_name); const deletedImageIndex = data.items.findIndex((i) => i.image_name === imageDTO.image_name);
const nextImage = data.items[deletedImageIndex + 1] ?? data.items[0] ?? null; const nextImage = data.items[deletedImageIndex + 1] ?? data.items[0] ?? null;
dispatch(imageSelected(nextImage)); if (nextImage?.image_name === imageDTO.image_name) {
// If the next image is the same as the deleted one, it means it was the last image, reset selection
dispatch(imageSelected(null));
} else {
dispatch(imageSelected(nextImage));
}
} }
} }
@ -176,6 +181,8 @@ export const addImageDeletionListeners = (startAppListening: AppStartListening)
const queryArgs = selectListImagesQueryArgs(state); const queryArgs = selectListImagesQueryArgs(state);
const { data } = imagesApi.endpoints.listImages.select(queryArgs)(state); const { data } = imagesApi.endpoints.listImages.select(queryArgs)(state);
if (data) { if (data) {
// When we delete multiple images, we clear the selection. Then, the the next time we load images, we will
// select the first one. This is handled below in the listener for `imagesApi.endpoints.listImages.matchFulfilled`.
dispatch(imageSelected(null)); dispatch(imageSelected(null));
} }
} }