feat(ui): add getImageDTO imperative RTKQ helper

This commit is contained in:
psychedelicious 2024-05-02 16:27:58 +10:00 committed by Kent Keirsey
parent 2cde8a643e
commit 4cd78b9478

View File

@ -1,5 +1,6 @@
import type { EntityState, Update } from '@reduxjs/toolkit'; import type { EntityState, Update } from '@reduxjs/toolkit';
import type { PatchCollection } from '@reduxjs/toolkit/dist/query/core/buildThunks'; import type { PatchCollection } from '@reduxjs/toolkit/dist/query/core/buildThunks';
import { getStore } from 'app/store/nanostores/store';
import type { JSONObject } from 'common/types'; import type { JSONObject } from 'common/types';
import type { BoardId } from 'features/gallery/store/types'; import type { BoardId } from 'features/gallery/store/types';
import { ASSETS_CATEGORIES, IMAGE_CATEGORIES, IMAGE_LIMIT } from 'features/gallery/store/types'; import { ASSETS_CATEGORIES, IMAGE_CATEGORIES, IMAGE_LIMIT } from 'features/gallery/store/types';
@ -1319,3 +1320,22 @@ export const {
useUnstarImagesMutation, useUnstarImagesMutation,
useBulkDownloadImagesMutation, useBulkDownloadImagesMutation,
} = imagesApi; } = imagesApi;
/**
* Imperative RTKQ helper to fetch an ImageDTO.
* @param image_name The name of the image to fetch
* @param forceRefetch Whether to force a refetch of the image
* @returns
*/
export const getImageDTO = async (image_name: string, forceRefetch?: boolean): Promise<ImageDTO | null> => {
const options = {
subscribe: false,
forceRefetch,
};
const req = getStore().dispatch(imagesApi.endpoints.getImageDTO.initiate(image_name, options));
try {
return await req.unwrap();
} catch {
return null;
}
};