Skip to content

Commit

Permalink
fix: avoided response mapping in cleeng
Browse files Browse the repository at this point in the history
  • Loading branch information
mirovladimitrovski committed Sep 11, 2023
1 parent 1d50c00 commit b172dc7
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 43 deletions.
22 changes: 21 additions & 1 deletion src/components/Account/__snapshots__/Account.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,27 @@ exports[`<Account> > renders and matches snapshot 1`] = `
>
<div
class="_customFields_c968f7"
/>
>
<div
class="_checkbox_531f07"
>
<div
class="_row_531f07"
>
<input
id="check-box_1235_consentsValues.marketing"
name="consentsValues.marketing"
type="checkbox"
value=""
/>
<label
for="check-box_1235_consentsValues.marketing"
>
Receive Marketing Emails
</label>
</div>
</div>
</div>
</form>
<div
class="_controls_1c1c63"
Expand Down
8 changes: 3 additions & 5 deletions src/components/CustomRegisterField/CustomRegisterField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { type FC, type ChangeEventHandler, type ReactNode, useMemo } from 'react
import { useTranslation } from 'react-i18next';
import type { GetRegisterFieldOption } from '@inplayer-org/inplayer.js';

import type { ConsentFieldVariants } from '#types/account';
import type { CustomRegisterFieldVariant } from '#types/account';
import Checkbox from '#components/Checkbox/Checkbox';
import TextField from '#components/TextField/TextField';
import Radio from '#components/Radio/Radio';
import Dropdown from '#components/Dropdown/Dropdown';
import DateField from '#components/DateField/DateField';

type Props = {
type: ConsentFieldVariants;
type?: CustomRegisterFieldVariant;
name: string;
value: string | boolean;
onChange: ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement>;
Expand Down Expand Up @@ -51,8 +51,6 @@ export const CustomRegisterField: FC<Props> = ({ type, value = '', ...props }) =
}, [t, type, props.options, i18n]);

switch (type) {
case 'checkbox':
return <Checkbox {...props} checked={value === true} />;
case 'input':
return <TextField {...props} value={value as string} />;
case 'radio':
Expand All @@ -64,7 +62,7 @@ export const CustomRegisterField: FC<Props> = ({ type, value = '', ...props }) =
case 'datepicker':
return <DateField {...props} value={value as string} />;
default:
return null;
return <Checkbox {...props} checked={value === true} />;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1597,7 +1597,29 @@ exports[`<CustomRegisterField> > renders and matches snapshot <Dropdown type="da
</div>
`;
exports[`<CustomRegisterField> > renders and matches snapshot <Dropdown type="randomstring"> 1`] = `<div />`;
exports[`<CustomRegisterField> > renders and matches snapshot <Dropdown type="randomstring"> 1`] = `
<div>
<div
class="_checkbox_531f07"
>
<div
class="_row_531f07"
>
<input
id="check-box_1235_name"
name="name"
type="checkbox"
value=""
/>
<label
for="check-box_1235_name"
>
label
</label>
</div>
</div>
</div>
`;
exports[`<CustomRegisterField> > renders and matches snapshot <Dropdown type="select"> 1`] = `
<div>
Expand Down
27 changes: 1 addition & 26 deletions src/services/cleeng.account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import type {
UpdateCustomerPayload,
ChangePasswordWithOldPassword,
UpdatePersonalShelves,
Consent,
} from '#types/account';
import cleengAuthService from '#src/services/cleeng.auth.service';

Expand Down Expand Up @@ -132,37 +131,13 @@ export async function getUser({ config }: { config: Config }) {
};
}

interface CleengConsent {
broadcasterId: number;
enabledByDefault: boolean;
label: string;
name: string;
required: boolean;
value: string;
version: string;
}

