Skip to content

Commit

Permalink
fix: i18n fixes, move useProfiles logic to controller and service
Browse files Browse the repository at this point in the history
  • Loading branch information
naumovski-filip committed Oct 18, 2023
1 parent a22f133 commit d15e2ac
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
4 changes: 2 additions & 2 deletions public/locales/en/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@
"already_exists": "This profile name is already taken. Please choose another one.",
"invalid_characters": "Your profile name can only contain letters, numbers, and spaces.",
"required": "Please enter a name for your profile.",
"too_long": "Please limit your profile name to {{count}} characters or fewer.",
"too_short": "Please enter at least {{count}} characters."
"too_long": "Please limit your profile name to {{charactersCount}} characters or fewer.",
"too_short": "Please enter at least {{charactersCount}} characters."
}
},
"watch_now": "Watch now"
Expand Down
15 changes: 0 additions & 15 deletions src/hooks/useProfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { createProfile, deleteProfile, enterProfile, listProfiles, updateProfile
import { useProfileStore } from '#src/stores/ProfileStore';
import type { CommonAccountResponse, ListProfilesResponse, ProfileDetailsPayload, ProfilePayload } from '#types/account';
import { useAccountStore } from '#src/stores/AccountStore';
import defaultAvatar from '#src/assets/profiles/default_avatar.png';
import type { GenericFormErrors } from '#types/form';
import type { ProfileFormSubmitError } from '#src/containers/Profiles/types';

Expand Down Expand Up @@ -102,23 +101,9 @@ export const useProfiles = (
const user = useAccountStore((s) => s.user);
const query = useQuery(['listProfiles'], listProfiles, { ...options, enabled: !!user });
const { canManageProfiles } = useAccountStore();
if (!canManageProfiles && query.data?.responseData.canManageProfiles) {
useAccountStore.setState({ canManageProfiles: true });
}

return {
...query,
data: {
...query.data,
responseData: {
...query.data?.responseData,
collection:
query.data?.responseData.collection.map((profile) => ({
...profile,
avatar_url: profile?.avatar_url || defaultAvatar,
})) ?? [],
},
},
profilesEnabled: query.data?.responseData.canManageProfiles && canManageProfiles,
};
};
7 changes: 6 additions & 1 deletion src/services/inplayer.profile.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import InPlayer from '@inplayer-org/inplayer.js';

import type { ListProfiles, CreateProfile, UpdateProfile, EnterProfile, GetProfileDetails, DeleteProfile } from '#types/account';
import defaultAvatar from '#src/assets/profiles/default_avatar.png';

export const listProfiles: ListProfiles = async () => {
try {
const response = await InPlayer.Account.getProfiles();
return {
responseData: {
canManageProfiles: true,
collection: response.data,
collection:
response.data.map((profile) => ({
...profile,
avatar_url: profile?.avatar_url || defaultAvatar,
})) ?? [],
},
errors: [],
};
Expand Down
8 changes: 7 additions & 1 deletion src/stores/ProfileController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useFavoritesStore } from './FavoritesStore';
import { useProfileStore } from './ProfileStore';
import { useWatchHistoryStore } from './WatchHistoryStore';
import { initializeAccount } from './AccountController';
import { useAccountStore } from './AccountStore';

import * as persist from '#src/utils/persist';
import type { ProfilePayload, EnterProfilePayload, ProfileDetailsPayload } from '#types/account';
Expand All @@ -15,7 +16,12 @@ export const unpersistProfile = () => {

export const listProfiles = async () => {
return await useService(async ({ profileService, sandbox }) => {
return await profileService?.listProfiles(undefined, sandbox ?? true);
const res = await profileService?.listProfiles(undefined, sandbox ?? true);
const canManageProfiles = useAccountStore.getState().canManageProfiles;
if (!canManageProfiles && res?.responseData.canManageProfiles) {
useAccountStore.setState({ canManageProfiles: true });
}
return res;
});
};

Expand Down

0 comments on commit d15e2ac

Please sign in to comment.