diff --git a/webstillas/src/components/logistics/project/map.js b/webstillas/src/components/logistics/project/map.js index 9599fae1091f505974d86310c2ef65a986e25cc2..65f9895e75f30f2368f80def5641d57d31501079 100644 --- a/webstillas/src/components/logistics/project/map.js +++ b/webstillas/src/components/logistics/project/map.js @@ -83,8 +83,7 @@ export function MapClass(props) { */ const AddProjectRequest = async () => { try { - console.log(JSON.stringify(project)) - await postModel(PROJECTS_URL, JSON.stringify(project)) + await postModel(PROJECTS_URL, (project)) await queryClient.refetchQueries("allProjects") } catch (e) { AlertCatch() diff --git a/webstillas/src/components/logistics/scaffold/addScaffolding.js b/webstillas/src/components/logistics/scaffold/addScaffolding.js index f8de9e8a26c8b7e49215231b9ee79a8fe91da0eb..a7a6fadf5c4b33c1cefaafaeeb2d4372e82b45ff 100644 --- a/webstillas/src/components/logistics/scaffold/addScaffolding.js +++ b/webstillas/src/components/logistics/scaffold/addScaffolding.js @@ -24,7 +24,7 @@ function AddScaffolding() { }) //Verification of - const [postSuccsess, setPostSuccsess] = useState(null) + const [postSuccess, setPostSuccess] = useState(null) const [buttonPress, setButtonPress] = useState(false) @@ -82,8 +82,10 @@ function AddScaffolding() { ] try { //posting body - const promise = await postModel(SCAFFOLDING_URL, (body)) - setPostSuccsess(promise.statusCode) + const promise = await postModel(SCAFFOLDING_URL, (body)).catch( + e => console.log(e) + ) + setPostSuccess(promise.statusCode) } catch (e) { console.log(e) } @@ -92,12 +94,12 @@ function AddScaffolding() { return ( <div className={"main-add-scaffolding"}> - {(postSuccsess === 201) ? + {(postSuccess === 201) ? (<Alert className={"alert-success"} key={"success"} variant={"success"}> Stillasdel har blitt registrert </Alert>) : null} - {(postSuccsess !== 201 && buttonPress) ? + {(postSuccess !== 201 && buttonPress) ? (<Alert className={"alert-success"} key={"danger"} variant={"danger"}> Stillasdel har ikke blitt registrert diff --git a/webstillas/src/components/projects/elements/Modal.js b/webstillas/src/components/projects/elements/Modal.js index 5fbedcc38ec0886bf8fda049e70988571ecef2c7..00f4b93bf03a773040e9a32242cb0efedfedd517 100644 --- a/webstillas/src/components/projects/elements/Modal.js +++ b/webstillas/src/components/projects/elements/Modal.js @@ -10,6 +10,8 @@ import "./Modal.css" //https://ordinarycoders.com/blog/article/react-bootstrap-modal + +//JSON body that is used to send request. const scaffoldingMove = [ { @@ -55,28 +57,38 @@ const scaffoldingMove = ] - - +/** + * Function that will endable the user to transfer scaffolding from one location to another. + * + * @param props information of a given project + * @returns {JSX.Element} + */ export default function InfoModalFunc(props) { const [show, setShow] = useState(false); const handleClose = () => setShow(false); const handleShow = () => setShow(true); - //https://codesandbox.io/s/react-week-date-view-forked-ruxjr9?file=/src/App.js:857-868 const queryClient = useQueryClient() let jsonProjects jsonProjects = queryClient.getQueryData("allProjects") let jsonProject = queryClient.getQueryData(["project", props.id]) - console.log(jsonProject) - const [roomRent, setRoomRent] = useState(scaffoldingMove); + + const [scaffolding, setScaffolding] = useState(scaffoldingMove); const [ToProject, setToProject] = useState(""); const [FromProject, setFromProject] = useState(""); - //Todo change variable - const handleroom = (e, id) => { - let result = [...roomRent]; + /** + * Function to set quantity of scaffolding types + * + * Code taken from https://codesandbox.io/s/react-week-date-view-forked-ruxjr9?file=/src/App.js:857-868 + * + * @param e quantity the user has passed + * @param id of the selected project. + */ + const setQuantity = (e, id) => { + let result = [...scaffolding]; result = result.map((x) => { if (x.type.toLowerCase() === id.toLowerCase()) { const inputvalue = (e.target.value) @@ -84,26 +96,40 @@ export default function InfoModalFunc(props) { return x; } else return x; }); - setRoomRent(result) + setScaffolding(result) }; - //todo add a note to the user if the transaction was a success or a fail. + + /** + * Function that will execute request to transfer scaffolding parts. + * + * @returns {Promise<void>} + */ const AddScaffold = async () => { console.log(JSON.stringify(move)) - await putModel(TRANSFER_SCAFFOLDING, JSON.stringify(move)); - await queryClient.resetQueries(["project", props.id]).then(() => handleClose()) + try { + await putModel(TRANSFER_SCAFFOLDING, JSON.stringify(move)) + await queryClient.resetQueries(["project", props.id]) + } catch (e) { + if (e.text === "invalid body"){ + window.alert("500 Internal Server Error\nNoe gikk galt! Prøv igjen senere") + }else { + window.alert("Advarsel: Kan ikke overføre antall stillasdeler") + } + } } + //JSON body that is sent with request const move = { "toProjectID": Number(ToProject), "fromProjectID": Number(FromProject), - "scaffold": roomRent + "scaffold": scaffolding } - const validFormat = ToProject !== FromProject - + //Checks if the user did not set to project equal to from project. + const validFormat = ToProject !== FromProject return ( <> <Button className="nextButton" onClick={handleShow}> @@ -129,7 +155,7 @@ export default function InfoModalFunc(props) { onChange={(e) => setToProject(e.target.value)}> <option selected defaultValue="">Choose here</option> <option value={0}>Storage</option> - {jsonProjects.map(e => { + {jsonProjects?.map(e => { return ( <option value={e.projectID}>{e.projectName}</option> ) @@ -166,7 +192,7 @@ export default function InfoModalFunc(props) { type="number" min={0} key={"input" + e.type} - onChange={(j) => handleroom(j, e.type)}/> + onChange={(j) => setQuantity(j, e.type)}/> </div> ) } diff --git a/webstillas/src/components/projects/elements/card.css b/webstillas/src/components/projects/elements/card.css index a6542bf37768d90a295a247cfe689d2a9ad78bc1..4e56ad67d98d31f7d8c0df241bcd9dfccea710ce 100644 --- a/webstillas/src/components/projects/elements/card.css +++ b/webstillas/src/components/projects/elements/card.css @@ -14,11 +14,6 @@ overflow: hidden; } -.btn-delete{ - height: fit-content; - width: fit-content; - background-size: cover; -} .header { display: flex; @@ -66,9 +61,6 @@ border-left-color: gray; } -.card-btn{ - padding: 0 24px 24px; -} .btn{ background-color: #F28A04; border-radius: 10px; @@ -82,9 +74,10 @@ .card-btns{ display: flex; - padding-top: 24px; align-items: center; justify-content: center; + padding: 0 24px 24px; + } .left-contact-text{ diff --git a/webstillas/src/components/projects/elements/card.js b/webstillas/src/components/projects/elements/card.js index 36dfa1e68d157f82062742afea91456cdfc21c48..869c8ba9b496b58e895023454438db046873d064 100644 --- a/webstillas/src/components/projects/elements/card.js +++ b/webstillas/src/components/projects/elements/card.js @@ -1,4 +1,4 @@ -import React from 'react' +import React, {useState} from 'react' import 'bootstrap/dist/css/bootstrap.min.css'; import './card.css' import img from '../images/blog-item.jpg' @@ -9,95 +9,102 @@ import {useQueryClient} from "react-query"; import {IconButton} from "@material-ui/core"; import DeleteIcon from "@material-ui/icons/Delete"; + +/** + * Function that will display an information card, with information of a project. + * + * @param props data sent from another view. + * @returns {JSX.Element} + */ function CardElement(props) { const queryClient = useQueryClient() + + /** + * Function that will delete an existing project. + * + * @returns {Promise<void>} + */ const DeleteProject = async () => { - if (window.confirm("Are you sure you want to delete " + props.name + "?" )){ + if (window.confirm("Are you sure you want to delete " + props.name + "?")) { const deleteBody = [ { id: props.id } ] - await deleteModel(PROJECTS_URL, (deleteBody)).catch(e => console.log(e)).then(e => console.log("success")) - await queryClient.invalidateQueries("allProjects") + try { + const result = await deleteModel(PROJECTS_URL, (deleteBody)) + await queryClient.invalidateQueries("allProjects") + + } catch (e) { + console.log(e) + window.alert("Something wrong happened! Try again later") + } } } - return ( - <article className={"card"}> - <div className={"name-btn"}> - <section className={"header"}> - <h3>{props.name}</h3> - </section> - <div className={"btn-delete"}> - <IconButton onClick={DeleteProject}> - <DeleteIcon style={{ fontSize: 50}} /> - </IconButton> - </div> - - </div> - <section className={"image-project"}> - <img src={img} alt={""}/> - </section> - - <section className={"information-highlights-cta"}> - <div className={"information-highlights"}> - <ul className={"information-list"}> - <li className={"horizontal-list"}> - <div className={"highlightText"}> - <span>{props.state}</span> - </div> - <div className={"highlightText-caption"}> - <span>Status</span> - </div> - </li> - <li className={"horizontal-list"}> - <div className={"highlightText"}> - <span>{props.rentPeriod}</span> - </div> - <div className={"highlightText-caption"}> - <span>Leieperiode</span> - </div> - </li> - <li className={"horizontal-list"}> - <div className={"highlightText"}> - <span> {props.size}</span> - <span>㎡</span> - </div> - <div className={"highlightText-caption"}> - <span>Størrelse</span> - </div> - </li> - </ul> - </div> - </section> - <section className={"contact-highlights-cta"}> - <div className={"information-highlights"}> - <ul className={"contact-list"}> - <li className={"horizontal-list-contact"}> - <span className={"left-contact-text"}>Kontakt person</span> - <span className={"right-contact-text"}>{props.contactPerson}</span> - </li> - <li className={"horizontal-list-contact"}> - <span className={"left-contact-text"}>Adresse</span> - <span - className={"right-contact-text"}>{props.address_Street}, {props.address_zip} {props.address_Municipality}</span> - </li> - <li className={"horizontal-list-contact"}> - <span className={"left-contact-text"}>Nummer</span> - <span className={"right-contact-text"}>{props.contactNumber}</span> - </li> - </ul> - </div> - </section> - <section className={"card-btn"}> - <div className={"card-btns"}> - <Link className={"btn"} to={"/project/" + props.id}>Mer Informasjon</Link> - </div> + return ( + <div className={"card"}> + <div className={"name-btn"}> + <section className={"header"}> + <h3>{props.name}</h3> </section> - </article> + <IconButton onClick={DeleteProject}> + <DeleteIcon style={{fontSize: 50}}/> + </IconButton> + </div> + <img className={"image-project"} src={img} alt={""}/> + <div className={"information-highlights"}> + <ul className={"information-list"}> + <li className={"horizontal-list"}> + <div className={"highlightText"}> + <span>{props.state}</span> + </div> + <div className={"highlightText-caption"}> + <span>Status</span> + </div> + </li> + <li className={"horizontal-list"}> + <div className={"highlightText"}> + <span>{props.rentPeriod}</span> + </div> + <div className={"highlightText-caption"}> + <span>Leieperiode</span> + </div> + </li> + <li className={"horizontal-list"}> + <div className={"highlightText"}> + <span> {props.size}</span> + <span>㎡</span> + </div> + <div className={"highlightText-caption"}> + <span>Størrelse</span> + </div> + </li> + </ul> + </div> + <div className={"information-highlights"}> + <ul className={"contact-list"}> + <li className={"horizontal-list-contact"}> + <span className={"left-contact-text"}>Kontakt person</span> + <span className={"right-contact-text"}>{props.contactPerson}</span> + </li> + <li className={"horizontal-list-contact"}> + <span className={"left-contact-text"}>Adresse</span> + <span + className={"right-contact-text"}>{props.address_Street}, {props.address_zip} {props.address_Municipality}</span> + </li> + <li className={"horizontal-list-contact"}> + <span className={"left-contact-text"}>Nummer</span> + <span className={"right-contact-text"}>{props.contactNumber}</span> + </li> + </ul> + </div> + <div className={"card-btns"}> + <Link className={"btn"} to={"/project/" + props.id}>Mer Informasjon</Link> + </div> + </div> ) } diff --git a/webstillas/src/modelData/deleteProject.js b/webstillas/src/modelData/deleteProject.js index bfb7f834361201f0ea9b1742d3b16a85b96f4eeb..c4cb473388dca3c768fe955d0629518236322892 100644 --- a/webstillas/src/modelData/deleteProject.js +++ b/webstillas/src/modelData/deleteProject.js @@ -18,7 +18,11 @@ export default function deleteModel(url, body) { text: xhr.responseText }))); } else { - resolve((xhr.responseText)); + resolve({ + statusCode: xhr.status, + text: xhr.responseText + } + ); } }); xhr.open('DELETE', BASE_URL + url); diff --git a/webstillas/src/modelData/putData.js b/webstillas/src/modelData/putData.js index 6825019a02366a2571555ca694f5afb704f8e703..2c8fea475d899ece5a1951a6f991d2d5cf28f43a 100644 --- a/webstillas/src/modelData/putData.js +++ b/webstillas/src/modelData/putData.js @@ -14,13 +14,17 @@ export default function putModel(url, body) { return; } if (xhr.status !== 200) { - reject(new Error(JSON.stringify({ - status: xhr.status, + reject((({ + statusCode: xhr.status, statusText: xhr.statusText, text: xhr.responseText }))); } else { - resolve((xhr.responseText)); + resolve({ + statusCode: xhr.status, + text: xhr.responseText + } + ); } }); xhr.send(body);