B: Fix library updates

This commit is contained in:
Ivan 2025-04-15 13:16:24 +03:00
parent dbceac0a6d
commit 40314dbb63
7 changed files with 21 additions and 7 deletions

View File

@ -7,6 +7,11 @@ import { usePreferencesStore } from '@/stores/preferences';
import { libraryApi } from './api';
export function useLibraryListKey() {
const adminMode = usePreferencesStore(state => state.adminMode);
return libraryApi.getLibraryQueryOptions({ isAdmin: adminMode }).queryKey;
}
export function useLibrarySuspense() {
const adminMode = usePreferencesStore(state => state.adminMode);
const { user } = useAuthSuspense();

View File

@ -7,9 +7,11 @@ import { KEYS } from '@/backend/configuration';
import { libraryApi } from './api';
import { type AccessPolicy, type ILibraryItem } from './types';
import { useLibraryListKey } from './use-library';
export const useSetAccessPolicy = () => {
const client = useQueryClient();
const libraryKey = useLibraryListKey();
const mutation = useMutation({
mutationKey: [KEYS.global_mutation, libraryApi.baseKey, 'set-location'],
mutationFn: libraryApi.setAccessPolicy,
@ -36,7 +38,7 @@ export const useSetAccessPolicy = () => {
client.setQueryData(rsKey, (prev: IRSFormDTO | undefined) =>
!prev ? undefined : { ...prev, access_policy: variables.policy }
);
client.setQueryData(libraryApi.libraryListKey, (prev: ILibraryItem[] | undefined) =>
client.setQueryData(libraryKey, (prev: ILibraryItem[] | undefined) =>
prev?.map(item => (item.id === variables.itemID ? { ...item, access_policy: variables.policy } : item))
);
},

View File

@ -7,9 +7,11 @@ import { KEYS } from '@/backend/configuration';
import { libraryApi } from './api';
import { type ILibraryItem } from './types';
import { useLibraryListKey } from './use-library';
export const useSetLocation = () => {
const client = useQueryClient();
const libraryKey = useLibraryListKey();
const mutation = useMutation({
mutationKey: [KEYS.global_mutation, libraryApi.baseKey, 'set-location'],
mutationFn: libraryApi.setLocation,
@ -36,7 +38,7 @@ export const useSetLocation = () => {
client.setQueryData(rsKey, (prev: IRSFormDTO | undefined) =>
!prev ? undefined : { ...prev, location: variables.location }
);
client.setQueryData(libraryApi.libraryListKey, (prev: ILibraryItem[] | undefined) =>
client.setQueryData(libraryKey, (prev: ILibraryItem[] | undefined) =>
prev?.map(item => (item.id === variables.itemID ? { ...item, location: variables.location } : item))
);
},

View File

@ -7,9 +7,11 @@ import { KEYS } from '@/backend/configuration';
import { libraryApi } from './api';
import { type ILibraryItem } from './types';
import { useLibraryListKey } from './use-library';
export const useSetOwner = () => {
const client = useQueryClient();
const libraryKey = useLibraryListKey();
const mutation = useMutation({
mutationKey: [KEYS.global_mutation, libraryApi.baseKey, 'set-owner'],
mutationFn: libraryApi.setOwner,
@ -36,7 +38,7 @@ export const useSetOwner = () => {
client.setQueryData(rsKey, (prev: IRSFormDTO | undefined) =>
!prev ? undefined : { ...prev, owner: variables.owner }
);
client.setQueryData(libraryApi.libraryListKey, (prev: ILibraryItem[] | undefined) =>
client.setQueryData(libraryKey, (prev: ILibraryItem[] | undefined) =>
prev?.map(item => (item.id === variables.itemID ? { ...item, owner: variables.owner } : item))
);
},

View File

@ -7,9 +7,11 @@ import { KEYS } from '@/backend/configuration';
import { libraryApi } from './api';
import { type ILibraryItem, type IUpdateLibraryItemDTO, LibraryItemType } from './types';
import { useLibraryListKey } from './use-library';
export const useUpdateItem = () => {
const client = useQueryClient();
const libraryKey = useLibraryListKey();
const mutation = useMutation({
mutationKey: [KEYS.global_mutation, libraryApi.baseKey, 'update-item'],
mutationFn: libraryApi.updateItem,
@ -18,7 +20,7 @@ export const useUpdateItem = () => {
data.item_type === LibraryItemType.RSFORM
? KEYS.composite.rsItem({ itemID: data.id })
: KEYS.composite.ossItem({ itemID: data.id });
client.setQueryData(libraryApi.libraryListKey, (prev: ILibraryItem[] | undefined) =>
client.setQueryData(libraryKey, (prev: ILibraryItem[] | undefined) =>
prev?.map(item => (item.id === data.id ? data : item))
);
client.setQueryData(itemKey, (prev: IRSFormDTO | IOperationSchemaDTO | undefined) =>

View File

@ -1,14 +1,15 @@
import { useQueryClient } from '@tanstack/react-query';
import { libraryApi } from './api';
import { type ILibraryItem } from './types';
import { useLibraryListKey } from './use-library';
export function useUpdateTimestamp() {
const client = useQueryClient();
const libraryKey = useLibraryListKey();
return {
updateTimestamp: (target: number) =>
client.setQueryData(
libraryApi.libraryListKey, //
libraryKey, //
(prev: ILibraryItem[] | undefined) =>
prev?.map(item => (item.id === target ? { ...item, time_update: Date() } : item))
)

View File

@ -9,7 +9,7 @@ export type IUserProfile = z.infer<typeof schemaUserProfile>;
/** Represents user reference information. */
export type IUserInfo = z.infer<typeof schemaUserInfo>;
/** Represents signup data, used to create new users. */
/** Represents signup data, used to create users. */
export type IUserSignupDTO = z.infer<typeof schemaUserSignup>;
/** Represents user data, intended to update user profile in persistent storage. */