Skip to content

Commit

Permalink
feat: inplayer update account details
Browse files Browse the repository at this point in the history
  • Loading branch information
darkoatanasovski committed Dec 1, 2022
1 parent 4d327b6 commit 9d5af70
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/components/Form/FormSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface FormSectionProps<TData extends GenericFormValues, TErrors> {
saveButton?: string;
cancelButton?: string;
canSave?: (values: TData) => boolean;
onSubmit?: (values: TData) => Promise<{ errors?: string[] }> | void;
onSubmit?: (values: TData) => Promise<{ errors?: string[] }> | Promise<void> | void;
content: (args: FormSectionContentArgs<TData, TErrors>) => ReactNode;
children?: never;
}
Expand Down
23 changes: 22 additions & 1 deletion src/services/inplayer.account.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import InPlayer, { Env } from '@inplayer-org/inplayer.js';

import type { AuthData, Customer, Login } from '#types/account';
import type { AuthData, Customer, Login, UpdateCustomer } from '#types/account';
import { processInplayerAccount, processInPlayerAuth } from '#src/utils/common';
import type { Config } from '#types/Config';

Expand Down Expand Up @@ -51,3 +51,24 @@ export const getUser = async (): Promise<Customer> => {
};

export const getFreshJwtToken = async ({ auth }: { auth: AuthData }) => auth;

export const updateCustomer: UpdateCustomer = async (values) => {
try {
const response = await InPlayer.Account.updateAccount({
fullName: `${values.firstName} ${values.lastName}`,
metadata: {
first_name: values.firstName as string,
last_name: values.lastName as string,
},
});

return {
errors: [],
// @ts-ignore
// wrong data type from InPlayer SDK
responseData: processInplayerAccount(response.data),
};
} catch {
throw new Error('Failed to fetch user data.');
}
};
35 changes: 21 additions & 14 deletions src/stores/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,25 @@ export const initializeAccount = async () => {
};

export async function updateUser(values: { firstName: string; lastName: string } | { email: string; confirmationPassword: string }) {
const { auth, user } = useAccountStore.getState();
await withAccountService(async ({ accountService, sandbox }) => {
useAccountStore.setState({ loading: true });

if (!auth || !user) throw new Error('no auth');
const { auth, user } = useAccountStore.getState();

const { cleengSandbox } = useConfigStore.getState().getCleengData();
if (!auth || !user) throw new Error('no auth');

const response = await cleengAccountService.updateCustomer({ ...values, id: user.id.toString() }, cleengSandbox, auth.jwt);
const response = await accountService.updateCustomer({ ...values, id: user.id.toString() }, sandbox, auth.jwt);

if (!response) {
return { errors: Array.of('Unknown error') };
}
if (!response) {
return { errors: Array.of('Unknown error') };
}

if (response.errors?.length === 0) {
useAccountStore.setState({ user: response.responseData });
}
if (response.errors?.length === 0) {
useAccountStore.setState({ user: response.responseData });
}

return response;
return response;
});
}

export const refreshJwtToken = async (auth: AuthData) => {
Expand Down Expand Up @@ -451,16 +453,21 @@ function useLoginContext<T>(callback: (args: { cleengId: string; cleengSandbox:
}

function withAccountService<T>(
callback: (args: { accountService: typeof inplayerAccountService | typeof cleengAccountService; config: Config; accessModel: AccessModel }) => T,
callback: (args: {
accountService: typeof inplayerAccountService | typeof cleengAccountService;
config: Config;
accessModel: AccessModel;
sandbox: boolean;
}) => T,
): T {
const { config, accessModel } = useConfigStore.getState();

const { cleeng, inplayer } = config.integrations;

if (inplayer?.clientId) {
return callback({ accountService: inplayerAccountService, config, accessModel });
return callback({ accountService: inplayerAccountService, config, accessModel, sandbox: !!inplayer.useSandbox });
} else if (cleeng?.id) {
return callback({ accountService: cleengAccountService, config, accessModel });
return callback({ accountService: cleengAccountService, config, accessModel, sandbox: !!cleeng.useSandbox });
}

throw new Error('No account service available');
Expand Down

0 comments on commit 9d5af70

Please sign in to comment.