Skip to content

Commit

Permalink
Fix/library settings global update categories empty dialog after upda…
Browse files Browse the repository at this point in the history
…ting (#361)

* Prevent dialog not to showing any categories after updating them

After updating to react v18 the dialog categories were empty after updating the category changes

* Immediately close the dialog after starting update

Catalog was open as long as the update was running

* Remove unnecessary state
  • Loading branch information
schroda authored Jun 9, 2023
1 parent 63c1ac9 commit 26b48f1
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/screens/settings/LibrarySettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,13 @@ export default function LibrarySettings() {
setAction(null);
}, [t]);

const { data: categories, isLoading, error: requestError, mutate } = requestManager.useGetCategories();

const [currentCategories, setCurrentCategories] = useState<ICategory[]>(categories ?? []); // categories to check if response categories changed
const [dialogCategories, setDialogCategories] = useState<ICategory[]>(categories ?? []); // categories that are shown and updated in the dialog
const { data: categories = [], error: requestError, mutate } = requestManager.useGetCategories();
const [dialogCategories, setDialogCategories] = useState<ICategory[]>(categories);
const [isDialogOpen, setIsDialogOpen] = useState(false);

const retrievedCategoriesChanged = !isLoading && categories?.length && categories !== currentCategories;
if (retrievedCategoriesChanged) {
setCurrentCategories(categories);
useEffect(() => {
setDialogCategories(categories);
}
}, [categories]);

const unsetCategories: ICategory[] =
categories?.filter((category) => category.includeInUpdate === IncludeInGlobalUpdate.UNSET) ?? [];
Expand Down Expand Up @@ -115,7 +111,7 @@ export default function LibrarySettings() {

const updateCategories = async () => {
const categoriesToUpdate = dialogCategories.filter((category) => {
const currentCategory = currentCategories.find((currCategory) => currCategory.id === category.id);
const currentCategory = categories.find((currCategory) => currCategory.id === category.id);

if (!currentCategory) {
return false;
Expand All @@ -124,17 +120,14 @@ export default function LibrarySettings() {
return currentCategory.includeInUpdate !== category.includeInUpdate;
});

setIsDialogOpen(false);

try {
await Promise.all(categoriesToUpdate.map((category) => updateCategory(category)));
mutate([...dialogCategories], { revalidate: false });
} catch (error) {
makeToast(t('global.error.label.failed_to_save_changes'), 'error');
} finally {
setIsDialogOpen(false);

if (categoriesToUpdate.length) {
setDialogCategories([]);
mutate([...dialogCategories], { revalidate: false });
}
mutate([...categories]);
}
};

Expand Down

0 comments on commit 26b48f1

Please sign in to comment.