Introduce new batch metadata update function
This commit is contained in:
@@ -7,7 +7,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { useEffect, useMemo } from 'react';
|
import { useEffect, useMemo } from 'react';
|
||||||
import { requestCategoryMetadataUpdate } from '@/features/metadata/services/MetadataUpdater.ts';
|
import {
|
||||||
|
requestCategoryMetadataUpdate,
|
||||||
|
requestBatchCategoryMetadataUpdate,
|
||||||
|
} from '@/features/metadata/services/MetadataUpdater.ts';
|
||||||
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
||||||
import { LibraryOptions } from '@/features/library/Library.types.ts';
|
import { LibraryOptions } from '@/features/library/Library.types.ts';
|
||||||
import { CategoryIdInfo, CategoryMetadataKeys, ICategoryMetadata } from '@/features/category/Category.types.ts';
|
import { CategoryIdInfo, CategoryMetadataKeys, ICategoryMetadata } from '@/features/category/Category.types.ts';
|
||||||
@@ -85,6 +88,27 @@ export const updateCategoryMetadata = async <
|
|||||||
update: [[metadataKey, convertAppMetadataToGqlMetadata({ [metadataKey]: value })[metadataKey]]],
|
update: [[metadataKey, convertAppMetadataToGqlMetadata({ [metadataKey]: value })[metadataKey]]],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const batchUpdateCategoryMetadata = async <
|
||||||
|
MetadataKeys extends CategoryMetadataKeys = CategoryMetadataKeys,
|
||||||
|
MetadataKey extends MetadataKeys = MetadataKeys,
|
||||||
|
>(
|
||||||
|
updates: Array<{
|
||||||
|
categories: (CategoryIdInfo & GqlMetaHolder)[];
|
||||||
|
entries: Array<{ metadataKey: MetadataKey; value: ICategoryMetadata[MetadataKey] }>;
|
||||||
|
}>,
|
||||||
|
): Promise<void> =>
|
||||||
|
requestBatchCategoryMetadataUpdate(
|
||||||
|
updates.map(({ categories, entries }) => ({
|
||||||
|
categories,
|
||||||
|
options: {
|
||||||
|
update: entries.map(({ metadataKey, value }) => [
|
||||||
|
metadataKey,
|
||||||
|
convertAppMetadataToGqlMetadata({ [metadataKey]: value })[metadataKey],
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
|
||||||
export const createUpdateCategoryMetadata =
|
export const createUpdateCategoryMetadata =
|
||||||
<Settings extends CategoryMetadataKeys>(
|
<Settings extends CategoryMetadataKeys>(
|
||||||
category: CategoryIdInfo & GqlMetaHolder,
|
category: CategoryIdInfo & GqlMetaHolder,
|
||||||
|
|||||||
@@ -17,7 +17,10 @@ import {
|
|||||||
MetadataHolder,
|
MetadataHolder,
|
||||||
} from '@/features/metadata/Metadata.types.ts';
|
} from '@/features/metadata/Metadata.types.ts';
|
||||||
import { convertFromGqlMeta } from '@/features/metadata/services/MetadataConverter.ts';
|
import { convertFromGqlMeta } from '@/features/metadata/services/MetadataConverter.ts';
|
||||||
import { requestMangaMetadataUpdate } from '@/features/metadata/services/MetadataUpdater.ts';
|
import {
|
||||||
|
requestMangaMetadataUpdate,
|
||||||
|
requestBatchMangaMetadataUpdate,
|
||||||
|
} from '@/features/metadata/services/MetadataUpdater.ts';
|
||||||
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
||||||
|
|
||||||
const DEFAULT_MANGA_METADATA: MangaMetadata = {
|
const DEFAULT_MANGA_METADATA: MangaMetadata = {
|
||||||
@@ -73,6 +76,27 @@ export const updateMangaMetadata = async <
|
|||||||
update: [[metadataKey, convertAppMetadataToGqlMetadata({ [metadataKey]: value })[metadataKey]]],
|
update: [[metadataKey, convertAppMetadataToGqlMetadata({ [metadataKey]: value })[metadataKey]]],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const batchUpdateMangaMetadata = async <
|
||||||
|
MetadataKeys extends MangaMetadataKeys = MangaMetadataKeys,
|
||||||
|
MetadataKey extends MetadataKeys = MetadataKeys,
|
||||||
|
>(
|
||||||
|
updates: Array<{
|
||||||
|
mangas: (MangaIdInfo & GqlMetaHolder)[];
|
||||||
|
entries: Array<{ metadataKey: MetadataKey; value: MangaMetadata[MetadataKey] }>;
|
||||||
|
}>,
|
||||||
|
): Promise<void> =>
|
||||||
|
requestBatchMangaMetadataUpdate(
|
||||||
|
updates.map(({ mangas, entries }) => ({
|
||||||
|
mangas,
|
||||||
|
options: {
|
||||||
|
update: entries.map(({ metadataKey, value }) => [
|
||||||
|
metadataKey,
|
||||||
|
convertAppMetadataToGqlMetadata({ [metadataKey]: value })[metadataKey],
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
|
||||||
export const createUpdateMangaMetadata =
|
export const createUpdateMangaMetadata =
|
||||||
<Settings extends MangaMetadataKeys>(
|
<Settings extends MangaMetadataKeys>(
|
||||||
manga: MangaIdInfo & GqlMetaHolder,
|
manga: MangaIdInfo & GqlMetaHolder,
|
||||||
|
|||||||
@@ -37,7 +37,13 @@ type MetadataUpdateOptions = {
|
|||||||
isMetadataKey?: boolean;
|
isMetadataKey?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
const requestMetadataUpdate = async (
|
type ProcessedEntityMetadata = {
|
||||||
|
updateMetas: MetaInput[];
|
||||||
|
deleteKeys: string[];
|
||||||
|
migrateMetas: MetaInput[];
|
||||||
|
};
|
||||||
|
|
||||||
|
const processEntityMetadata = (
|
||||||
metadataHolder: GqlMetaHolder,
|
metadataHolder: GqlMetaHolder,
|
||||||
holderType: MetadataHolderType,
|
holderType: MetadataHolderType,
|
||||||
{
|
{
|
||||||
@@ -47,7 +53,7 @@ const requestMetadataUpdate = async (
|
|||||||
keyPrefixes,
|
keyPrefixes,
|
||||||
isMetadataKey = false,
|
isMetadataKey = false,
|
||||||
}: MetadataUpdateOptions,
|
}: MetadataUpdateOptions,
|
||||||
): Promise<void> => {
|
): ProcessedEntityMetadata => {
|
||||||
if (keysToMigrate?.length > 1 || (keysToMigrate?.length === 1 && keysToMigrate[0][0] !== 'migration')) {
|
if (keysToMigrate?.length > 1 || (keysToMigrate?.length === 1 && keysToMigrate[0][0] !== 'migration')) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`requestMetadataUpdate: "migrate" option must only contain a single key-value pair with the key "migration"`,
|
`requestMetadataUpdate: "migrate" option must only contain a single key-value pair with the key "migration"`,
|
||||||
@@ -92,77 +98,200 @@ const requestMetadataUpdate = async (
|
|||||||
value: `${value}`,
|
value: `${value}`,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
return { updateMetas: allUpdateMetas, deleteKeys: uniqueDeleteKeys, migrateMetas };
|
||||||
|
};
|
||||||
|
|
||||||
|
type ProcessedEntry = ProcessedEntityMetadata & { metadataHolder: GqlMetaHolder };
|
||||||
|
|
||||||
|
const groupByIdenticalMetas = <Id extends number | string>(
|
||||||
|
processed: Array<ProcessedEntry & { metadataHolder: { id: Id } }>,
|
||||||
|
): {
|
||||||
|
updateGroups: Array<{ ids: Id[]; metas: MetaInput[] }>;
|
||||||
|
deleteGroups: Array<{ ids: Id[]; keys: string[] }>;
|
||||||
|
migrateGroups: Array<{ ids: Id[]; metas: MetaInput[] }>;
|
||||||
|
} => {
|
||||||
|
const updateMap = new Map<string, { ids: Id[]; metas: MetaInput[] }>();
|
||||||
|
const deleteMap = new Map<string, { ids: Id[]; keys: string[] }>();
|
||||||
|
const migrateMap = new Map<string, { ids: Id[]; metas: MetaInput[] }>();
|
||||||
|
|
||||||
|
for (const entry of processed) {
|
||||||
|
const { id } = entry.metadataHolder;
|
||||||
|
|
||||||
|
if (entry.updateMetas.length > 0) {
|
||||||
|
const key = JSON.stringify(entry.updateMetas);
|
||||||
|
const existing = updateMap.get(key);
|
||||||
|
if (existing) {
|
||||||
|
existing.ids.push(id);
|
||||||
|
} else {
|
||||||
|
updateMap.set(key, { ids: [id], metas: entry.updateMetas });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entry.deleteKeys.length > 0) {
|
||||||
|
const key = JSON.stringify(entry.deleteKeys);
|
||||||
|
const existing = deleteMap.get(key);
|
||||||
|
if (existing) {
|
||||||
|
existing.ids.push(id);
|
||||||
|
} else {
|
||||||
|
deleteMap.set(key, { ids: [id], keys: entry.deleteKeys });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entry.migrateMetas.length > 0) {
|
||||||
|
const key = JSON.stringify(entry.migrateMetas);
|
||||||
|
const existing = migrateMap.get(key);
|
||||||
|
if (existing) {
|
||||||
|
existing.ids.push(id);
|
||||||
|
} else {
|
||||||
|
migrateMap.set(key, { ids: [id], metas: entry.migrateMetas });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
updateGroups: [...updateMap.values()],
|
||||||
|
deleteGroups: [...deleteMap.values()],
|
||||||
|
migrateGroups: [...migrateMap.values()],
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const createEntityMetaInput = <Key extends string, Id extends number | string>(
|
||||||
|
processed: ProcessedEntry[],
|
||||||
|
idKey: Key,
|
||||||
|
) => {
|
||||||
|
const { updateGroups, deleteGroups, migrateGroups } = groupByIdenticalMetas(
|
||||||
|
processed as Array<ProcessedEntry & { metadataHolder: { id: Id } }>,
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
updateInput: {
|
||||||
|
items: updateGroups.map(
|
||||||
|
({ ids, metas }) => ({ [idKey]: ids, metas }) as Record<Key, Id[]> & { metas: MetaInput[] },
|
||||||
|
),
|
||||||
|
},
|
||||||
|
deleteInput: {
|
||||||
|
items: deleteGroups.map(
|
||||||
|
({ ids, keys }) => ({ [idKey]: ids, keys }) as Record<Key, Id[]> & { keys: string[] },
|
||||||
|
),
|
||||||
|
},
|
||||||
|
migrateInput: {
|
||||||
|
items: migrateGroups.map(
|
||||||
|
({ ids, metas }) => ({ [idKey]: ids, metas }) as Record<Key, Id[]> & { metas: MetaInput[] },
|
||||||
|
),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const requestBatchMetadataUpdate = async (
|
||||||
|
holderType: MetadataHolderType,
|
||||||
|
entries: Array<{ metadataHolders: GqlMetaHolder[]; options: MetadataUpdateOptions }>,
|
||||||
|
): Promise<void> => {
|
||||||
|
if (entries.length === 0) return;
|
||||||
|
|
||||||
|
const processed = entries.flatMap(({ metadataHolders, options }) =>
|
||||||
|
metadataHolders.map((metadataHolder) => ({
|
||||||
|
metadataHolder,
|
||||||
|
...processEntityMetadata(metadataHolder, holderType, options),
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
|
||||||
switch (holderType) {
|
switch (holderType) {
|
||||||
case 'category': {
|
case 'global': {
|
||||||
const categoryId = (metadataHolder as CategoryIdInfo).id;
|
const withUpdates = processed.filter(({ updateMetas }) => updateMetas.length > 0);
|
||||||
await requestManager.updateCategoryMeta({
|
const withDeletes = processed.filter(({ deleteKeys }) => deleteKeys.length > 0);
|
||||||
updateInput: { items: [{ categoryIds: [categoryId], metas: allUpdateMetas }] },
|
const withMigrations = processed.filter(({ migrateMetas }) => migrateMetas.length > 0);
|
||||||
deleteInput: { items: [{ categoryIds: [categoryId], keys: uniqueDeleteKeys }] },
|
|
||||||
migrateInput: { items: [{ categoryIds: [categoryId], metas: migrateMetas }] },
|
|
||||||
}).response;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'chapter': {
|
|
||||||
const chapterId = (metadataHolder as ChapterIdInfo).id;
|
|
||||||
await requestManager.updateChapterMeta({
|
|
||||||
updateInput: { items: [{ chapterIds: [chapterId], metas: allUpdateMetas }] },
|
|
||||||
deleteInput: { items: [{ chapterIds: [chapterId], keys: uniqueDeleteKeys }] },
|
|
||||||
migrateInput: { items: [{ chapterIds: [chapterId], metas: migrateMetas }] },
|
|
||||||
}).response;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'global':
|
|
||||||
await requestManager.updateGlobalMeta({
|
await requestManager.updateGlobalMeta({
|
||||||
updateInput: { metas: allUpdateMetas },
|
updateInput: { metas: withUpdates.flatMap(({ updateMetas }) => updateMetas) },
|
||||||
deleteInput: { keys: uniqueDeleteKeys },
|
deleteInput: { keys: withDeletes.flatMap(({ deleteKeys }) => deleteKeys) },
|
||||||
migrateInput: { metas: migrateMetas },
|
migrateInput: { metas: withMigrations.flatMap(({ migrateMetas }) => migrateMetas) },
|
||||||
}).response;
|
|
||||||
break;
|
|
||||||
case 'manga': {
|
|
||||||
const mangaId = (metadataHolder as MangaIdInfo).id;
|
|
||||||
await requestManager.updateMangaMeta({
|
|
||||||
updateInput: { items: [{ mangaIds: [mangaId], metas: allUpdateMetas }] },
|
|
||||||
deleteInput: { items: [{ mangaIds: [mangaId], keys: uniqueDeleteKeys }] },
|
|
||||||
migrateInput: { items: [{ mangaIds: [mangaId], metas: migrateMetas }] },
|
|
||||||
}).response;
|
}).response;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'source': {
|
case 'category':
|
||||||
const sourceId = (metadataHolder as SourceIdInfo).id;
|
await requestManager.updateCategoryMeta(
|
||||||
await requestManager.updateSourceMeta({
|
createEntityMetaInput<'categoryIds', number>(processed, 'categoryIds'),
|
||||||
updateInput: { items: [{ sourceIds: [sourceId], metas: allUpdateMetas }] },
|
).response;
|
||||||
deleteInput: { items: [{ sourceIds: [sourceId], keys: uniqueDeleteKeys }] },
|
break;
|
||||||
migrateInput: { items: [{ sourceIds: [sourceId], metas: migrateMetas }] },
|
case 'chapter':
|
||||||
}).response;
|
await requestManager.updateChapterMeta(createEntityMetaInput<'chapterIds', number>(processed, 'chapterIds'))
|
||||||
|
.response;
|
||||||
|
break;
|
||||||
|
case 'manga':
|
||||||
|
await requestManager.updateMangaMeta(createEntityMetaInput<'mangaIds', number>(processed, 'mangaIds'))
|
||||||
|
.response;
|
||||||
|
break;
|
||||||
|
case 'source':
|
||||||
|
await requestManager.updateSourceMeta(createEntityMetaInput<'sourceIds', string>(processed, 'sourceIds'))
|
||||||
|
.response;
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
throw new Error(`requestMetadataUpdate: unknown holderType "${holderType}"`);
|
throw new Error(`requestBatchMetadataUpdate: unknown holderType "${holderType}"`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const requestBatchServerMetadataUpdate = async (
|
||||||
|
entries: Array<{ options: MetadataUpdateOptions }>,
|
||||||
|
): Promise<void> =>
|
||||||
|
requestBatchMetadataUpdate(
|
||||||
|
'global',
|
||||||
|
entries.map(({ options }) => ({ metadataHolders: [{}], options })),
|
||||||
|
);
|
||||||
|
|
||||||
|
export const requestBatchMangaMetadataUpdate = async (
|
||||||
|
entries: Array<{ mangas: (MangaIdInfo & GqlMetaHolder)[]; options: MetadataUpdateOptions }>,
|
||||||
|
): Promise<void> =>
|
||||||
|
requestBatchMetadataUpdate(
|
||||||
|
'manga',
|
||||||
|
entries.map(({ mangas, options }) => ({ metadataHolders: mangas, options })),
|
||||||
|
);
|
||||||
|
|
||||||
|
export const requestBatchChapterMetadataUpdate = async (
|
||||||
|
entries: Array<{ chapters: (ChapterIdInfo & GqlMetaHolder)[]; options: MetadataUpdateOptions }>,
|
||||||
|
): Promise<void> =>
|
||||||
|
requestBatchMetadataUpdate(
|
||||||
|
'chapter',
|
||||||
|
entries.map(({ chapters, options }) => ({ metadataHolders: chapters, options })),
|
||||||
|
);
|
||||||
|
|
||||||
|
export const requestBatchCategoryMetadataUpdate = async (
|
||||||
|
entries: Array<{ categories: (CategoryIdInfo & GqlMetaHolder)[]; options: MetadataUpdateOptions }>,
|
||||||
|
): Promise<void> =>
|
||||||
|
requestBatchMetadataUpdate(
|
||||||
|
'category',
|
||||||
|
entries.map(({ categories, options }) => ({ metadataHolders: categories, options })),
|
||||||
|
);
|
||||||
|
|
||||||
|
export const requestBatchSourceMetadataUpdate = async (
|
||||||
|
entries: Array<{ sources: (SourceIdInfo & GqlMetaHolder)[]; options: MetadataUpdateOptions }>,
|
||||||
|
): Promise<void> =>
|
||||||
|
requestBatchMetadataUpdate(
|
||||||
|
'source',
|
||||||
|
entries.map(({ sources, options }) => ({ metadataHolders: sources, options })),
|
||||||
|
);
|
||||||
|
|
||||||
export const requestServerMetadataUpdate = async (options: MetadataUpdateOptions): Promise<void> =>
|
export const requestServerMetadataUpdate = async (options: MetadataUpdateOptions): Promise<void> =>
|
||||||
requestMetadataUpdate({}, 'global', options);
|
requestBatchServerMetadataUpdate([{ options }]);
|
||||||
|
|
||||||
export const requestMangaMetadataUpdate = async (
|
export const requestMangaMetadataUpdate = async (
|
||||||
manga: MangaIdInfo & GqlMetaHolder,
|
manga: MangaIdInfo & GqlMetaHolder,
|
||||||
options: MetadataUpdateOptions,
|
options: MetadataUpdateOptions,
|
||||||
): Promise<void> => requestMetadataUpdate(manga, 'manga', options);
|
): Promise<void> => requestBatchMangaMetadataUpdate([{ mangas: [manga], options }]);
|
||||||
|
|
||||||
export const requestChapterMetadataUpdate = async (
|
export const requestChapterMetadataUpdate = async (
|
||||||
chapter: ChapterIdInfo & GqlMetaHolder,
|
chapter: ChapterIdInfo & GqlMetaHolder,
|
||||||
options: MetadataUpdateOptions,
|
options: MetadataUpdateOptions,
|
||||||
): Promise<void> => requestMetadataUpdate(chapter, 'chapter', options);
|
): Promise<void> => requestBatchChapterMetadataUpdate([{ chapters: [chapter], options }]);
|
||||||
|
|
||||||
export const requestCategoryMetadataUpdate = async (
|
export const requestCategoryMetadataUpdate = async (
|
||||||
category: CategoryIdInfo & GqlMetaHolder,
|
category: CategoryIdInfo & GqlMetaHolder,
|
||||||
options: MetadataUpdateOptions,
|
options: MetadataUpdateOptions,
|
||||||
): Promise<void> => requestMetadataUpdate(category, 'category', options);
|
): Promise<void> => requestBatchCategoryMetadataUpdate([{ categories: [category], options }]);
|
||||||
|
|
||||||
export const requestSourceMetadataUpdate = async (
|
export const requestSourceMetadataUpdate = async (
|
||||||
source: SourceIdInfo & GqlMetaHolder,
|
source: SourceIdInfo & GqlMetaHolder,
|
||||||
options: MetadataUpdateOptions,
|
options: MetadataUpdateOptions,
|
||||||
): Promise<void> => requestMetadataUpdate(source, 'source', options);
|
): Promise<void> => requestBatchSourceMetadataUpdate([{ sources: [source], options }]);
|
||||||
|
|
||||||
export const getMetadataUpdateFunction = (
|
export const getMetadataUpdateFunction = (
|
||||||
type: MetadataHolderType,
|
type: MetadataHolderType,
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ import {
|
|||||||
SourceNameInfo,
|
SourceNameInfo,
|
||||||
} from '@/features/source/Source.types.ts';
|
} from '@/features/source/Source.types.ts';
|
||||||
import { Sources } from '@/features/source/services/Sources';
|
import { Sources } from '@/features/source/services/Sources';
|
||||||
import { getSourceMetadata, updateSourceMetadata } from '@/features/source/services/SourceMetadata.ts';
|
import { batchUpdateSourceMetadata, getSourceMetadata } from '@/features/source/services/SourceMetadata.ts';
|
||||||
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
||||||
import { ListCardAvatar } from '@/base/components/lists/cards/ListCardAvatar.tsx';
|
import { ListCardAvatar } from '@/base/components/lists/cards/ListCardAvatar.tsx';
|
||||||
import { makeToast } from '@/base/utils/Toast.ts';
|
import { makeToast } from '@/base/utils/Toast.ts';
|
||||||
@@ -113,10 +113,21 @@ export const SourceLanguageSelect = ({
|
|||||||
const handleOk = () => {
|
const handleOk = () => {
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
|
|
||||||
Promise.all(
|
batchUpdateSourceMetadata(
|
||||||
Object.entries(tmpSourceIdToEnabledState).map(([sourceId, enabled]) =>
|
Object.entries(tmpSourceIdToEnabledState)
|
||||||
updateSourceMetadata(sources.find((source) => source.id === sourceId)!, 'isEnabled', enabled),
|
.map(([sourceId, enabled]) => {
|
||||||
),
|
const source = sources.find((sourceToEnable) => sourceToEnable.id === sourceId);
|
||||||
|
|
||||||
|
if (!source) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
sources: [source],
|
||||||
|
entries: [{ metadataKey: 'isEnabled' as const, value: enabled }],
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.filter((entry) => entry !== null),
|
||||||
).catch((e) => makeToast(t`Failed to save changes`, 'error', getErrorMessage(e)));
|
).catch((e) => makeToast(t`Failed to save changes`, 'error', getErrorMessage(e)));
|
||||||
setTmpSourceIdToEnabledState({});
|
setTmpSourceIdToEnabledState({});
|
||||||
setSelectedLanguages(toUniqueLanguageCodes(tmpSelectedLanguages));
|
setSelectedLanguages(toUniqueLanguageCodes(tmpSelectedLanguages));
|
||||||
|
|||||||
@@ -7,7 +7,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { useEffect, useMemo } from 'react';
|
import { useEffect, useMemo } from 'react';
|
||||||
import { requestSourceMetadataUpdate } from '@/features/metadata/services/MetadataUpdater.ts';
|
import {
|
||||||
|
requestSourceMetadataUpdate,
|
||||||
|
requestBatchSourceMetadataUpdate,
|
||||||
|
} from '@/features/metadata/services/MetadataUpdater.ts';
|
||||||
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
||||||
import { ISourceMetadata, SourceIdInfo, SourceMetadataKeys } from '@/features/source/Source.types.ts';
|
import { ISourceMetadata, SourceIdInfo, SourceMetadataKeys } from '@/features/source/Source.types.ts';
|
||||||
import { convertFromGqlMeta } from '@/features/metadata/services/MetadataConverter.ts';
|
import { convertFromGqlMeta } from '@/features/metadata/services/MetadataConverter.ts';
|
||||||
@@ -55,6 +58,27 @@ export const updateSourceMetadata = async <
|
|||||||
update: [[metadataKey, convertAppMetadataToGqlMetadata({ [metadataKey]: value })[metadataKey]]],
|
update: [[metadataKey, convertAppMetadataToGqlMetadata({ [metadataKey]: value })[metadataKey]]],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const batchUpdateSourceMetadata = async <
|
||||||
|
MetadataKeys extends SourceMetadataKeys = SourceMetadataKeys,
|
||||||
|
MetadataKey extends MetadataKeys = MetadataKeys,
|
||||||
|
>(
|
||||||
|
updates: Array<{
|
||||||
|
sources: (SourceIdInfo & GqlMetaHolder)[];
|
||||||
|
entries: Array<{ metadataKey: MetadataKey; value: ISourceMetadata[MetadataKey] }>;
|
||||||
|
}>,
|
||||||
|
): Promise<void> =>
|
||||||
|
requestBatchSourceMetadataUpdate(
|
||||||
|
updates.map(({ sources, entries }) => ({
|
||||||
|
sources,
|
||||||
|
options: {
|
||||||
|
update: entries.map(({ metadataKey, value }) => [
|
||||||
|
metadataKey,
|
||||||
|
convertAppMetadataToGqlMetadata({ [metadataKey]: value })[metadataKey],
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
|
||||||
export const createUpdateSourceMetadata =
|
export const createUpdateSourceMetadata =
|
||||||
<Settings extends SourceMetadataKeys>(
|
<Settings extends SourceMetadataKeys>(
|
||||||
source: SourceIdInfo & GqlMetaHolder,
|
source: SourceIdInfo & GqlMetaHolder,
|
||||||
|
|||||||
Reference in New Issue
Block a user