|
| 1 | +import { createPortal } from "react-dom"; |
| 2 | +import { EditBoardModalProps } from "../types"; |
| 3 | +import { useState } from "react"; |
| 4 | +import "../scss/modal.scss"; |
| 5 | + |
| 6 | +const EditBoardModal = ({ |
| 7 | + setEditingBoard, |
| 8 | + setCurrentBoard, |
| 9 | + currentBoard, |
| 10 | +}: EditBoardModalProps) => { |
| 11 | + const [boardName, setBoardName] = useState<string>(currentBoard.name); |
| 12 | + |
| 13 | + const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 14 | + const inputValue: string = e.target.value; |
| 15 | + setBoardName(inputValue.trim()); //edge case for whitespace |
| 16 | + }; |
| 17 | + |
| 18 | + const handleFormSubmit = async (e: React.FormEvent<HTMLFormElement>) => { |
| 19 | + e.preventDefault(); |
| 20 | + // send put request to /boards/edit with new boardName and id in body |
| 21 | + const body = { |
| 22 | + boardName: boardName, |
| 23 | + id: currentBoard.id, |
| 24 | + }; |
| 25 | + const response: Response = await fetch("/boards/edit", { |
| 26 | + method: "PUT", |
| 27 | + headers: { |
| 28 | + "Content-type": "application/json; charset=UTF-8", |
| 29 | + }, |
| 30 | + body: JSON.stringify(body), |
| 31 | + }); |
| 32 | + // if request success, update currentBoard |
| 33 | + if (response.status === 200) { |
| 34 | + const responseData = await response.json(); |
| 35 | + setCurrentBoard({ name: responseData.name, id: responseData._id }); |
| 36 | + setEditingBoard(false); |
| 37 | + } else { |
| 38 | + console.log("Failed to edit board."); |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + const handleDeleteBoard = () => { |
| 43 | + const fetchDeleteBoard = async () => { |
| 44 | + const response: Response = await fetch( |
| 45 | + `/boards/delete/${currentBoard.id}`, |
| 46 | + { |
| 47 | + method: "DELETE", |
| 48 | + } |
| 49 | + ); |
| 50 | + if (response.status === 200) { |
| 51 | + setCurrentBoard({ |
| 52 | + name: "", |
| 53 | + id: "", |
| 54 | + }); |
| 55 | + setEditingBoard(false); |
| 56 | + } |
| 57 | + }; |
| 58 | + fetchDeleteBoard().catch(console.error); |
| 59 | + }; |
| 60 | + |
| 61 | + const isButtonDisabled: boolean = boardName === ""; //checking if boardName is empty? using trim in handle input |
| 62 | + |
| 63 | + return createPortal( |
| 64 | + <div className="modal-overlay"> |
| 65 | + <div className="modal"> |
| 66 | + <form className="modal-form" onSubmit={handleFormSubmit}> |
| 67 | + <h2 className="modal-title">Edit Board</h2> |
| 68 | + <input |
| 69 | + className="modal-input" |
| 70 | + name="boardname" |
| 71 | + type="text" |
| 72 | + value={boardName} |
| 73 | + onChange={handleInputChange} |
| 74 | + required |
| 75 | + /> |
| 76 | + <div className="modal-btns"> |
| 77 | + <button |
| 78 | + className="modal-submit" |
| 79 | + type="submit" |
| 80 | + disabled={isButtonDisabled} |
| 81 | + > |
| 82 | + Save |
| 83 | + </button> |
| 84 | + <button |
| 85 | + className="modal-cancel" |
| 86 | + onClick={() => { |
| 87 | + setEditingBoard(false); |
| 88 | + }} |
| 89 | + > |
| 90 | + Cancel |
| 91 | + </button> |
| 92 | + <button className="modal-delete" onClick={handleDeleteBoard}> |
| 93 | + Delete |
| 94 | + </button> |
| 95 | + </div> |
| 96 | + </form> |
| 97 | + </div> |
| 98 | + </div>, |
| 99 | + document.getElementById("portal") as Element |
| 100 | + ); |
| 101 | +}; |
| 102 | + |
| 103 | +export default EditBoardModal; |
0 commit comments