implementing changeUsername component
This commit is contained in:
parent
3e11b87eee
commit
1950112202
1 changed files with 30 additions and 10 deletions
|
@ -1,23 +1,41 @@
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import InputField from "./InputField";
|
import InputField from "./InputField";
|
||||||
|
import { api } from "../API/API";
|
||||||
|
|
||||||
function ChangeUsername(): JSX.Element {
|
function ChangeUsername(): JSX.Element {
|
||||||
const [newUsername, setNewUsername] = useState("");
|
const [newUsername, setNewUsername] = useState("");
|
||||||
|
const [errorMessage, setErrorMessage] = useState("");
|
||||||
|
|
||||||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
|
||||||
setNewUsername(e.target.value);
|
setNewUsername(e.target.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
// const handleSubmit = async (): Promise<void> => {
|
const handleSubmit = async (): Promise<void> => {
|
||||||
// try {
|
try {
|
||||||
// // Call the API function to update the username
|
// Call the API function to change the username
|
||||||
// await api.updateUsername(newUsername);
|
const response = await api.changeUserName(
|
||||||
// // Optionally, add a success message or redirect the user
|
{ prevName: "currentName", newName: newUsername },
|
||||||
// } catch (error) {
|
"token",
|
||||||
// console.error("Error updating username:", error);
|
);
|
||||||
// // Optionally, handle the error
|
if (response.success) {
|
||||||
// }
|
// Optionally, add a success message or redirect the user
|
||||||
// };
|
console.log("Username changed successfully");
|
||||||
|
} else {
|
||||||
|
// Handle the error message
|
||||||
|
console.error("Failed to change username:", response.message);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error changing username:", error);
|
||||||
|
// Optionally, handle the error
|
||||||
|
setErrorMessage("Failed to change username");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleButtonClick = (): void => {
|
||||||
|
handleSubmit().catch((error) => {
|
||||||
|
console.error("Error in handleSubmit:", error);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
|
@ -27,6 +45,8 @@ function ChangeUsername(): JSX.Element {
|
||||||
value={newUsername}
|
value={newUsername}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
/>
|
/>
|
||||||
|
{errorMessage && <div>{errorMessage}</div>}
|
||||||
|
<button onClick={handleButtonClick}>Update Username</button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue