From 8c6061396d32860bdf902ac9671acd9536b3fd1e Mon Sep 17 00:00:00 2001 From: Dave Walker Date: Sun, 3 Dec 2023 10:18:39 +0000 Subject: [PATCH] Added equipment list (WIP) --- src/music-catalogue-ui/components/app.js | 3 + .../components/componentPicker.js | 9 ++ .../equipment/deleteEquipmentActionIcon.js | 64 +++++++++++++ .../components/equipment/equipmentList.js | 79 +++++++++++++++ .../equipment/equipmentList.module.css | 9 ++ .../components/equipment/equipmentRow.js | 96 +++++++++++++++++++ .../deleteManufacturerActionIcon.js | 4 +- src/music-catalogue-ui/components/menuBar.js | 2 +- src/music-catalogue-ui/helpers/navigation.js | 2 + 9 files changed, 265 insertions(+), 3 deletions(-) create mode 100644 src/music-catalogue-ui/components/equipment/deleteEquipmentActionIcon.js create mode 100644 src/music-catalogue-ui/components/equipment/equipmentList.js create mode 100644 src/music-catalogue-ui/components/equipment/equipmentList.module.css create mode 100644 src/music-catalogue-ui/components/equipment/equipmentRow.js diff --git a/src/music-catalogue-ui/components/app.js b/src/music-catalogue-ui/components/app.js index 77db2c1..ca2cb78 100644 --- a/src/music-catalogue-ui/components/app.js +++ b/src/music-catalogue-ui/components/app.js @@ -25,6 +25,7 @@ const defaultContext = { // Equipment registry equipmentType: null, manufacturer: null, + equipment: null, // Data retrieval/filering criteria filter: "A", @@ -48,6 +49,7 @@ const App = () => { genre = null, equipmentType = null, manufacturer = null, + equipment = null, filter = "A", isWishList = false, } = {}) => { @@ -61,6 +63,7 @@ const App = () => { genre: typeof genre != "undefined" ? genre : null, equipmentType: typeof equipmentType != "undefined" ? equipmentType : null, manufacturer: typeof manufacturer != "undefined" ? manufacturer : null, + equipment: typeof equipment != "undefined" ? equipment : null, filter: typeof filter != "undefined" ? filter : "A", isWishList: typeof isWishList != "undefined" ? isWishList : false, }; diff --git a/src/music-catalogue-ui/components/componentPicker.js b/src/music-catalogue-ui/components/componentPicker.js index df4a0d6..def9126 100644 --- a/src/music-catalogue-ui/components/componentPicker.js +++ b/src/music-catalogue-ui/components/componentPicker.js @@ -21,6 +21,7 @@ import EquipmentTypeList from "./equipmentTypes/equipmentTypeList"; import EquipmentTypeEditor from "./equipmentTypes/equipmentTypeEditor"; import ManufacturerList from "./manufacturers/manufacturerList"; import ManufacturerEditor from "./manufacturers/manufacturerEditor"; +import EquipmentList from "./equipment/equipmentList"; /** * Component using the current context to select and render the current page @@ -141,6 +142,14 @@ const ComponentPicker = ({ context, navigate, logout }) => { logout={logout} /> ); + case pages.equipment: + return ( + + ); case pages.export: return ; case pages.artistStatisticsReport: diff --git a/src/music-catalogue-ui/components/equipment/deleteEquipmentActionIcon.js b/src/music-catalogue-ui/components/equipment/deleteEquipmentActionIcon.js new file mode 100644 index 0000000..41362cc --- /dev/null +++ b/src/music-catalogue-ui/components/equipment/deleteEquipmentActionIcon.js @@ -0,0 +1,64 @@ +import { useCallback } from "react"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; +import { faTrashAlt } from "@fortawesome/free-solid-svg-icons"; +import { + apiDeleteEquipment, + apiFetchEquipment, +} from "@/helpers/api/apiEquipment"; + +/** + * Icon and associated action to delete an item of equipment + * @param {*} equipment + * @param {*} isWishList + * @param {*} logout + * @param {*} setEquipment + * @param {*} setError + * @returns + */ +const DeleteEquipmentActionIcon = ({ + equipment, + isWishList, + logout, + setEquipment, + setError, +}) => { + /* Callback to prompt for confirmation and delete an item of equipment */ + const confirmDeleteEquipment = useCallback( + async (e) => { + // Prevent the default action associated with the click event + e.preventDefault(); + + // Show a confirmation message and get the user response + const message = `This will delete the equipment "${equipment.description}" - click OK to confirm`; + const result = confirm(message); + + // If they've confirmed the deletion ... + if (result) { + // ... delete the equipment and confirm the API call was successful + try { + const result = await apiDeleteEquipment(equipment.id, logout); + if (result) { + // Successful, so refresh the equipment list + const fetchedEquipment = await apiFetchEquipment( + isWishList, + logout + ); + setEquipment(fetchedEquipment); + } + } catch (ex) { + setError(`Error deleting the equipment: ${ex.message}`); + } + } + }, + [equipment, isWishList, logout, setEquipment, setError] + ); + + return ( + confirmDeleteEquipment(e)} + /> + ); +}; + +export default DeleteEquipmentActionIcon; diff --git a/src/music-catalogue-ui/components/equipment/equipmentList.js b/src/music-catalogue-ui/components/equipment/equipmentList.js new file mode 100644 index 0000000..cdc1e9b --- /dev/null +++ b/src/music-catalogue-ui/components/equipment/equipmentList.js @@ -0,0 +1,79 @@ +import styles from "./equipmentList.module.css"; +import pages from "@/helpers/navigation"; +import useEquipment from "@/hooks/useEquipment"; +import { useState } from "react"; +import EquipmentRow from "./equipmentRow"; + +/** + * Component to render a table listing all the equipment in the register + * @param {*} isWishList + * @param {*} navigate + * @param {*} logout + * @returns + */ +const EquipmentList = ({ isWishList, navigate, logout }) => { + const { equipment, setEquipment } = useEquipment(logout); + const [error, setError] = useState(""); + + return ( + <> +
+
Equipment
+
+
+ {error != "" ? ( +
{error}
+ ) : ( + <> + )} +
+ + + + + + + + + + + + + + + + + + {equipment != null && ( + + {equipment.map((e) => ( + + ))} + + )} +
DescriptionModelSerial No.TypeManufacturerPurchasedPriceRetailer
+
+ +
+ + ); +}; + +export default EquipmentList; diff --git a/src/music-catalogue-ui/components/equipment/equipmentList.module.css b/src/music-catalogue-ui/components/equipment/equipmentList.module.css new file mode 100644 index 0000000..1ed5aff --- /dev/null +++ b/src/music-catalogue-ui/components/equipment/equipmentList.module.css @@ -0,0 +1,9 @@ +.equipmentListAddButton { + float: right; +} + +.equipmentListError { + font-weight: bold; + color: red; + text-align: center; +} diff --git a/src/music-catalogue-ui/components/equipment/equipmentRow.js b/src/music-catalogue-ui/components/equipment/equipmentRow.js new file mode 100644 index 0000000..372f836 --- /dev/null +++ b/src/music-catalogue-ui/components/equipment/equipmentRow.js @@ -0,0 +1,96 @@ +import pages from "@/helpers/navigation"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; +import { faPenToSquare } from "@fortawesome/free-solid-svg-icons"; +import DeleteEquipmentActionIcon from "./deleteEquipmentActionIcon"; +import DateFormatter from "../common/dateFormatter"; +import CurrencyFormatter from "../common/currencyFormatter"; + +/** + * Component to render a row containing the details for a single item of equipment + * @param {*} equipment + * @param {*} isWishList + * @param {*} navigate + * @param {*} logout + * @param {*} setManufacturers + * @param {*} setError + * @returns + */ +const EquipmentRow = ({ + equipment, + isWishList, + navigate, + logout, + setEquipment, + setError, +}) => { + // Get the equipment type name + const equipmentType = equipment["equipmentType"]; + const equipmentTypeName = equipmentType != null ? equipmentType["name"] : ""; + + // Get the manufacturer name + const manufacturer = equipment["manufacturer"]; + const manufacturerName = manufacturer != null ? manufacturer["name"] : ""; + + // Get the retailer name + const retailer = equipment["retailer"]; + const retailerName = retailer != null ? retailer["name"] : ""; + + return ( + + {equipment.description} + {equipment.model} + {equipment.serialNumber} + {equipmentTypeName} + {manufacturerName} + + + + + + + {retailerName} + + + + + + navigate({ + page: pages.equipmentEditor, + equipment: equipment, + }) + } + /> + + + {/* */} + + + {/* + navigate({ + page: pages.albumPurchaseDetails, + artist: artist, + album: album, + }) + } + /> */} + + + ); +}; + +export default EquipmentRow; diff --git a/src/music-catalogue-ui/components/manufacturers/deleteManufacturerActionIcon.js b/src/music-catalogue-ui/components/manufacturers/deleteManufacturerActionIcon.js index cb8524e..31424a8 100644 --- a/src/music-catalogue-ui/components/manufacturers/deleteManufacturerActionIcon.js +++ b/src/music-catalogue-ui/components/manufacturers/deleteManufacturerActionIcon.js @@ -20,7 +20,7 @@ const DeleteManufacturerActionIcon = ({ setManufacturers, setError, }) => { - /* Callback to prompt for confirmation and delete amanufacturer */ + /* Callback to prompt for confirmation and delete avmanufacturer */ const confirmDeleteManufacturer = useCallback( async (e) => { // Prevent the default action associated with the click event @@ -41,7 +41,7 @@ const DeleteManufacturerActionIcon = ({ setManufacturers(fetchedManufacturers); } } catch (ex) { - setError(`Error deleting the equipment type: ${ex.message}`); + setError(`Error deleting the manufacturer: ${ex.message}`); } } }, diff --git a/src/music-catalogue-ui/components/menuBar.js b/src/music-catalogue-ui/components/menuBar.js index 4c52ba6..720a807 100644 --- a/src/music-catalogue-ui/components/menuBar.js +++ b/src/music-catalogue-ui/components/menuBar.js @@ -61,7 +61,7 @@ const MenuBar = ({ navigate, logout }) => {