2023-12-30 23:41:25 +00:00
|
|
|
import { FormModalUiHandler } from "./form-modal-ui-handler";
|
|
|
|
import { ModalConfig } from "./modal-ui-handler";
|
|
|
|
import * as Utils from "../utils";
|
|
|
|
import { Mode } from "./ui";
|
2024-04-19 00:08:53 +01:00
|
|
|
import { TextStyle, addTextObject } from "./text";
|
2024-04-23 01:39:51 +01:00
|
|
|
import i18next from '../plugins/i18n';
|
2023-12-30 23:41:25 +00:00
|
|
|
|
|
|
|
export default class RegistrationFormUiHandler extends FormModalUiHandler {
|
|
|
|
getModalTitle(config?: ModalConfig): string {
|
2024-04-23 01:39:51 +01:00
|
|
|
return i18next.t('menu:register');
|
2023-12-30 23:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getFields(config?: ModalConfig): string[] {
|
2024-04-23 01:39:51 +01:00
|
|
|
return [ i18next.t('menu:username'), i18next.t('menu:password'), i18next.t('menu:confirmPassword') ];
|
2023-12-30 23:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getWidth(config?: ModalConfig): number {
|
|
|
|
return 160;
|
|
|
|
}
|
|
|
|
|
|
|
|
getMargin(config?: ModalConfig): [number, number, number, number] {
|
|
|
|
return [ 0, 0, 48, 0 ];
|
|
|
|
}
|
|
|
|
|
2024-04-19 00:08:53 +01:00
|
|
|
getButtonTopMargin(): number {
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
2023-12-30 23:41:25 +00:00
|
|
|
getButtonLabels(config?: ModalConfig): string[] {
|
2024-04-23 01:39:51 +01:00
|
|
|
return [ i18next.t('menu:register'), i18next.t('menu:backToLogin') ];
|
2023-12-30 23:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getReadableErrorMessage(error: string): string {
|
|
|
|
let colonIndex = error?.indexOf(':');
|
|
|
|
if (colonIndex > 0)
|
|
|
|
error = error.slice(0, colonIndex);
|
|
|
|
switch (error) {
|
|
|
|
case 'invalid username':
|
2024-04-23 01:39:51 +01:00
|
|
|
return i18next.t('menu:invalidRegisterUsername');
|
2023-12-30 23:41:25 +00:00
|
|
|
case 'invalid password':
|
2024-04-23 01:39:51 +01:00
|
|
|
return i18next.t('menu:invalidRegisterPassword');
|
2023-12-30 23:41:25 +00:00
|
|
|
case 'failed to add account record':
|
2024-04-23 01:39:51 +01:00
|
|
|
return i18next.t('menu:usernameAlreadyUsed');
|
2023-12-30 23:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.getReadableErrorMessage(error);
|
|
|
|
}
|
|
|
|
|
2024-04-19 00:08:53 +01:00
|
|
|
setup(): void {
|
|
|
|
super.setup();
|
|
|
|
|
2024-04-23 01:39:51 +01:00
|
|
|
const label = addTextObject(this.scene, 10, 87, i18next.t('menu:registrationAgeWarning'), TextStyle.TOOLTIP_CONTENT, { fontSize: '42px' });
|
2024-04-19 00:08:53 +01:00
|
|
|
|
|
|
|
this.modalContainer.add(label);
|
|
|
|
}
|
|
|
|
|
2023-12-30 23:41:25 +00:00
|
|
|
show(args: any[]): boolean {
|
|
|
|
if (super.show(args)) {
|
|
|
|
const config = args[0] as ModalConfig;
|
|
|
|
|
|
|
|
const originalRegistrationAction = this.submitAction;
|
|
|
|
this.submitAction = (_) => {
|
|
|
|
// Prevent overlapping overrides on action modification
|
|
|
|
this.submitAction = originalRegistrationAction;
|
|
|
|
this.sanitizeInputs();
|
|
|
|
this.scene.ui.setMode(Mode.LOADING, { buttonActions: [] });
|
|
|
|
const onFail = error => {
|
|
|
|
this.scene.ui.setMode(Mode.REGISTRATION_FORM, Object.assign(config, { errorMessage: error?.trim() }));
|
|
|
|
this.scene.ui.playError();
|
|
|
|
};
|
|
|
|
if (!this.inputs[0].text)
|
2024-04-23 01:39:51 +01:00
|
|
|
return onFail(i18next.t('menu:emptyUsername'));
|
2023-12-30 23:41:25 +00:00
|
|
|
if (!this.inputs[1].text)
|
|
|
|
return onFail(this.getReadableErrorMessage('invalid password'));
|
|
|
|
if (this.inputs[1].text !== this.inputs[2].text)
|
2024-04-23 01:39:51 +01:00
|
|
|
return onFail(i18next.t('menu:passwordNotMatchingConfirmPassword'));
|
2024-04-25 00:08:02 +01:00
|
|
|
Utils.apiPost(`account/register`, `username=${encodeURIComponent(this.inputs[0].text)}&password=${encodeURIComponent(this.inputs[1].text)}`, 'application/x-www-form-urlencoded')
|
2023-12-30 23:41:25 +00:00
|
|
|
.then(response => response.text())
|
|
|
|
.then(response => {
|
|
|
|
if (!response) {
|
2024-04-25 00:08:02 +01:00
|
|
|
Utils.apiPost(`account/login`, `username=${encodeURIComponent(this.inputs[0].text)}&password=${encodeURIComponent(this.inputs[1].text)}`, 'application/x-www-form-urlencoded')
|
2023-12-30 23:41:25 +00:00
|
|
|
.then(response => {
|
|
|
|
if (!response.ok)
|
|
|
|
return response.text();
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
if (response.hasOwnProperty('token')) {
|
|
|
|
Utils.setCookie(Utils.sessionIdKey, response.token);
|
|
|
|
originalRegistrationAction();
|
|
|
|
} else
|
|
|
|
onFail(response);
|
|
|
|
});
|
|
|
|
} else
|
|
|
|
onFail(response);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|