Some fixes to ChangeUsername
This commit is contained in:
		
							parent
							
								
									e06aced6dd
								
							
						
					
					
						commit
						9b0a231701
					
				
					 1 changed files with 18 additions and 53 deletions
				
			
		| 
						 | 
					@ -1,61 +1,26 @@
 | 
				
			||||||
import React, { useState } from "react";
 | 
					import { APIResponse, api } from "../API/API";
 | 
				
			||||||
import InputField from "./InputField";
 | 
					import { StrNameChange } from "../Types/goTypes";
 | 
				
			||||||
import { api } from "../API/API";
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
function ChangeUsername(): JSX.Element {
 | 
					function ChangeUsername(props: { nameChange: StrNameChange }): void {
 | 
				
			||||||
  const [newUsername, setNewUsername] = useState("");
 | 
					  if (props.nameChange.newName === "") {
 | 
				
			||||||
  const [errorMessage, setErrorMessage] = useState("");
 | 
					    alert("You have to select a new name");
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
 | 
					 | 
				
			||||||
    setNewUsername(e.target.value);
 | 
					 | 
				
			||||||
  };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  const handleSubmit = async (): Promise<void> => {
 | 
					 | 
				
			||||||
    try {
 | 
					 | 
				
			||||||
      // Call the API function to change the username
 | 
					 | 
				
			||||||
      const token = localStorage.getItem("accessToken");
 | 
					 | 
				
			||||||
      if (!token) {
 | 
					 | 
				
			||||||
        throw new Error("Access token not found");
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					  api
 | 
				
			||||||
      const response = await api.changeUserName(
 | 
					    .changeUserName(props.nameChange, localStorage.getItem("accessToken") ?? "")
 | 
				
			||||||
        { prevName: "currentName", newName: newUsername },
 | 
					    .then((response: APIResponse<void>) => {
 | 
				
			||||||
        token,
 | 
					 | 
				
			||||||
      );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (response.success) {
 | 
					      if (response.success) {
 | 
				
			||||||
        // Optionally, add a success message or redirect the user
 | 
					        alert("Name changed successfully");
 | 
				
			||||||
        console.log("Username changed successfully");
 | 
					        location.reload();
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        // Handle the error message
 | 
					        alert("Name not changed");
 | 
				
			||||||
        console.error("Failed to change username:", response.message);
 | 
					        console.error(response.message);
 | 
				
			||||||
        setErrorMessage(response.message ?? "Failed to change username");
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    } catch (error) {
 | 
					    })
 | 
				
			||||||
      console.error("Error changing username:", error);
 | 
					    .catch((error) => {
 | 
				
			||||||
      // Optionally, handle the error
 | 
					      alert("Name not changed");
 | 
				
			||||||
      setErrorMessage("Failed to change username");
 | 
					      console.error("An error occurred during change:", error);
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  const handleButtonClick = (): void => {
 | 
					 | 
				
			||||||
    handleSubmit().catch((error) => {
 | 
					 | 
				
			||||||
      console.error("Error in handleSubmit:", error);
 | 
					 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  return (
 | 
					 | 
				
			||||||
    <div>
 | 
					 | 
				
			||||||
      <InputField
 | 
					 | 
				
			||||||
        label="New Username"
 | 
					 | 
				
			||||||
        type="text"
 | 
					 | 
				
			||||||
        value={newUsername}
 | 
					 | 
				
			||||||
        onChange={handleChange}
 | 
					 | 
				
			||||||
      />
 | 
					 | 
				
			||||||
      {errorMessage && <div>{errorMessage}</div>}
 | 
					 | 
				
			||||||
      <button onClick={handleButtonClick}>Update Username</button>
 | 
					 | 
				
			||||||
    </div>
 | 
					 | 
				
			||||||
  );
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default ChangeUsername;
 | 
					export default ChangeUsername;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue