diff --git a/packages/widgets/user-profile-widget/src/lib/widget/api/sdk/createUserSdk.ts b/packages/widgets/user-profile-widget/src/lib/widget/api/sdk/createUserSdk.ts index cfc3aeec0..188596cc0 100644 --- a/packages/widgets/user-profile-widget/src/lib/widget/api/sdk/createUserSdk.ts +++ b/packages/widgets/user-profile-widget/src/lib/widget/api/sdk/createUserSdk.ts @@ -31,7 +31,7 @@ export const createUserSdk = ({ const json = await res.json(); - return json.data; + return json.data || []; }; return { diff --git a/packages/widgets/user-profile-widget/src/lib/widget/api/types.ts b/packages/widgets/user-profile-widget/src/lib/widget/api/types.ts index 87bf8b7fa..5a9cd7def 100644 --- a/packages/widgets/user-profile-widget/src/lib/widget/api/types.ts +++ b/packages/widgets/user-profile-widget/src/lib/widget/api/types.ts @@ -4,7 +4,7 @@ export type Sdk = ReturnType; type CustomAttributeType = string | boolean | number; -export type CustomAttributes = Record; +type CustomAttributes = Record; type UserStatus = 'enabled' | 'disabled' | 'invited'; diff --git a/packages/widgets/user-profile-widget/src/lib/widget/mixins/initMixin/initComponentsMixins/initUserCustomAttributesMixin.ts b/packages/widgets/user-profile-widget/src/lib/widget/mixins/initMixin/initComponentsMixins/initUserCustomAttributesMixin.ts index 9fdb5008c..69c5bf71f 100644 --- a/packages/widgets/user-profile-widget/src/lib/widget/mixins/initMixin/initComponentsMixins/initUserCustomAttributesMixin.ts +++ b/packages/widgets/user-profile-widget/src/lib/widget/mixins/initMixin/initComponentsMixins/initUserCustomAttributesMixin.ts @@ -22,8 +22,6 @@ export const initUserCustomAttributesMixin = createSingletonMixin( initWidgetRootMixin, modalMixin, )(superclass) { - customValueUserAttr: UserAttributeDriver; - // flow Id is key in all maps #editModals: Record = {}; diff --git a/packages/widgets/user-profile-widget/src/lib/widget/state/selectors.ts b/packages/widgets/user-profile-widget/src/lib/widget/state/selectors.ts index 054ca9b2f..61b59e04a 100644 --- a/packages/widgets/user-profile-widget/src/lib/widget/state/selectors.ts +++ b/packages/widgets/user-profile-widget/src/lib/widget/state/selectors.ts @@ -1,5 +1,5 @@ import { createSelector } from 'reselect'; -import { AttributeType } from '../api/types'; +import { AttributeType, CustomAttr } from '../api/types'; import { State } from './types'; export const getMe = (state: State) => state.me.data; @@ -25,26 +25,32 @@ export const getCustomAttributes = (state: State) => export const getUserCustomAttrs = createSelector( getMe, getCustomAttributes, - (userData, allCustomAttrs = []) => { - const res: Record = {}; + ( + userData: Record, + allCustomAttrs: CustomAttr[] = [], + ): Record => { const userCustomAttributes = userData['customAttributes'] || {}; - Object.keys(userCustomAttributes).forEach((key: string) => { - const type = - allCustomAttrs.find((attr) => attr.name === key)?.type || - AttributeType.text; - if (type === AttributeType.date && userCustomAttributes[key]) { - // to full date time - res[key] = new Date(userCustomAttributes[key]).toLocaleString(); - } else if ( - type === AttributeType.boolean && - userCustomAttributes[key] !== undefined - ) { - res[key] = !userCustomAttributes[key] ? 'False' : 'True'; - } else { - res[key] = (userCustomAttributes[key] || '').toString(); - } - }); - return res; + return Object.entries(userCustomAttributes).reduce( + (acc, [key]) => { + const type = + allCustomAttrs.find((attr) => attr.name === key)?.type || + AttributeType.text; + + if (type === AttributeType.date && userCustomAttributes[key]) { + // to full date time + acc[key] = new Date(userCustomAttributes[key]).toLocaleString(); + } else if ( + type === AttributeType.boolean && + userCustomAttributes[key] !== undefined + ) { + acc[key] = !userCustomAttributes[key] ? 'False' : 'True'; + } else { + acc[key] = (userCustomAttributes[key] || '').toString(); + } + return acc; + }, + {} as Record, + ); }, ); diff --git a/packages/widgets/user-profile-widget/test/user-profile-widget.test.ts b/packages/widgets/user-profile-widget/test/user-profile-widget.test.ts index ba0dab5cd..67986e4ba 100644 --- a/packages/widgets/user-profile-widget/test/user-profile-widget.test.ts +++ b/packages/widgets/user-profile-widget/test/user-profile-widget.test.ts @@ -102,5 +102,22 @@ describe('user-profile-widget', () => { expect(result).toEqual(mockUser); }); + + it('custom attributes', async () => { + mockHttpClient.reset(); + const sdk = createSdk({ projectId: mockProjectId }, false); + const result = await sdk.user.getCustomAttributes(); + + await waitFor(() => expect(mockHttpClient.get).toHaveBeenCalledTimes(1), { + timeout: 5000, + }); + await waitFor(() => + expect(mockHttpClient.get).toHaveBeenCalledWith( + apiPaths.user.customAttributes, + ), + ); + + expect(result).toEqual([]); + }); }); });