-
Notifications
You must be signed in to change notification settings - Fork 0
Rss ecomm 3 17 #162
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
Open
InStageTwo
wants to merge
3
commits into
feat/user-profile
Choose a base branch
from
RSS-ECOMM-3_17
base: feat/user-profile
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
Rss ecomm 3 17 #162
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,32 @@ | ||
| import { Customer, CustomerChangePassword } from '@commercetools/platform-sdk'; | ||
| import apiRoot from './api-root'; | ||
|
|
||
| const changeCustomerPassword = ( | ||
| customerId: string, | ||
| version: number, | ||
| currentPassword: string, | ||
| newPassword: string | ||
| ): Promise<Customer | null> => { | ||
| const body: CustomerChangePassword = { | ||
| id: customerId, | ||
| version, | ||
| currentPassword, | ||
| newPassword, | ||
| }; | ||
|
|
||
| return apiRoot | ||
| .customers() | ||
| .password() | ||
| .post({ body }) | ||
| .execute() | ||
| .then((response) => { | ||
| // console.log('Password changed successfully:', response); | ||
| return response; | ||
| }) | ||
| .catch((error) => { | ||
| // console.error('Error changing password:', error); | ||
| return error; | ||
| }); | ||
| }; | ||
|
|
||
| export default changeCustomerPassword; | ||
This file contains hidden or 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,250 @@ | ||
| import View from '@views/view'; | ||
| import { BaseComponent, IAttributes } from '@components/base-component'; | ||
| import { FormComponent, IFormAttributes } from '@components/form-component'; | ||
| import { InputFieldComponent } from '@components/input-field-component'; | ||
| import { | ||
| ButtonComponent, | ||
| IButtonAttributes, | ||
| } from '@components/button-component'; | ||
| import Router from '@utils/router'; | ||
| import { showErrorMessage, showSucessMessage } from '@utils/toast-messages'; | ||
| import createPasswordField from '@utils/create-password-field'; | ||
| import createEmailField from '@utils/create-email-field'; | ||
| import { getCustomer } from '@services/getCustomer'; | ||
| import { Customer, CustomerSignin } from '@commercetools/platform-sdk'; | ||
| import FormSectionView from '@views/registration/form-section'; | ||
| import { IFormInputField } from '@utils/create-input-field'; | ||
| import apiRoot from '@services/api-root'; | ||
| import { | ||
| passwordValidator, | ||
| specialCharValidator, | ||
| } from '@utils/validators/password-validator'; | ||
| import changeCustomerPassword from '@services/password'; | ||
|
|
||
| export default class PasswordChangeView extends View { | ||
| private form = new FormComponent({}); | ||
|
|
||
| private currentWrapper = new BaseComponent({}); | ||
|
|
||
| private newWrapper = new BaseComponent({}); | ||
|
|
||
| private passwordSection = new FormSectionView(); | ||
|
|
||
| private emailField = new InputFieldComponent({}, {}, {}); | ||
|
|
||
| private passwordField = new InputFieldComponent({}, {}, {}); | ||
|
|
||
| private newPasswordField = new InputFieldComponent({}, {}, {}); | ||
|
|
||
| private confirmNewPasswordField = new InputFieldComponent({}, {}, {}); | ||
|
|
||
| private button = new ButtonComponent({}); | ||
|
|
||
| private showPasswordButton = new ButtonComponent({}); | ||
|
|
||
| private changePassword = new ButtonComponent({}); | ||
|
|
||
| constructor() { | ||
| const attrs: IAttributes = { | ||
| tag: 'section', | ||
| id: 'change-section', | ||
| classList: 'row', | ||
| }; | ||
| super(attrs); | ||
| this.addForm(); | ||
| this.addCurrentSection(); | ||
| this.getData(); | ||
| this.addEmailField(); | ||
| this.addPasswordField(); | ||
| this.addNewPasswordField(); | ||
| } | ||
|
|
||
| private addForm() { | ||
| const attrs: IFormAttributes = { | ||
| classList: 'col s6 offset-s3', | ||
| }; | ||
| this.form = new FormComponent(attrs); | ||
| this.passwordSection = new FormSectionView( | ||
| 'You can change your password here:' | ||
| ); | ||
| this.appendChild(this.form); | ||
| this.form.node.appendChild(this.passwordSection.htmlElement); | ||
| } | ||
|
|
||
| private async getData() { | ||
| try { | ||
| const customer = JSON.parse( | ||
| localStorage.getItem('codecraftCustomer') as string | ||
| ); | ||
| const customerData = await getCustomer(customer.id); | ||
| this.populateForm(customerData); | ||
| this.addConfirmPasswordButton(); | ||
| this.addChangePasswordButton(customerData); | ||
| return customerData; | ||
| } catch (error) { | ||
| showErrorMessage('Error getting customer data'); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private populateForm(customerData: Customer | null) { | ||
| if (customerData) { | ||
| const customer: Customer = customerData; | ||
| if (customer.email !== undefined) { | ||
| this.emailField.input.value = customer.email; | ||
| } else { | ||
| showErrorMessage('Email is undefined'); | ||
| } | ||
| } else { | ||
| showErrorMessage('Error getting customer data'); | ||
| } | ||
| } | ||
|
|
||
| private addCurrentSection() { | ||
| const currentWrapperAttr: IAttributes = { | ||
| tag: 'section', | ||
| classList: 'row col s12', | ||
| }; | ||
| this.currentWrapper = new BaseComponent(currentWrapperAttr); | ||
| this.form.appendChild(this.currentWrapper); | ||
| } | ||
|
|
||
| public addEmailField() { | ||
| this.emailField = createEmailField(); | ||
| this.emailField.input.setDisable(true); | ||
| this.currentWrapper.appendChild(this.emailField); | ||
| } | ||
|
|
||
| public addPasswordField() { | ||
| const updatedAttrs: IFormInputField = { | ||
| id: 'currentPassword', | ||
| label: 'Current password', | ||
| placeholder: 'Enter current password', | ||
| type: 'password', | ||
| customValidators: [], | ||
| }; | ||
| this.passwordField = createPasswordField(updatedAttrs); | ||
| this.currentWrapper.appendChild(this.passwordField); | ||
| } | ||
|
|
||
| public addConfirmPasswordButton() { | ||
| const attrs: IButtonAttributes = { | ||
| type: 'button', | ||
| content: 'Confirm current password', | ||
| onClick: () => { | ||
| const email = this.emailField.getValue(); | ||
| const password = this.passwordField.getValue(); | ||
| this.authenticateCustomer(email, password); | ||
| }, | ||
| }; | ||
| this.button = new ButtonComponent(attrs); | ||
| this.button.addClass('col'); | ||
| this.button.addClass('s6'); | ||
| this.currentWrapper.appendChild(this.button); | ||
|
|
||
| const toggleButtonAttrs: IButtonAttributes = { | ||
| content: 'Show/Hide Password', | ||
| onClick: (event) => { | ||
| if (event) { | ||
| event.preventDefault(); | ||
| } | ||
| this.passwordField.togglePasswordVisibility(); | ||
| this.newPasswordField.togglePasswordVisibility(); | ||
| this.confirmNewPasswordField.togglePasswordVisibility(); | ||
| }, | ||
| }; | ||
|
|
||
| this.showPasswordButton = new ButtonComponent(toggleButtonAttrs); | ||
| this.showPasswordButton.addClass('show-hide'); | ||
| this.form.appendChild(this.showPasswordButton); | ||
| } | ||
|
|
||
| async authenticateCustomer(email: string, password: string) { | ||
| const signInBody: CustomerSignin = { | ||
| email, | ||
| password, | ||
| }; | ||
|
|
||
| try { | ||
| const signInResult = await apiRoot | ||
| .login() | ||
| .post({ body: signInBody }) | ||
| .execute(); | ||
| const customerData = signInResult.body.customer; | ||
| showSucessMessage('Valid password'); | ||
| this.addNewSection(); | ||
| this.addNewPasswordField(); | ||
| this.addChangePasswordButton(customerData); | ||
| return signInResult; | ||
| } catch (error) { | ||
| showErrorMessage('Invalid password'); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| private addNewSection() { | ||
| const newWrapperAttr: IAttributes = { | ||
| tag: 'section', | ||
| classList: 'row col s12', | ||
| }; | ||
| this.newWrapper = new BaseComponent(newWrapperAttr); | ||
| this.form.appendChild(this.newWrapper); | ||
| } | ||
|
|
||
| public addNewPasswordField() { | ||
| const newPasswordAttrs: IFormInputField = { | ||
| id: 'new-password', | ||
| label: 'Enter new password', | ||
| placeholder: 'New password', | ||
| type: 'password', | ||
| customValidators: [passwordValidator, specialCharValidator], | ||
| }; | ||
| this.newPasswordField = createPasswordField(newPasswordAttrs); | ||
| this.newWrapper.appendChild(this.newPasswordField); | ||
|
|
||
| const confirmNewPasswordAttrs: IFormInputField = { | ||
| id: 'confirm-new', | ||
| label: 'Enter new password again', | ||
| placeholder: 'New password', | ||
| type: 'password', | ||
| customValidators: [passwordValidator, specialCharValidator], | ||
| }; | ||
| this.confirmNewPasswordField = createPasswordField(confirmNewPasswordAttrs); | ||
| this.newWrapper.appendChild(this.confirmNewPasswordField); | ||
| } | ||
|
|
||
| public addChangePasswordButton(customerData: Customer | null) { | ||
| const attrs: IButtonAttributes = { | ||
| type: 'button', | ||
| content: 'Save new password', | ||
| onClick: () => this.handlePasswordChange(customerData), | ||
| }; | ||
|
|
||
| this.changePassword = new ButtonComponent(attrs); | ||
| this.changePassword.addClass('col'); | ||
| this.changePassword.addClass('s6'); | ||
| this.newWrapper.appendChild(this.changePassword); | ||
| } | ||
|
|
||
| private handlePasswordChange(customerData: Customer | null) { | ||
| const currentPassword = this.passwordField.getValue().trim(); | ||
| const newPassword = this.newPasswordField.getValue().trim(); | ||
| const confirmedPassword = this.confirmNewPasswordField.getValue().trim(); | ||
| if (customerData && newPassword === confirmedPassword) { | ||
| changeCustomerPassword( | ||
| customerData.id, | ||
| customerData.version, | ||
| currentPassword, | ||
| newPassword | ||
| ) | ||
| .then(() => PasswordChangeView.successChange()) | ||
| .catch(() => showErrorMessage('Passwords do not match')); | ||
| } | ||
| } | ||
|
|
||
| static successChange() { | ||
| const SUCSESS_MSG = 'The password has been changed'; | ||
| Router.navigateTo('#profile'); | ||
| showSucessMessage(SUCSESS_MSG); | ||
| } | ||
| } |
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.
Лишние комменты