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

Generate username automatically - new signup #604

Merged
merged 4 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion app/components/new-signup/input.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{{#if @dev}}
Next
{{else}}
{{if (eq @currentStep 'username') 'Submit' 'Next'}}
{{if (eq @currentStep 'lastName') 'Submit' 'Next'}}
{{/if}}
</Button>
</div>
Expand Down
1 change: 1 addition & 0 deletions app/constants/new-signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
37 changes: 36 additions & 1 deletion app/controllers/new-signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +43,35 @@ export default class NewSignUpController extends Controller {
this.analytics.trackEvent(NEW_SIGNUP_FLOW.USER_GETTING_STARTED);
}

async generateUsername(firstname, lastname) {
iamitprakash marked this conversation as resolved.
Show resolved Hide resolved
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);
Expand Down Expand Up @@ -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,
lakshayman marked this conversation as resolved.
Show resolved Hide resolved
};
const roles = {};
Object.entries(this.signupDetails.roles).forEach(([key, value]) => {
Expand Down
17 changes: 3 additions & 14 deletions app/templates/new-signup.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@

{{#if (eq this.currentStep this.THIRD_STEP)}}
<NewSignup::Input
@onClick={{this.changeStepToFour}}
@onClick={{this.register}}
@currentStep={{this.currentStep}}
@onChange={{this.handleInputChange}}
@isButtonDisabled={{this.isButtonDisabled}}
@isLoading={{this.isLoading}}
/>
{{/if}}

{{#if (eq this.currentStep this.FOURTH_STEP)}}
{{#if this.isDevMode}}
{{#if this.isDevMode}}
{{#if (eq this.currentStep this.FOURTH_STEP)}}
<NewSignup::Input
@onClick={{this.changeStepToFive}}
@currentStep={{this.currentStep}}
Expand All @@ -38,19 +38,8 @@
@isLoading={{this.isLoading}}
@error={{this.error}}
/>
{{else}}
<NewSignup::Input
@onClick={{this.register}}
@currentStep={{this.currentStep}}
@onChange={{this.handleInputChange}}
@isButtonDisabled={{this.isButtonDisabled}}
@isLoading={{this.isLoading}}
@error={{this.error}}
/>
{{/if}}
{{/if}}

{{#if this.isDevMode}}
{{#if (eq this.currentStep this.FIFTH_STEP)}}
<NewSignup::Checkbox
@onClick={{this.register}}
Expand Down
Loading