Skip to content

Commit

Permalink
Resolved comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshayman committed Aug 18, 2024
1 parent 86a0d78 commit 1cd48ed
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 59 deletions.
105 changes: 53 additions & 52 deletions app/controllers/new-signup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { toastNotificationTimeoutOptions } from '../constants/toast-notification
export default class NewSignUpController extends Controller {
@service analytics;
@service featureFlag;
@service toast;

queryParams = ['currentStep', 'dev'];

Expand Down Expand Up @@ -44,6 +45,9 @@ export default class NewSignUpController extends Controller {
}

async generateUsername(firstname, lastname) {
if (typeof firstname !== 'string' || typeof lastname !== 'string') {
throw new Error('Invalid input: firstname and lastname must be strings');
}
try {
const sanitizedFirstname = firstname.toLowerCase();
const sanitizedLastname = lastname.toLowerCase();
Expand All @@ -63,12 +67,16 @@ export default class NewSignUpController extends Controller {
if (user && user.username) {
return user;
}
throw new Error(
'Username generation failed: Invalid response from server'
);
} catch (err) {
this.toast.error(
ERROR_MESSAGES.usernameGeneration,
'error!',
toastNotificationTimeoutOptions
);
throw new Error(ERROR_MESSAGES.usernameGeneration);
}
}

Expand Down Expand Up @@ -122,34 +130,36 @@ 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: user?.username,
};
const roles = {};
Object.entries(this.signupDetails.roles).forEach(([key, value]) => {
if (value === true) {
roles[key] = value;
}
});
try {
let user;
if (!this.isDevMode)
user = await this.generateUsername(
this.signupDetails.firstName,
this.signupDetails.lastName
);
const signupDetails = {
first_name: this.signupDetails.firstName,
last_name: this.signupDetails.lastName,
username: this.isDevMode ? this.signupDetails.username : user.username,
};
const roles = {};
Object.entries(this.signupDetails.roles).forEach(([key, value]) => {
if (value === true) {
roles[key] = value;
}
});

this.isLoading = true;
this.isLoading = true;

const isUsernameAvailable = await checkUserName(signupDetails.username);
if (!isUsernameAvailable) {
this.analytics.trackEvent(NEW_SIGNUP_FLOW.USERNAME_NOT_AVAILABLE);
this.isLoading = false;
this.isButtonDisabled = false;
return (this.error = ERROR_MESSAGES.userName);
}
const isUsernameAvailable = await checkUserName(signupDetails.username);
if (!isUsernameAvailable) {
this.analytics.trackEvent(NEW_SIGNUP_FLOW.USERNAME_NOT_AVAILABLE);
this.isLoading = false;
this.isButtonDisabled = false;
return (this.error = ERROR_MESSAGES.userName);
}

if (this.isDevMode) {
try {
if (this.isDevMode) {
const res = await newRegisterUser(signupDetails, roles);
if (res.status === 204) {
this.analytics.identifyUser();
Expand All @@ -160,35 +170,26 @@ export default class NewSignUpController extends Controller {
this.error = ERROR_MESSAGES.others;
this.isButtonDisabled = false;
}
} catch (error) {
this.analytics.trackEvent(NEW_SIGNUP_FLOW.UNABLE_TO_REGISTER);
this.error = ERROR_MESSAGES.others;
this.isButtonDisabled = false;
} finally {
this.isLoading = false;
}
} else {
//this will get removed after removing feature flag
registerUser(signupDetails)
.then((res) => {
if (res.status === 204) {
this.analytics.identifyUser();
this.analytics.trackEvent(NEW_SIGNUP_FLOW.USER_REGISTERED);
this.currentStep = this.LAST_STEP;
} else {
this.analytics.trackEvent(NEW_SIGNUP_FLOW.UNABLE_TO_SIGNUP);
this.error = ERROR_MESSAGES.others;
this.isButtonDisabled = false;
}
})
.catch(() => {
this.analytics.trackEvent(NEW_SIGNUP_FLOW.UNABLE_TO_REGISTER);
} else {
//this will get removed after removing feature flag
const res = await registerUser(signupDetails);
if (res.status === 204) {
this.analytics.identifyUser();
this.analytics.trackEvent(NEW_SIGNUP_FLOW.USER_REGISTERED);
this.currentStep = this.LAST_STEP;
} else {
this.analytics.trackEvent(NEW_SIGNUP_FLOW.UNABLE_TO_SIGNUP);
this.error = ERROR_MESSAGES.others;
this.isButtonDisabled = false;
})
.finally(() => {
this.isLoading = false;
});
}
}
} catch (error) {
this.analytics.trackEvent(NEW_SIGNUP_FLOW.UNABLE_TO_REGISTER);
console.log(error);
this.error = error?.message || ERROR_MESSAGES.others;
this.isButtonDisabled = false;
} finally {
this.isLoading = false;
}
}
}
25 changes: 18 additions & 7 deletions app/templates/new-signup.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@
{{/if}}

{{#if (eq this.currentStep this.THIRD_STEP)}}
<NewSignup::Input
@onClick={{this.register}}
@currentStep={{this.currentStep}}
@onChange={{this.handleInputChange}}
@isButtonDisabled={{this.isButtonDisabled}}
@isLoading={{this.isLoading}}
/>
{{#if this.isDevMode}}
<NewSignup::Input
@onClick={{this.changeStepToFour}}
@currentStep={{this.currentStep}}
@onChange={{this.handleInputChange}}
@isButtonDisabled={{this.isButtonDisabled}}
@isLoading={{this.isLoading}}
/>
{{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}}
Expand Down

0 comments on commit 1cd48ed

Please sign in to comment.