{"version":3,"file":"profile.slice-Dy8dpNrF.js","sources":["../../../../../../../../app/frontend/entities/profile/models/profile.slice.ts"],"sourcesContent":["import { router } from '@inertiajs/react'\nimport { createAsyncThunk, createSlice } from '@reduxjs/toolkit'\nimport type { PayloadAction } from '@reduxjs/toolkit'\nimport { AxiosError } from 'axios'\nimport i18next, { t } from 'i18next'\n\nimport { getUpdatedURLWithLocale } from '~/app/helpers'\nimport userApi from '~/shared/api/user.api'\n\nimport { showLoginErrors, showRegistrationErrors } from '../lib/utils'\n\nexport type ProfileSliceType = {\n isLoggedIn: boolean\n fullName: string\n email: string\n isAdmin: boolean\n isLoading: boolean\n offerSubscriptions: []\n activeOfferSubscriptions: {\n id: string\n offerId: string\n offerName: string\n state: string\n nextPaymentOn: string\n }[]\n id: string\n}\n\nexport type ProfileRequestType = {\n email: string\n password: string\n}\n\nexport type ProfileRegistrationRequestType = ProfileRequestType & {\n full_name: string\n redirectTo?: string\n}\n\nexport type ProfileConfirmPasswordRequestType = Omit & {\n password_confirmation: string\n reset_password_token: string\n}\n\nexport type ProfileResetPasswordRequestType = Omit\n\nconst initialState: ProfileSliceType = {\n isLoggedIn: false,\n fullName: '',\n email: '',\n isAdmin: false,\n isLoading: true,\n offerSubscriptions: [],\n activeOfferSubscriptions: [],\n id: 'guest',\n}\n\nexport const getMe = createAsyncThunk('profile/me', async () => {\n try {\n const response = await userApi.me()\n return response.data\n } catch (e) {}\n})\n\nexport const logout = createAsyncThunk('profile/logout', async () => {\n const response = await userApi.logout()\n\n return response.data\n})\n\nexport const login = createAsyncThunk('profile/login', async (user: ProfileRequestType) => {\n try {\n const response = await userApi.login(user)\n\n // TODO: Delete authorization header from everywhere\n return { ...response.data, authorization: response.headers.authorization }\n } catch (e) {\n if (e instanceof AxiosError) {\n showLoginErrors(e)\n }\n }\n})\n\nexport const register = createAsyncThunk(\n 'profile/register',\n async (user: ProfileRegistrationRequestType) => {\n try {\n const response = await userApi.register(user)\n router.replace(user.redirectTo)\n\n return { ...response.data, authorization: response.headers.authorization }\n } catch (e) {\n if (e instanceof AxiosError) {\n showRegistrationErrors(e, t('internal_server_error_page.title'))\n }\n }\n }\n)\n\nexport const profileSlice = createSlice({\n name: 'profile',\n initialState,\n reducers: {},\n extraReducers: (builder) => {\n builder.addCase(\n getMe.fulfilled,\n (state, action: PayloadAction<{ user: ProfileSliceType } | null>) => {\n if (action.payload?.user) {\n state.isLoggedIn = true\n state.isLoading = false\n state.email = action.payload.user.email\n state.fullName = action.payload.user.fullName\n state.isAdmin = action.payload.user.isAdmin\n state.id = action.payload.user.id\n state.activeOfferSubscriptions = action.payload.user.activeOfferSubscriptions\n } else {\n state.isLoggedIn = false\n state.isLoading = false\n }\n }\n )\n builder.addCase(getMe.pending, (state) => {\n state.isLoading = true\n state.isLoggedIn = false\n })\n builder.addCase(getMe.rejected, (state) => {\n state.isLoggedIn = false\n state.isLoading = false\n })\n builder.addCase(logout.fulfilled, (state) => {\n localStorage.removeItem('Authorization')\n state.isLoggedIn = false\n state.isLoading = false\n state.email = ''\n state.fullName = ''\n state.isAdmin = false\n state.id = 'guest'\n state.offerSubscriptions = []\n state.activeOfferSubscriptions = []\n // TODO: wrap it to normal helper\n router.visit(getUpdatedURLWithLocale('/app/login', i18next.language, location.origin))\n })\n builder.addCase(logout.pending, (state) => {\n state.isLoading = true\n state.isLoggedIn = false\n })\n builder.addCase(logout.rejected, (state) => {\n state.isLoggedIn = false\n state.isLoading = false\n })\n builder.addCase(\n login.fulfilled,\n (\n state,\n action: PayloadAction<\n { user: ProfileSliceType } & { authorization: string; redirectTo: string }\n >\n ) => {\n localStorage.setItem('Authorization', action.payload.authorization)\n state.isLoggedIn = true\n state.email = action.payload.user.email\n state.fullName = action.payload.user.fullName\n state.isAdmin = action.payload.user.isAdmin\n state.isLoading = false\n state.id = action.payload.user.id\n state.offerSubscriptions = []\n state.activeOfferSubscriptions = []\n // TODO: wrap it to normal helper\n router.replace(action.payload.redirectTo)\n }\n )\n builder.addCase(login.pending, (state) => {\n state.isLoading = true\n })\n builder.addCase(login.rejected, (state) => {\n state.isLoading = false\n state.isLoggedIn = false\n })\n builder.addCase(\n register.fulfilled,\n (\n state,\n action: PayloadAction\n ) => {\n localStorage.setItem('Authorization', action.payload.authorization)\n state.isLoggedIn = false\n state.email = action.payload.email\n state.fullName = action.payload.email\n state.isLoading = false\n state.isAdmin = action.payload.isAdmin\n state.id = action.payload?.id ?? ''\n state.offerSubscriptions = []\n state.activeOfferSubscriptions = []\n }\n )\n builder.addCase(register.pending, (state) => {\n state.isLoading = true\n })\n builder.addCase(register.rejected, (state) => {\n state.isLoading = false\n state.isLoggedIn = false\n })\n },\n})\n\nexport default profileSlice.reducer\n"],"names":["initialState","getMe","createAsyncThunk","userApi","logout","login","user","response","AxiosError","showLoginErrors","register","router","showRegistrationErrors","t","profileSlice","createSlice","builder","state","action","_a","getUpdatedURLWithLocale","i18next","profile"],"mappings":"+PA6CA,MAAMA,EAAiC,CACrC,WAAY,GACZ,SAAU,GACV,MAAO,GACP,QAAS,GACT,UAAW,GACX,mBAAoB,CAAC,EACrB,yBAA0B,CAAC,EAC3B,GAAI,OACN,EAEaC,EAAQC,EAAiB,aAAc,SAAY,CAC1D,GAAA,CAEF,OADiB,MAAMC,EAAQ,MACf,UACN,CAAC,CACf,CAAC,EAEYC,EAASF,EAAiB,iBAAkB,UACtC,MAAMC,EAAQ,UAEf,IACjB,EAEYE,EAAQH,EAAiB,gBAAiB,MAAOI,GAA6B,CACrF,GAAA,CACF,MAAMC,EAAW,MAAMJ,EAAQ,MAAMG,CAAI,EAGzC,MAAO,CAAE,GAAGC,EAAS,KAAM,cAAeA,EAAS,QAAQ,qBACpD,EAAG,CACN,aAAaC,GACfC,EAAgB,CAAC,CAErB,CACF,CAAC,EAEYC,EAAWR,EACtB,mBACA,MAAOI,GAAyC,CAC1C,GAAA,CACF,MAAMC,EAAW,MAAMJ,EAAQ,SAASG,CAAI,EACrCK,OAAAA,EAAA,QAAQL,EAAK,UAAU,EAEvB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