export const getPublisherConsents: GetPublisherConsents = async (config) => {
const { cleeng } = config.integrations;
const response = await get(!!cleeng?.useSandbox, `/publishers/${cleeng?.id}/consents`);

handleErrors(response.errors);

const consents = ((response?.responseData?.consents || []) as CleengConsent[]).map(
(cleengConsent): Consent => ({
type: 'checkbox',
isCustomRegisterField: false,
defaultValue: cleengConsent.enabledByDefault,
name: cleengConsent.name,
label: cleengConsent.label,
placeholder: '',
required: cleengConsent.required,
options: {},
version: cleengConsent.version,
}),
);

return { consents };
return { consents: response?.responseData?.consents || [] };
};

export const getCustomerConsents: GetCustomerConsents = async (payload) => {
Expand Down
14 changes: 10 additions & 4 deletions src/services/inplayer.account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {
UpdateCustomerArgs,
UpdateCustomerConsents,
UpdatePersonalShelves,
ConsentFieldVariants,
CustomRegisterFieldVariant,
} from '#types/account';
import type { Config } from '#types/Config';
import type { InPlayerAuthData, InPlayerError } from '#types/inplayer';
Expand Down Expand Up @@ -164,16 +164,22 @@ export const getPublisherConsents: GetPublisherConsents = async (config) => {
.filter((field) => !['email_confirmation', 'first_name', 'surname'].includes(field.name))
.map(
(field): Consent => ({
type: field.type as ConsentFieldVariants,
type: field.type as CustomRegisterFieldVariant,
isCustomRegisterField: true,
defaultValue: field.type === 'checkbox' ? field.default_value === 'true' : field.default_value,
name: field.name,
label: field.label,
placeholder: field.placeholder,
required: field.required,
// todo: field.option type in SDK is incorrect, remove the type casting after fixing that
options: field.options as unknown as Record<string, string>,
version: '1',
...(field.type === 'checkbox'
? {
enabledByDefault: field.default_value === 'true',
}
: {
defaultValue: field.default_value,
}),
}),
);

Expand Down Expand Up @@ -475,7 +481,7 @@ function getTermsConsent(): Consent {
required: true,
name: 'terms',
label: i18next.t('account:registration.terms_consent', { termsUrl }),
defaultValue: false,
enabledByDefault: false,
placeholder: '',
options: {},
version: '1',
Expand Down
4 changes: 2 additions & 2 deletions src/utils/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const formatConsentValues = (publisherConsents: Consent[] | null = [], customerC
const consent = customerConsents?.find((customerConsent) => customerConsent.name === publisherConsent.name);

if (consent) {
const value = publisherConsent.isCustomRegisterField ? consent.value ?? '' : consent.state === 'accepted';
const value = publisherConsent.isCustomRegisterField === true ? consent.value ?? '' : consent.state === 'accepted';
values[publisherConsent.name] = value;
}
});
Expand Down Expand Up @@ -97,7 +97,7 @@ const extractConsentValues = (consents?: Consent[]) => {
}

consents?.forEach((consent) => {
values[consent.name] = consent.defaultValue ?? '';
values[consent.name] = consent.type === 'checkbox' ? consent.enabledByDefault === true : consent.defaultValue ?? '';
});

return values;
Expand Down
9 changes: 5 additions & 4 deletions types/account.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,13 @@ export type UpdateCustomerArgs = {
fullName?: string;
};

export type ConsentFieldVariants = 'input' | 'select' | 'country' | 'us_state' | 'radio' | 'checkbox' | 'datepicker';
export type CustomRegisterFieldVariant = 'input' | 'select' | 'country' | 'us_state' | 'radio' | 'checkbox' | 'datepicker';

export interface Consent {
type: ConsentFieldVariants;
isCustomRegisterField: boolean;
defaultValue: string | boolean;
type?: CustomRegisterFieldVariant;
isCustomRegisterField?: boolean;
enabledByDefault?: boolean;
defaultValue?: string;
name: string;
label: string;
placeholder: string;
Expand Down

0 comments on commit b172dc7

Please sign in to comment.