Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support custom fields in user-profile-widget + Edit/Delete flows #843

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/widgets/user-profile-widget/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ module.exports = {
global: {
branches: 0,
functions: 12,
lines: 38,
statements: 37,
lines: 36,
statements: 36,
},
},
globals: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import {
FlowDriver,
ModalDriver,
UserAttributeDriver,
} from '@descope/sdk-component-drivers';
import {
compose,
createSingletonMixin,
withMemCache,
} from '@descope/sdk-helpers';
import { loggerMixin, modalMixin } from '@descope/sdk-mixins';
import { getUserCustomAttrs } from '../../../state/selectors';
import { createFlowTemplate } from '../../helpers';
import { stateManagementMixin } from '../../stateManagementMixin';
import { initWidgetRootMixin } from './initWidgetRootMixin';

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

// flow Id is key in all maps
#editModals: Record<string, ModalDriver> = {};

#editFlows: Record<string, FlowDriver> = {};

#deleteModals: Record<string, ModalDriver> = {};

#deleteFlows: Record<string, FlowDriver> = {};

#initEditModalContent(flowId: string) {
this.#editModals[flowId]?.setContent(
createFlowTemplate({
projectId: this.projectId,
flowId,
baseUrl: this.baseUrl,
baseStaticUrl: this.baseStaticUrl,
}),
);
this.#editFlows[flowId]?.onSuccess(() => {
this.#editModals[flowId]?.close();
this.actions.getMe();
});
}

// have 2 init functions for edit and delete modals in order to keep the same standards as the email/phone/name mixin
#initDeleteModalContent(flowId: string) {
this.#deleteModals[flowId]?.setContent(
createFlowTemplate({
projectId: this.projectId,
flowId,
baseUrl: this.baseUrl,
baseStaticUrl: this.baseStaticUrl,
}),
);
this.#deleteFlows[flowId]?.onSuccess(() => {
this.#deleteModals[flowId]?.close();
this.actions.getMe();
});
}

#updateCustomValueUserAttrs = withMemCache(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is a bit big and contains some repetitive code
Do you think we could split it into smaller functions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was really about keeping the same standards as the other mixin - to begin with a preferred extracting code to functions and reuse.
Let me see what I can do here

(customAttr: ReturnType<typeof getUserCustomAttrs>) => {
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,
});

compInstance.value = customAttr[customAttrName] || '';

const editFlowId = nodeEle.getAttribute('edit-flow-id');
if (editFlowId) {
this.#editModals[editFlowId] = this.createModal({
'data-id': `edit-${customAttrName}`,
});

this.#editFlows[editFlowId] = new FlowDriver(
() =>
this.#editModals[editFlowId]?.ele?.querySelector(
'descope-wc',
),
{ logger: this.logger },
);
this.#editModals[editFlowId].afterClose =
this.#initEditModalContent.bind(this, editFlowId);

compInstance.onEditClick(() => {
this.#editModals?.[editFlowId]?.open();
});

this.#initEditModalContent(editFlowId);
}

const deleteFlowId = nodeEle.getAttribute('delete-flow-id');
if (deleteFlowId) {
this.#deleteModals[deleteFlowId] = this.createModal({
'data-id': `delete-${customAttrName}`,
});

this.#deleteFlows[deleteFlowId] = new FlowDriver(
() =>
this.#deleteModals[deleteFlowId]?.ele?.querySelector(
'descope-wc',
),
{ logger: this.logger },
);
this.#deleteModals[deleteFlowId].afterClose =
this.#initDeleteModalContent.bind(this, deleteFlowId);

compInstance.onDeleteClick(() => {
this.#deleteModals?.[deleteFlowId]?.open();
});

this.#initDeleteModalContent(deleteFlowId);
}
});
},
);

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

this.#updateCustomValueUserAttrs(getUserCustomAttrs(this.state));

this.subscribe(
this.#updateCustomValueUserAttrs.bind(this),
getUserCustomAttrs,
);
}
},
);
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,
);
Loading
Loading