{"version":3,"file":"B7TEQkQM.js","sources":["../../../../src/components/dialog/newEditFavoriteList/useNewEditFavoriteList.ts"],"sourcesContent":["import { ErrorIdent } from '~/@types/errorIdents';\nimport { useSecureSessionPost } from '~/composables/dataFetching/genericFetchers';\nimport { useDialogStore, DialogIdent } from '~/stores/useDialogStore';\nimport { useFavLists } from '~/stores/useFavLists';\nimport { useUserContext } from '~/stores/useUserContext';\nimport type {\n Result,\n PostBody as CreateBody,\n} from '@/server/api/[site]/user/favList/createFavList.post';\nimport type { FavoriteList } from '~/server/transformers/shop/favoriteListTransformer';\nimport { handleLoadingError } from '~/utils/handleLoadingError';\n\n/**\n * @INFO\n * list and options must be optional\n * because you can have either one but never both for create or edit\n */\ntype FavListDialogType = {\n list?: FavoriteList;\n options?: {\n items: CreateBody['items'];\n preventNavigateTo?: boolean;\n comingFromMultiselect?: boolean;\n };\n};\n\nexport function useNewEditFavoriteList() {\n const site = useSiteIdent();\n const errors = ref([]);\n const userContext = useUserContext();\n const favListStore = useFavLists();\n const dialogStore = useDialogStore();\n const isLoading = ref(false);\n const { t } = useTrans();\n interface createFavoriteListForm {\n listName: string;\n }\n\n const dialogData = dialogStore.getDialogData(\n DialogIdent.NEW_EDIT_FAVORITE_LIST,\n );\n\n const list = dialogData?.list;\n\n if (!dialogData || dialogData.options) {\n //either i have no dialogData or the dialogData.options to create a new favList\n dialogStore.setHeadline(t('myProducts.dialog.addNewFavList.headline'));\n } else {\n dialogStore.setHeadline(t('myProducts.favLists.settings.renameList'));\n }\n\n async function onSubmit(data: createFavoriteListForm) {\n isLoading.value = true;\n await createFavoriteList(data);\n isLoading.value = false;\n }\n\n async function createFavoriteList(formData: createFavoriteListForm) {\n try {\n isLoading.value = true;\n let result;\n\n if (!list) {\n const createBody = {\n name: formData.listName,\n companyId: userContext?.userData?.customerId,\n } as CreateBody;\n\n if (dialogData?.options?.items) {\n createBody.items = dialogData.options.items;\n }\n\n //create\n result = await useSecureSessionPost(\n `/api/${site}/user/favList/createFavList`,\n createBody,\n );\n\n if (result) {\n if (dialogData?.options?.comingFromMultiselect) {\n dialogStore.closeDialog(DialogIdent.NEW_EDIT_FAVORITE_LIST);\n dialogStore.openDialog(\n DialogIdent.FAV_LIST_MULTI_SELECT_MOVE_ITEMS,\n );\n dialogStore.setHeadline(\n favListStore.multiselect?.selectedItems?.length +\n ' ' +\n t('favLists.multiSelect.moveItems'),\n );\n return;\n }\n const oldFavLists = favListStore.favoriteLists;\n await favListStore.loadLists(false);\n const newFavList = compareFavListsForChanges(\n oldFavLists,\n favListStore.favoriteLists,\n );\n\n if (!dialogData?.options?.preventNavigateTo)\n navigateTo(`/productlists/favorites/${newFavList.id}`);\n }\n } else {\n //edit\n const postData = {\n name: formData.listName,\n companyId: userContext?.userData?.customerId,\n };\n result = await useSecureSessionPost(\n `/api/${site}/user/favList/editFavList`,\n {\n listId: list.id,\n ...postData,\n },\n );\n\n if (result) await favListStore.loadLists();\n }\n\n dialogStore.closeDialog(DialogIdent.NEW_EDIT_FAVORITE_LIST);\n } catch (e: any) {\n errors.value = e.data?.data?.errors ?? ErrorIdent.UNKNOWN_ERROR;\n isLoading.value = false;\n handleLoadingError(e);\n }\n }\n\n return {\n list,\n onSubmit,\n errors,\n isLoading,\n dialogStore,\n };\n}\n\nfunction compareFavListsForChanges(\n oldFavLists: FavoriteList[],\n newFavLists: FavoriteList[],\n): FavoriteList {\n for (const newList of newFavLists) {\n const oldList = oldFavLists.find((list) => list.id === newList.id);\n if (!oldList) {\n return newList;\n }\n }\n}\n"],"names":["useNewEditFavoriteList","site","useSiteIdent","errors","ref","userContext","useUserContext","favListStore","useFavLists","dialogStore","useDialogStore","isLoading","t","useTrans","dialogData","DialogIdent","list","onSubmit","data","createFavoriteList","formData","result","postData","_g","useSecureSessionPost","createBody","_a","_b","_c","_e","_d","oldFavLists","newFavList","compareFavListsForChanges","_f","navigateTo","e","_i","_h","ErrorIdent","handleLoadingError","newFavLists","newList"],"mappings":"iIA0BO,SAASA,GAAyB,CAC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