Skip to content

Commit

Permalink
Support custom fields in user-profile-widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitzperetz committed Nov 10, 2024
1 parent 6c990bf commit 170f145
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/sdks/react-sdk/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"typeRoots": ["./node_modules/@types", "./node_modules/@types/react"]
},
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "build", "dist", "test", "examples"]
"exclude": ["node_modules", "build", "dist", "test"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { UserAttributeDriver } from '@descope/sdk-component-drivers';
import { compose, createSingletonMixin } from '@descope/sdk-helpers';
import { loggerMixin } from '@descope/sdk-mixins';
import { getUserCustomAttrs } from '../../../state/selectors';
import { stateManagementMixin } from '../../stateManagementMixin';
import { initWidgetRootMixin } from './initWidgetRootMixin';

export const initUserCustomAttributesMixin = createSingletonMixin(
<T extends CustomElementConstructor>(superclass: T) =>
class UserCustomAttributesMixinClass extends compose(
stateManagementMixin,
loggerMixin,
initWidgetRootMixin,
)(superclass) {
customValueUserAttr: UserAttributeDriver;

#initCustomValueUserAttrs() {
const allCustomAttributesComponents = this.shadowRoot?.querySelectorAll(
'descope-user-attribute[data-id^="customAttributes."]',
);

Array.from(allCustomAttributesComponents).forEach((nodeEle) => {
const attrName = nodeEle.getAttribute('data-id');
const customAttrName = attrName.replace('customAttributes.', '');

const compInstance = new UserAttributeDriver(nodeEle, {
logger: this.logger,
});

const userCustomAttributesData = getUserCustomAttrs(this.state);
compInstance.value = userCustomAttributesData[customAttrName];
});
}

async onWidgetRootReady() {
await super.onWidgetRootReady?.();

this.#initCustomValueUserAttrs();
}
},
);
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { compose, createSingletonMixin } from '@descope/sdk-helpers';
import { debuggerMixin, themeMixin } from '@descope/sdk-mixins';
import { flowRedirectUrlMixin } from '../flowRedirectUrlMixin';
import { initAvatarMixin } from './initComponentsMixins/initAvatarMixin';
import { initEmailUserAttrMixin } from './initComponentsMixins/initEmailUserAttrMixin';
import { initLogoutMixin } from './initComponentsMixins/initLogoutMixin';
import { initNameUserAttrMixin } from './initComponentsMixins/initNameUserAttrMixin';
import { initPhoneUserAttrMixin } from './initComponentsMixins/initPhoneUserAttrMixin';
import { initPasskeyUserAuthMethodMixin } from './initComponentsMixins/initPasskeyUserAuthMethodMixin';
import { initPasswordUserAuthMethodMixin } from './initComponentsMixins/initPasswordUserAuthMethodMixin';
import { initLogoutMixin } from './initComponentsMixins/initLogoutMixin';
import { flowRedirectUrlMixin } from '../flowRedirectUrlMixin';
import { initPhoneUserAttrMixin } from './initComponentsMixins/initPhoneUserAttrMixin';
import { initUserCustomAttributesMixin } from './initComponentsMixins/initUserCustomAttributesMixin';

export const initMixin = createSingletonMixin(
<T extends CustomElementConstructor>(superclass: T) =>
Expand All @@ -16,6 +17,7 @@ export const initMixin = createSingletonMixin(
debuggerMixin,
themeMixin,
flowRedirectUrlMixin, // This mixin must be before all other mixins that loads flows
initUserCustomAttributesMixin,
initEmailUserAttrMixin,
initAvatarMixin,
initNameUserAttrMixin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ export const getIsPhoneVerified = createSelector(
);
export const getHasPasskey = createSelector(getMe, (me) => me.webauthn);
export const getHasPassword = createSelector(getMe, (me) => me.password);

export const getUserCustomAttrs = createSelector(
getMe,
(me) => me.customAttributes,
);

0 comments on commit 170f145

Please sign in to comment.