Skip to content

Commit

Permalink
fix title change error
Browse files Browse the repository at this point in the history
  • Loading branch information
Alforoan committed Jun 30, 2024
1 parent 87345f1 commit c4e0084
Showing 1 changed file with 36 additions and 31 deletions.
67 changes: 36 additions & 31 deletions client/src/components/EditBoardName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const EditBoardName: React.FC<EditBoardNameProps> = ({ onSuccess }) => {
setIsEditing(false);
setNewName(originalName); // Revert to the most recently saved board name in case of cancel
updateTitleText();
setError(null);
}, [originalName]);

useEffect(() => {
Expand All @@ -54,9 +55,7 @@ const EditBoardName: React.FC<EditBoardNameProps> = ({ onSuccess }) => {
return;
}

setIsLoading(true);
setError(null);
updateTitleText();

try {
const token = localStorage.getItem("jwt");
const response = await axios.put(
Expand All @@ -79,6 +78,7 @@ const EditBoardName: React.FC<EditBoardNameProps> = ({ onSuccess }) => {
if (axios.isAxiosError(err)) {
if (err.response?.status === 400) {
setError("Board name already exists, please try another.");
return;
} else {
setError("Failed to update board name. Please try again later.");
}
Expand All @@ -87,6 +87,9 @@ const EditBoardName: React.FC<EditBoardNameProps> = ({ onSuccess }) => {
}
setIsLoading(false);
}
setIsLoading(true);
setError(null);
updateTitleText();
};

return (
Expand All @@ -99,35 +102,37 @@ const EditBoardName: React.FC<EditBoardNameProps> = ({ onSuccess }) => {
Edit
</button>
) : (
<>
<input
type="text"
value={newName}
onChange={handleInputChange}
onKeyDown={(e) => {
if (e.key === "Enter") {
handleSubmit();
}
}}
size={newName.length - 1}
className="text-3xl font-bold font-primary border rounded px-2"
maxLength={30}
/>
<button
onClick={handleSubmit}
disabled={isLoading}
className="ml-2 bg-secondaryElements font-primary text-primaryText px-4 py-2 rounded hover:text-primaryTextLighter"
>
Save
</button>
<button
onClick={handleCancel}
className="ml-2 bg-secondaryElements font-primary text-primaryText px-4 py-2 rounded hover:text-primaryTextLighter"
>
Cancel
</button>
<main className="flex-col">
<div className="flex items-center">
<input
type="text"
value={newName}
onChange={handleInputChange}
onKeyDown={(e) => {
if (e.key === "Enter") {
handleSubmit();
}
}}
size={newName.length - 1}
className="text-3xl font-bold font-primary border rounded px-2"
maxLength={30}
/>
<button
onClick={handleSubmit}
disabled={isLoading}
className="ml-2 bg-secondaryElements font-primary text-primaryText px-4 py-2 rounded hover:text-primaryTextLighter"
>
Save
</button>
<button
onClick={handleCancel}
className="ml-2 bg-secondaryElements font-primary text-primaryText px-4 py-2 rounded hover:text-primaryTextLighter"
>
Cancel
</button>
</div>
{error && <p className="text-red-500 mt-2">{error}</p>}
</>
</main>
)}
</div>
);
Expand Down

0 comments on commit c4e0084

Please sign in to comment.