Fixed and updated some minor error message related stuff

This commit is contained in:
Opaque02 2024-10-10 11:02:00 +10:00
parent 84561dbb98
commit 05be3f2f95

View File

@ -11,15 +11,18 @@ export default class AdminUiHandler extends FormModalUiHandler {
private adminMode: AdminMode; private adminMode: AdminMode;
private adminResult: AdminSearchInfo; // this is the username that we're looking for private adminResult: AdminSearchInfo; // this is the username that we're looking for
private config: ModalConfig; private config: ModalConfig;
private readonly httpUserNotFoundErrorCode: number = 204; // this is the http response from the server when a username isn't found in the server. This has to be the same error the server is giving private readonly httpUserNotFoundErrorCode: number = 404; // this is the http response from the server when a username isn't found in the server. This has to be the same error the server is giving
private readonly buttonGap = 10; private readonly buttonGap = 10;
private readonly ERR_REQUIRED_USERNAME: string = "Username is required"; private readonly ERR_REQUIRED_FIELD = (field: string) => {
private readonly ERR_REQUIRED_DISCORD: string = "Discord Id is required"; if (field === "username") {
private readonly ERR_REQUIRED_USERNAME_OR_DISCORD: string = "Username or discord Id is required"; return `${Utils.formatText(field)} is required`;
private readonly SUCCESS_DISCORD_LINKED: string = "Username and discord successfully linked"; } else {
private readonly SUCCESS_DISCORD_UNLINKED: string = "Username and discord successfully unlinked"; return `${Utils.formatText(field)} Id is required`;
private readonly SUCCESS_GOOGLE_LINKED: string = "Username and google successfully linked"; }
private readonly SUCCESS_GOOGLE_UNLINKED: string = "Username and google successfully unlinked"; };
private readonly SUCCESS_SERVICE_MODE = (service: string, mode: string) => { // this returns a string saying whether a username has been successfully linked/unlinked to discord/google
return `Username and ${service} successfully ${mode}ed`;
};
private readonly ERR_USERNAME_NOT_FOUND: string = "Username not found!"; private readonly ERR_USERNAME_NOT_FOUND: string = "Username not found!";
private readonly ERR_GENERIC_ERROR: string = "There was an error"; private readonly ERR_GENERIC_ERROR: string = "There was an error";
@ -62,6 +65,8 @@ export default class AdminUiHandler extends FormModalUiHandler {
return ["Link Account", "Cancel"]; return ["Link Account", "Cancel"];
case AdminMode.SEARCH: case AdminMode.SEARCH:
return ["Find account", "Cancel"]; return ["Find account", "Cancel"];
case AdminMode.ADMIN:
return ["Back to search", "Cancel"];
default: default:
return ["Activate ADMIN", "Cancel"]; return ["Activate ADMIN", "Cancel"];
} }
@ -119,7 +124,7 @@ export default class AdminUiHandler extends FormModalUiHandler {
if (response.error) { if (response.error) {
return this.showMessage(response.errorType, adminSearchResult, true); return this.showMessage(response.errorType, adminSearchResult, true);
} else { } else {
return this.showMessage(this.SUCCESS_DISCORD_LINKED, adminSearchResult, false); return this.showMessage(this.SUCCESS_SERVICE_MODE("discord", "link"), adminSearchResult, false);
} }
}); });
} else if (this.adminMode === AdminMode.SEARCH) { } else if (this.adminMode === AdminMode.SEARCH) {
@ -130,6 +135,8 @@ export default class AdminUiHandler extends FormModalUiHandler {
} }
this.updateAdminPanelInfo(response.adminSearchResult ?? adminSearchResult); this.updateAdminPanelInfo(response.adminSearchResult ?? adminSearchResult);
}); });
} else if (this.adminMode === AdminMode.ADMIN) {
this.updateAdminPanelInfo(adminSearchResult, AdminMode.SEARCH);
} }
return false; return false;
@ -165,7 +172,14 @@ export default class AdminUiHandler extends FormModalUiHandler {
img.setScale(0.5); img.setScale(0.5);
img.setInteractive(); img.setInteractive();
img.on("pointerdown", () => { img.on("pointerdown", () => {
this.adminLinkUnlink(this.convertInputsToAdmin(), aR.includes("discord") ? "discord" : "google", adminResult[aR] === "" ? "link" : "unlink").then(response => { const service = aR.toLowerCase().replace("id", ""); // this takes our key (discordId or googleId) and removes the "Id" at the end to make it more url friendly
const mode = adminResult[aR] === "" ? "link" : "unlink"; // this figures out if we're linking or unlinking a service
const validFields = this.areFieldsValid(this.adminMode, service);
if (validFields.error) {
this.scene.ui.setMode(Mode.LOADING, { buttonActions: [] }); // this is here to force a loading screen to allow the admin tool to reopen again if there's an error
return this.showMessage(validFields.errorMessage ?? "", adminResult, true);
}
this.adminLinkUnlink(this.convertInputsToAdmin(), service, mode).then(response => {
if (response.error) { if (response.error) {
return this.showMessage(response.errorType, adminResult, true); return this.showMessage(response.errorType, adminResult, true);
} else { } else {
@ -175,7 +189,7 @@ export default class AdminUiHandler extends FormModalUiHandler {
if (response.error) { if (response.error) {
return this.showMessage(response.errorType, adminResult, true); return this.showMessage(response.errorType, adminResult, true);
} }
this.updateAdminPanelInfo(response.adminSearchResult ?? adminResult); return this.showMessage(this.SUCCESS_SERVICE_MODE(service, mode), response.adminSearchResult ?? adminResult, false);
}); });
} }
}); });
@ -193,28 +207,44 @@ export default class AdminUiHandler extends FormModalUiHandler {
} }
} }
areFieldsValid(adminMode: AdminMode): { error: boolean; errorMessage?: string; } { areFieldsValid(adminMode: AdminMode, service?: string): { error: boolean; errorMessage?: string; } {
switch (adminMode) { switch (adminMode) {
case AdminMode.LINK: case AdminMode.LINK:
if (!this.inputs[0].text) { if (!this.inputs[0].text) { // username missing from link panel
return { return {
error: true, error: true,
errorMessage: this.ERR_REQUIRED_USERNAME errorMessage: this.ERR_REQUIRED_FIELD("username")
}; };
} }
if (!this.inputs[1].text) { if (!this.inputs[1].text) { // discordId missing from linking panel
return { return {
error: true, error: true,
errorMessage: this.ERR_REQUIRED_DISCORD errorMessage: this.ERR_REQUIRED_FIELD("discord")
}; };
} }
break;
case AdminMode.SEARCH: case AdminMode.SEARCH:
if (!this.inputs[0].text) { if (!this.inputs[0].text) { // either username or discordId missing from search panel
return { return {
error: true, error: true,
errorMessage: this.ERR_REQUIRED_USERNAME_OR_DISCORD errorMessage: this.ERR_REQUIRED_FIELD("username or discord")
}; };
} }
break;
case AdminMode.ADMIN:
if (!this.inputs[1].text && service === "discord") { // discordId missing from admin panel
return {
error: true,
errorMessage: this.ERR_REQUIRED_FIELD(service)
};
}
if (!this.inputs[2].text && service === "google") { // googleId missing from admin panel
return {
error: true,
errorMessage: this.ERR_REQUIRED_FIELD(service)
};
}
break;
} }
return { return {
error: false error: false
@ -234,14 +264,9 @@ export default class AdminUiHandler extends FormModalUiHandler {
async adminSearch(adminSearchResult: AdminSearchInfo) { async adminSearch(adminSearchResult: AdminSearchInfo) {
try { try {
const adminInfo = await Utils.apiFetch(`admin/account/admin-search?username=${encodeURIComponent(adminSearchResult.username)}`, true); const adminInfo = await Utils.apiFetch(`admin/account/admin-search?username=${encodeURIComponent(adminSearchResult.username)}`, true);
if (!adminInfo.ok) { if (!adminInfo.ok) { // error - if adminInfo.status === this.httpUserNotFoundErrorCode that means the username can't be found in the db
console.error(adminInfo); return { adminSearchResult: adminSearchResult, error: true, errorType: adminInfo.status === this.httpUserNotFoundErrorCode ? this.ERR_USERNAME_NOT_FOUND : this.ERR_GENERIC_ERROR };
console.log(adminSearchResult); } else { // success
return { adminSearchResult: adminSearchResult, error: true, errorType: this.ERR_GENERIC_ERROR };
} else if (adminInfo.status === this.httpUserNotFoundErrorCode) { // username doesn't exist
console.log(adminSearchResult);
return { adminSearchResult: adminSearchResult, error: true, errorType: this.ERR_USERNAME_NOT_FOUND };
} else {
const adminInfoJson: AdminSearchInfo = await adminInfo.json(); const adminInfoJson: AdminSearchInfo = await adminInfo.json();
return { adminSearchResult: adminInfoJson, error: false }; return { adminSearchResult: adminInfoJson, error: false };
} }
@ -254,22 +279,11 @@ export default class AdminUiHandler extends FormModalUiHandler {
async adminLinkUnlink(adminSearchResult: AdminSearchInfo, service: string, mode: string) { async adminLinkUnlink(adminSearchResult: AdminSearchInfo, service: string, mode: string) {
try { try {
const response = await Utils.apiPost(`admin/account/${service}-${mode}`, `username=${encodeURIComponent(adminSearchResult.username)}&${service}Id=${encodeURIComponent(service === "discord" ? adminSearchResult.discordId : adminSearchResult.googleId)}`, "application/x-www-form-urlencoded", true); const response = await Utils.apiPost(`admin/account/${service}-${mode}`, `username=${encodeURIComponent(adminSearchResult.username)}&${service}Id=${encodeURIComponent(service === "discord" ? adminSearchResult.discordId : adminSearchResult.googleId)}`, "application/x-www-form-urlencoded", true);
//.then(response => { if (!response.ok) { // error - if response.status === this.httpUserNotFoundErrorCode that means the username can't be found in the db
if (!response.ok) { return { adminSearchResult: adminSearchResult, error: true, errorType: response.status === this.httpUserNotFoundErrorCode ? this.ERR_USERNAME_NOT_FOUND : this.ERR_GENERIC_ERROR };
console.error(response); } else { // success!
return { adminSearchResult: adminSearchResult, error: true, errorType: this.ERR_GENERIC_ERROR };
} else if (response.status === this.httpUserNotFoundErrorCode) { // username doesn't exist
return { adminSearchResult: adminSearchResult, error: true, errorType: this.ERR_USERNAME_NOT_FOUND };
} else {
//let successString: string = "";
//if (service === "discord") {
// successString = mode === "link" ? this.SUCCESS_DISCORD_LINKED : this.SUCCESS_DISCORD_UNLINKED;
//} else if (service === "google") {
// successString = mode === "link" ? this.SUCCESS_GOOGLE_LINKED : this.SUCCESS_GOOGLE_UNLINKED;
//}
return { adminSearchResult: adminSearchResult, error: false }; return { adminSearchResult: adminSearchResult, error: false };
} }
//})
} catch (err) { } catch (err) {
console.error(err); console.error(err);
return { error: true, errorType: err }; return { error: true, errorType: err };