From 86a0d789a758b16e552825780a8dc96658d001a8 Mon Sep 17 00:00:00 2001 From: Lakshay Manchanda Date: Sun, 18 Aug 2024 01:32:31 +0530 Subject: [PATCH] Generate username automatically - new signup --- app/components/new-signup/input.hbs | 2 +- app/constants/new-signup.js | 1 + app/controllers/new-signup.js | 37 ++++++++++++++++++++++++++++- app/templates/new-signup.hbs | 17 +++---------- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/app/components/new-signup/input.hbs b/app/components/new-signup/input.hbs index 6fdd5fa0..a03842db 100644 --- a/app/components/new-signup/input.hbs +++ b/app/components/new-signup/input.hbs @@ -25,7 +25,7 @@ {{#if @dev}} Next {{else}} - {{if (eq @currentStep 'username') 'Submit' 'Next'}} + {{if (eq @currentStep 'lastName') 'Submit' 'Next'}} {{/if}} diff --git a/app/constants/new-signup.js b/app/constants/new-signup.js index 81fd0bc5..e7a374cb 100644 --- a/app/constants/new-signup.js +++ b/app/constants/new-signup.js @@ -24,6 +24,7 @@ export const LABEL_TEXT = { export const ERROR_MESSAGES = { userName: 'username already taken!', others: 'something went wrong', + usernameGeneration: 'Username cannot be generated', }; export const CHECK_BOX_DATA = [ diff --git a/app/controllers/new-signup.js b/app/controllers/new-signup.js index 1c541ea5..4b61935f 100644 --- a/app/controllers/new-signup.js +++ b/app/controllers/new-signup.js @@ -7,6 +7,8 @@ import { GOTO_URL } from '../constants/url'; import { NEW_SIGNUP_FLOW } from '../constants/analytics'; import { ERROR_MESSAGES, NEW_SIGNUP_STEPS } from '../constants/new-signup'; import checkUserName from '../utils/check-username'; +import ENV from 'website-my/config/environment'; +import { toastNotificationTimeoutOptions } from '../constants/toast-notification'; export default class NewSignUpController extends Controller { @service analytics; @@ -41,6 +43,35 @@ export default class NewSignUpController extends Controller { this.analytics.trackEvent(NEW_SIGNUP_FLOW.USER_GETTING_STARTED); } + async generateUsername(firstname, lastname) { + try { + const sanitizedFirstname = firstname.toLowerCase(); + const sanitizedLastname = lastname.toLowerCase(); + + const response = await fetch( + `${ENV.BASE_API_URL}/users/username?dev=true&firstname=${sanitizedFirstname}&lastname=${sanitizedLastname}`, + { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + credentials: 'include', + } + ); + const user = await response.json(); + + if (user && user.username) { + return user; + } + } catch (err) { + this.toast.error( + ERROR_MESSAGES.usernameGeneration, + 'error!', + toastNotificationTimeoutOptions + ); + } + } + @action changeStepToThree() { this.currentStep = this.THIRD_STEP; this.analytics.trackEvent(NEW_SIGNUP_FLOW.USER_FIRST_NAME); @@ -91,10 +122,14 @@ export default class NewSignUpController extends Controller { } @action async signup() { + const user = await this.generateUsername( + this.signupDetails.firstName, + this.signupDetails.lastName + ); const signupDetails = { first_name: this.signupDetails.firstName, last_name: this.signupDetails.lastName, - username: this.signupDetails.username, + username: user?.username, }; const roles = {}; Object.entries(this.signupDetails.roles).forEach(([key, value]) => { diff --git a/app/templates/new-signup.hbs b/app/templates/new-signup.hbs index 3239b741..3887e6bc 100644 --- a/app/templates/new-signup.hbs +++ b/app/templates/new-signup.hbs @@ -19,7 +19,7 @@ {{#if (eq this.currentStep this.THIRD_STEP)}} {{/if}} -{{#if (eq this.currentStep this.FOURTH_STEP)}} - {{#if this.isDevMode}} +{{#if this.isDevMode}} + {{#if (eq this.currentStep this.FOURTH_STEP)}} - {{else}} - {{/if}} -{{/if}} -{{#if this.isDevMode}} {{#if (eq this.currentStep this.FIFTH_STEP)}}