-
Notifications
You must be signed in to change notification settings - Fork 8
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
Nitzperetz
wants to merge
16
commits into
main
Choose a base branch
from
feat/user_profile_widget_add_custom_fields
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
170f145
Support custom fields in user-profile-widget
Nitzperetz 88ebdf4
Merge branch 'main' into feat/user_profile_widget_add_custom_fields
Nitzperetz 84f8ed4
getCustomAttributes in higher scope
Nitzperetz 5580e33
Update tsconfig.json
Nitzperetz 24279c9
getCustomAttributes add subscribe
Nitzperetz 97db9a6
Merge branch 'feat/user_profile_widget_add_custom_fields' of github.c…
Nitzperetz acadf65
getCustomAttributes add cache
Nitzperetz b784625
update func for custom fields display and package.json start for reac…
Nitzperetz 0332f05
Update package.json
Nitzperetz c57559b
Merge branch 'main' into feat/user_profile_widget_add_custom_fields
Nitzperetz baa58d3
Merge branch 'main' into feat/user_profile_widget_add_custom_fields
nirgur 22b0eac
edit/delete flows in user custom attributes field
Nitzperetz 67a460e
Merge branch 'feat/user_profile_widget_add_custom_fields' of github.c…
Nitzperetz 347fe6d
update from main
Nitzperetz 80f27d8
update from main
Nitzperetz d0925fe
cov
Nitzperetz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
...get/src/lib/widget/mixins/initMixin/initComponentsMixins/initUserCustomAttributesMixin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
(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, | ||
); | ||
} | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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