|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { Button, FormControlLabel, TextField } from "@mui/material"; |
| 5 | +import Navbar from "../../../components/Navbar/Navbar"; |
| 6 | +import styles from "./styles.module.css"; |
| 7 | +import { |
| 8 | + Descendant, |
| 9 | + DescendantObjectsArray, |
| 10 | +} from "../../../models/descendantsTypes"; |
| 11 | +import { |
| 12 | + getDescendants, |
| 13 | + saveDescendants, |
| 14 | +} from "../../../scripts/cacheManager/descendantsCRUD"; |
| 15 | +import { TraitObjectsArray } from "../../../models/traitsTypes"; |
| 16 | +import { |
| 17 | + getTraits, |
| 18 | + saveTraits, |
| 19 | +} from "../../../scripts/cacheManager/traitsCRUD"; |
| 20 | +import BpCheckbox from "../../../components/BpCheckbox/BpCheckbox"; |
| 21 | +import DescendantListItem from "../../../components/DescendantListItem/DescendantListItem"; |
| 22 | + |
| 23 | +export default function AddDescendantsPage() { |
| 24 | + // constants: |
| 25 | + |
| 26 | + const [inputValue, setInputValue] = useState(""); |
| 27 | + const setInputValueFunc = (e: any) => { |
| 28 | + setInputValue(e.target.value); |
| 29 | + }; |
| 30 | + |
| 31 | + const [newTraitInputValue, setNewTraitInputValue] = useState<string>(""); |
| 32 | + const setNewTraitInputValueFunc = (e: any) => { |
| 33 | + setNewTraitInputValue(e.target.value); |
| 34 | + }; |
| 35 | + const [isSaved, setIsSaved] = useState<boolean>(false); |
| 36 | + const [descendantObjectsArray, setDescendantObjectsArray] = |
| 37 | + useState<DescendantObjectsArray>([]); |
| 38 | + const [traitObjectsArray, setTraitObjectsArray] = useState<TraitObjectsArray>( |
| 39 | + [] |
| 40 | + ); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + // get the cached descendants object array |
| 44 | + const cachedDescendantObjectsArray: DescendantObjectsArray | undefined = |
| 45 | + getDescendants(); |
| 46 | + if (cachedDescendantObjectsArray) { |
| 47 | + setDescendantObjectsArray(cachedDescendantObjectsArray); |
| 48 | + } |
| 49 | + |
| 50 | + // get the cached traits object array |
| 51 | + const cachedTraitsObject: TraitObjectsArray | undefined = getTraits(); |
| 52 | + if (cachedTraitsObject) { |
| 53 | + setTraitObjectsArray(cachedTraitsObject); |
| 54 | + } |
| 55 | + }, []); |
| 56 | + |
| 57 | + function createDescendant(descendantName: string) { |
| 58 | + if (inputValue.trim() !== "") { |
| 59 | + const lastDescendantAdded = descendantObjectsArray[0]; |
| 60 | + const lastDescendantAddedId = lastDescendantAdded |
| 61 | + ? lastDescendantAdded.id + 1 |
| 62 | + : 1; // autoincrement id system |
| 63 | + |
| 64 | + const newDescendant: Descendant = { |
| 65 | + id: lastDescendantAddedId, |
| 66 | + descendantName, |
| 67 | + traitsIds: [], |
| 68 | + active: true, |
| 69 | + }; |
| 70 | + |
| 71 | + // save new descendant in the array |
| 72 | + setDescendantObjectsArray([newDescendant, ...descendantObjectsArray]); |
| 73 | + setInputValue(""); // Reset inputValue |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // const handleTraitChange = (traitId: number, descendantId: number) => { |
| 78 | + // const descendantThatWillBeChanged = descendantObjectsArray.find( |
| 79 | + // (descendant) => descendant.id === descendantId, |
| 80 | + // ); |
| 81 | + // if (descendantThatWillBeChanged) { |
| 82 | + // const alreadyAddedTraitsInDescendant = |
| 83 | + // descendantThatWillBeChanged.traitsIds; |
| 84 | + // const newDescendant: Descendant = { |
| 85 | + // id: descendantThatWillBeChanged.id, |
| 86 | + // descendantName: descendantThatWillBeChanged.descendantName, |
| 87 | + // traitsIds: [...alreadyAddedTraitsInDescendant, traitId], |
| 88 | + // active: descendantThatWillBeChanged.active, |
| 89 | + // }; |
| 90 | + |
| 91 | + // const descendantIndex = descendantObjectsArray.findIndex( |
| 92 | + // (descendant) => descendant === descendantThatWillBeChanged, |
| 93 | + // ); |
| 94 | + |
| 95 | + // const newDescendantObjectsArray = descendantObjectsArray; |
| 96 | + // newDescendantObjectsArray[descendantIndex] = newDescendant; |
| 97 | + |
| 98 | + // setDescendantObjectsArray(newDescendantObjectsArray); |
| 99 | + // } |
| 100 | + // }; |
| 101 | + |
| 102 | + /** |
| 103 | + * Handles the change of traits for a descendant. |
| 104 | + * @param traitId - Id of the trait |
| 105 | + * @param descendantId - Id of the descendant to be changed |
| 106 | + * @param remove - Boolean indicating whether the trait is to be added or removed |
| 107 | + */ |
| 108 | + const handleTraitChange = ( |
| 109 | + traitId: number, |
| 110 | + descendantId: number, |
| 111 | + remove: boolean | undefined |
| 112 | + ) => { |
| 113 | + const descendantToChange = descendantObjectsArray.find( |
| 114 | + (descendant) => descendant.id === descendantId |
| 115 | + ); |
| 116 | + |
| 117 | + if (descendantToChange) { |
| 118 | + const { traitsIds } = descendantToChange; |
| 119 | + |
| 120 | + if (!remove) { |
| 121 | + const newTraitsIds = traitsIds ? [...traitsIds, traitId] : [traitId]; |
| 122 | + const newDescendant = { |
| 123 | + ...descendantToChange, |
| 124 | + traitsIds: newTraitsIds, |
| 125 | + }; |
| 126 | + const descendantIndex = descendantObjectsArray.findIndex( |
| 127 | + (descendant) => descendant.id === descendantToChange.id |
| 128 | + ); |
| 129 | + const newDescendantObjectsArray = [...descendantObjectsArray]; |
| 130 | + newDescendantObjectsArray[descendantIndex] = newDescendant; |
| 131 | + setDescendantObjectsArray(newDescendantObjectsArray); |
| 132 | + } else if (remove === true) { |
| 133 | + if (traitsIds) { |
| 134 | + const indexOfTraitToBeDeleted = traitsIds.indexOf(traitId); |
| 135 | + |
| 136 | + if (indexOfTraitToBeDeleted !== -1) { |
| 137 | + const newTraitsIds = [...traitsIds]; |
| 138 | + newTraitsIds.splice(indexOfTraitToBeDeleted, 1); |
| 139 | + |
| 140 | + const newDescendant = { |
| 141 | + ...descendantToChange, |
| 142 | + traitsIds: newTraitsIds, |
| 143 | + }; |
| 144 | + const descendantIndex = descendantObjectsArray.findIndex( |
| 145 | + (descendant) => descendant.id === descendantToChange.id |
| 146 | + ); |
| 147 | + |
| 148 | + const newDescendantObjectsArray = [...descendantObjectsArray]; |
| 149 | + newDescendantObjectsArray[descendantIndex] = newDescendant; |
| 150 | + |
| 151 | + setDescendantObjectsArray(newDescendantObjectsArray); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + const saveTraitsAndDescendantsInSessionStorage = () => { |
| 159 | + setIsSaved(true); |
| 160 | + saveTraits(traitObjectsArray); |
| 161 | + saveDescendants(descendantObjectsArray); |
| 162 | + }; |
| 163 | + |
| 164 | + const addInputValueToTraitsArray = () => { |
| 165 | + // Check for non-empty input |
| 166 | + if (newTraitInputValue.trim() !== "") { |
| 167 | + const lastTraitAdded = traitObjectsArray[0]; |
| 168 | + const lastTraitAddedId = lastTraitAdded ? lastTraitAdded.id + 1 : 1; // autoincrement id system |
| 169 | + |
| 170 | + const newTraitObjectsArray: TraitObjectsArray = [ |
| 171 | + { |
| 172 | + id: lastTraitAddedId, // autoincrement system for those ids |
| 173 | + traitName: newTraitInputValue, |
| 174 | + lastTraitName: undefined, |
| 175 | + active: true, |
| 176 | + }, |
| 177 | + ...traitObjectsArray, |
| 178 | + ]; |
| 179 | + setTraitObjectsArray(newTraitObjectsArray); |
| 180 | + |
| 181 | + setNewTraitInputValue(""); // Reset inputValue |
| 182 | + } |
| 183 | + }; |
| 184 | + |
| 185 | + const deleteDescendantFromArray = (descendantId: number) => { |
| 186 | + const newDescendants = descendantObjectsArray.map((descendant) => { |
| 187 | + if (descendant.id === descendantId) { |
| 188 | + return { ...descendant, active: false }; |
| 189 | + } |
| 190 | + return descendant; |
| 191 | + }); |
| 192 | + |
| 193 | + setDescendantObjectsArray(newDescendants); |
| 194 | + }; |
| 195 | + |
| 196 | + const editDescendantFromArray = ( |
| 197 | + descendantId: number, |
| 198 | + newDescendantName: string |
| 199 | + ) => { |
| 200 | + const updatedDescendants = descendantObjectsArray.map( |
| 201 | + (descendant: Descendant) => { |
| 202 | + if (descendant.id === descendantId) { |
| 203 | + return { |
| 204 | + ...descendant, |
| 205 | + descendantName: newDescendantName, |
| 206 | + }; |
| 207 | + } |
| 208 | + return descendant; |
| 209 | + } |
| 210 | + ); |
| 211 | + setDescendantObjectsArray(updatedDescendants); |
| 212 | + }; |
| 213 | + |
| 214 | + return ( |
| 215 | + <> |
| 216 | + <Navbar /> |
| 217 | + <div className={styles.main}> |
| 218 | + <div className={styles.inputs}> |
| 219 | + <div className={styles.newDescendantInput}> |
| 220 | + <TextField |
| 221 | + label="Nome do descendente:" |
| 222 | + variant="standard" |
| 223 | + size="small" |
| 224 | + onChange={setInputValueFunc} |
| 225 | + value={inputValue} |
| 226 | + id="descendantNameInput" |
| 227 | + /> |
| 228 | + <div className={styles.addDescendantButton}> |
| 229 | + <Button |
| 230 | + onClick={() => createDescendant(inputValue)} |
| 231 | + variant="contained" |
| 232 | + style={{ height: "100%" }} |
| 233 | + id="addDescendantButton" |
| 234 | + > |
| 235 | + Adicionar |
| 236 | + </Button> |
| 237 | + </div> |
| 238 | + </div> |
| 239 | + <div className={styles.newTraitInput}> |
| 240 | + <TextField |
| 241 | + label="Adicionar características:" |
| 242 | + variant="standard" |
| 243 | + size="small" |
| 244 | + onChange={setNewTraitInputValueFunc} |
| 245 | + value={newTraitInputValue} |
| 246 | + /> |
| 247 | + <div> |
| 248 | + <Button |
| 249 | + variant="contained" |
| 250 | + style={{ marginLeft: "2vw", height: "100%" }} |
| 251 | + onClick={addInputValueToTraitsArray} |
| 252 | + > |
| 253 | + Adicionar |
| 254 | + </Button> |
| 255 | + </div> |
| 256 | + </div> |
| 257 | + </div> |
| 258 | + <div className={styles.descendantsContainer}> |
| 259 | + <h1 className={styles.title}>Descendentes:</h1> |
| 260 | + <div className={styles.descendantsContainerActionArea}> |
| 261 | + <div className={styles.descendantsArea}> |
| 262 | + {descendantObjectsArray.map((descendant: Descendant) => { |
| 263 | + if (descendant.active === true) { |
| 264 | + return ( |
| 265 | + <div className={styles.descendantsItems}> |
| 266 | + <DescendantListItem |
| 267 | + id={descendant.id} |
| 268 | + value={descendant.descendantName} |
| 269 | + trashFunc={() => |
| 270 | + deleteDescendantFromArray(descendant.id) |
| 271 | + } |
| 272 | + pencilFunc={editDescendantFromArray} |
| 273 | + /> |
| 274 | + {traitObjectsArray.map((trait) => { |
| 275 | + if (trait.active === true) { |
| 276 | + return ( |
| 277 | + <FormControlLabel |
| 278 | + key={descendant.id + trait.id} |
| 279 | + control={ |
| 280 | + <BpCheckbox |
| 281 | + id={`trait${trait.id}descendant${descendant.id}`} |
| 282 | + checked={descendant.traitsIds?.includes( |
| 283 | + trait.id |
| 284 | + )} // Checking if the trait exists in externalGroup traits array |
| 285 | + onChange={() => |
| 286 | + handleTraitChange( |
| 287 | + trait.id, |
| 288 | + descendant.id, |
| 289 | + descendant.traitsIds?.includes(trait.id) |
| 290 | + ) |
| 291 | + } |
| 292 | + /> |
| 293 | + } |
| 294 | + label={trait.traitName} |
| 295 | + /> |
| 296 | + ); |
| 297 | + } |
| 298 | + return null; |
| 299 | + })} |
| 300 | + </div> |
| 301 | + ); |
| 302 | + } |
| 303 | + return null; |
| 304 | + })} |
| 305 | + </div> |
| 306 | + {isSaved ? ( |
| 307 | + <Button |
| 308 | + variant="contained" |
| 309 | + color="success" |
| 310 | + onClick={() => saveTraitsAndDescendantsInSessionStorage()} |
| 311 | + style={{ marginTop: "2vh" }} |
| 312 | + > |
| 313 | + Salvar |
| 314 | + </Button> |
| 315 | + ) : ( |
| 316 | + <Button |
| 317 | + variant="outlined" |
| 318 | + onClick={() => saveTraitsAndDescendantsInSessionStorage()} |
| 319 | + style={{ marginTop: "2vh" }} |
| 320 | + > |
| 321 | + Salvar |
| 322 | + </Button> |
| 323 | + )} |
| 324 | + </div> |
| 325 | + </div> |
| 326 | + </div> |
| 327 | + {/* <Button onClick={() => console.log(descendantObjectsArray)}>ver</Button> */} |
| 328 | + </> |
| 329 | + ); |
| 330 | +} |
0 commit comments