Skip to content

Commit

Permalink
fix: fix loading overlay, move select profile logic to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
naumovski-filip committed Oct 18, 2023
1 parent d15e2ac commit 519f76e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/containers/Profiles/CreateProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const CreateProfile = () => {
},
);

if (loadingProfilesList) return <LoadingOverlay inline />;
if (loadingProfilesList) return <LoadingOverlay />;

return (
<div className={styles.user}>
Expand Down
2 changes: 1 addition & 1 deletion src/containers/Profiles/DeleteProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const DeleteProfile = () => {
return (
<div>
<Dialog open={!!viewParam} onClose={closeHandler}>
{isDeleting && <LoadingOverlay inline />}
{isDeleting && <LoadingOverlay />}
<div className={styles.deleteModal}>
<div>
<h2>{t('profile.delete')}</h2>
Expand Down
2 changes: 1 addition & 1 deletion src/containers/Profiles/EditProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const EditProfile = ({ contained = false }: EditProfileProps) => {
},
);

if (isLoading || isFetching) return <LoadingOverlay inline />;
if (isLoading || isFetching) return <LoadingOverlay />;

return (
<div className={classNames(styles.user, contained && profileStyles.contained)}>
Expand Down
2 changes: 1 addition & 1 deletion src/containers/Profiles/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const Form = ({ initialValues, formHandler, selectedAvatar, showCancelButton = t
<div className={profileStyles.profileInfo}>{t('profile.description')}</div>
<div className={profileStyles.formFields}>
{errors.form ? <FormFeedback variant="error">{errors.form}</FormFeedback> : null}
{submitting && <LoadingOverlay inline />}
{submitting && <LoadingOverlay />}
<h2 className={profileStyles.nameHeading}>{t('name')}</h2>
<TextField
required
Expand Down
2 changes: 1 addition & 1 deletion src/containers/Profiles/Profiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const Profiles = ({ editMode = false }: Props) => {

const createdProfileData = data?.responseData.collection.find((profile: Profile) => profile.id === createdProfileId);

if (loading || isLoading || isFetching) return <LoadingOverlay inline />;
if (loading || isLoading || isFetching) return <LoadingOverlay />;

return (
<>
Expand Down
38 changes: 27 additions & 11 deletions src/stores/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import i18next from 'i18next';
import { useProfileStore } from './ProfileStore';
import { unpersistProfile } from './ProfileController';

import type {
Profile,
Capture,
Customer,
CustomerConsent,
EmailConfirmPasswordInput,
FirstLastNameInput,
GetCaptureStatusResponse,
GetCustomerConsentsResponse,
GetPublisherConsentsResponse,
} from '#types/account';
import { queryClient } from '#src/containers/QueryProvider/QueryProvider';
import useAccount from '#src/hooks/useAccount';
import useService from '#src/hooks/useService';
Expand All @@ -14,21 +25,11 @@ import { restoreWatchHistory, serializeWatchHistory } from '#src/stores/WatchHis
import { useWatchHistoryStore } from '#src/stores/WatchHistoryStore';
import { logDev } from '#src/utils/common';
import * as persist from '#src/utils/persist';
import type {
Capture,
Customer,
CustomerConsent,
EmailConfirmPasswordInput,
FirstLastNameInput,
GetCaptureStatusResponse,
GetCustomerConsentsResponse,
GetPublisherConsentsResponse,
} from '#types/account';
import type { Offer } from '#types/checkout';

const PERSIST_PROFILE = 'profile';

export const initializeAccount = async () => {
export const initializeAccount = async ({ profile }: { profile?: Profile } = {}) => {
await useService(async ({ accountService, config }) => {
if (!accountService) {
useAccountStore.setState({ loading: false });
Expand All @@ -52,6 +53,9 @@ export const initializeAccount = async () => {
await accountService.initialize(config, logout);

try {
if (profile?.credentials?.access_token) {
loadProfile(profile);
}
const authData = await accountService.getAuthData();

if (authData) {
Expand All @@ -73,6 +77,18 @@ export const initializeAccount = async () => {
});
};

const loadProfile = (profile: Profile) => {
persist.setItem(PERSIST_PROFILE, profile);
persist.setItemStorage('inplayer_token', {
expires: profile.credentials.expires,
token: profile.credentials.access_token,
refreshToken: '',
});
useFavoritesStore.setState({ favorites: [] });
useWatchHistoryStore.setState({ watchHistory: [] });
useProfileStore.getState().setProfile(profile);
};

export async function updateUser(values: FirstLastNameInput | EmailConfirmPasswordInput): Promise<ServiceResponse<Customer>> {
return await useService(async ({ accountService, sandbox = true }) => {
useAccountStore.setState({ loading: true });
Expand Down
18 changes: 3 additions & 15 deletions src/stores/ProfileController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { useFavoritesStore } from './FavoritesStore';
import { useProfileStore } from './ProfileStore';
import { useWatchHistoryStore } from './WatchHistoryStore';
import { initializeAccount } from './AccountController';
import { useAccountStore } from './AccountStore';

Expand Down Expand Up @@ -41,18 +38,9 @@ export const enterProfile = async ({ id, pin }: EnterProfilePayload) => {
return await useService(async ({ profileService, sandbox }) => {
const response = await profileService?.enterProfile({ id, pin }, sandbox ?? true);
const profile = response?.responseData;
if (profile?.credentials?.access_token) {
persist.setItem(PERSIST_PROFILE, profile);
persist.setItemStorage('inplayer_token', {
expires: profile.credentials.expires,
token: profile.credentials.access_token,
refreshToken: '',
});
useFavoritesStore.setState({ favorites: [] });
useWatchHistoryStore.setState({ watchHistory: [] });
useProfileStore.getState().setProfile(profile);
}
return initializeAccount();
return initializeAccount({
profile,
});
});
};

Expand Down

0 comments on commit 519f76e

Please sign in to comment.