Skip to content

Commit

Permalink
fix: remove svod check for profiles, replace type guard check with yup
Browse files Browse the repository at this point in the history
  • Loading branch information
naumovski-filip committed Oct 18, 2023
1 parent 3ba6592 commit 2043921
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
4 changes: 0 additions & 4 deletions src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import type { LanguageDefinition } from '#src/i18n/config';
import Panel from '#components/Panel/Panel';
import type { Profile } from '#types/account';
import ProfileCircle from '#src/icons/ProfileCircle';
import type { AccessModel } from '#types/Config';
import AccountCircle from '#src/icons/AccountCircle';

type TypeHeader = 'static' | 'fixed';
Expand Down Expand Up @@ -50,7 +49,6 @@ type Props = {
currentLanguage: LanguageDefinition | undefined;
onLanguageClick: (code: string) => void;

accessModel?: AccessModel;
profilesData?: {
currentProfile: Profile | null;
profiles: Profile[];
Expand Down Expand Up @@ -84,7 +82,6 @@ const Header: React.FC<Props> = ({
supportedLanguages,
currentLanguage,
onLanguageClick,
accessModel,
profilesData: { currentProfile, profiles, profilesEnabled, selectProfile, isSelectingProfile } = {},
}) => {
const { t } = useTranslation('menu');
Expand Down Expand Up @@ -148,7 +145,6 @@ const Header: React.FC<Props> = ({
onClick={closeUserMenu}
showPaymentsItem={showPaymentsMenuItem}
small
accessModel={accessModel}
currentProfile={currentProfile}
profilesEnabled={profilesEnabled}
profiles={profiles}
Expand Down
16 changes: 2 additions & 14 deletions src/components/UserMenu/UserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,20 @@ import Exit from '#src/icons/Exit';
import MenuButton from '#components/MenuButton/MenuButton';
import { logout } from '#src/stores/AccountController';
import ProfileCircle from '#src/icons/ProfileCircle';
import type { AccessModel } from '#types/Config';
import type { Profile } from '#types/account';

type Props = {
small?: boolean;
showPaymentsItem: boolean;
onClick?: () => void;
accessModel?: AccessModel;
currentProfile?: Profile | null;
profilesEnabled?: boolean;
profiles?: Profile[];
isSelectingProfile?: boolean;
selectProfile?: ({ id, avatarUrl }: { id: string; avatarUrl: string }) => void;
};

const UserMenu = ({
showPaymentsItem,
small = false,
onClick,
accessModel,
currentProfile,
profilesEnabled,
profiles,
isSelectingProfile,
selectProfile,
}: Props) => {
const UserMenu = ({ showPaymentsItem, small = false, onClick, currentProfile, profilesEnabled, profiles, isSelectingProfile, selectProfile }: Props) => {
const { t } = useTranslation('user');
const navigate = useNavigate();

Expand All @@ -53,7 +41,7 @@ const UserMenu = ({

return (
<ul className={styles.menuItems}>
{accessModel === 'SVOD' && profilesEnabled && selectProfile && (
{profilesEnabled && selectProfile && (
<ProfilesMenu
profiles={profiles ?? []}
currentProfile={currentProfile}
Expand Down
1 change: 0 additions & 1 deletion src/containers/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ const Layout = () => {
closeLanguageMenu={closeLanguageMenu}
canLogin={!!clientId}
showPaymentsMenuItem={accessModel !== 'AVOD'}
accessModel={accessModel}
profilesData={{
currentProfile: profile,
profiles,
Expand Down
29 changes: 19 additions & 10 deletions src/stores/ProfileController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ProfilesData } from '@inplayer-org/inplayer.js';
import * as yup from 'yup';

import { useAccountStore } from './AccountStore';
import { useFavoritesStore } from './FavoritesStore';
Expand All @@ -25,16 +26,24 @@ export const persistProfile = ({ profile }: { profile: ProfilesData }) => {
});
};

export const isValidProfile = (profile: unknown): profile is ProfilesData => {
return (
typeof profile === 'object' &&
profile !== null &&
'id' in profile &&
'name' in profile &&
'avatar_url' in profile &&
'adult' in profile &&
'credentials' in profile
);
const profileSchema = yup.object().shape({
id: yup.string().required(),
name: yup.string().required(),
avatar_url: yup.string(),
adult: yup.boolean().required(),
credentials: yup.object().shape({
access_token: yup.string().required(),
expires: yup.number().required(),
}),
});

const isValidProfile = (profile: unknown): profile is ProfilesData => {
try {
profileSchema.validateSync(profile);
return true;
} catch (e: unknown) {
return false;
}
};

export const loadPersistedProfile = () => {
Expand Down

0 comments on commit 2043921

Please sign in to comment.