Skip to content

Commit

Permalink
Add associatedNode and toast to add org page
Browse files Browse the repository at this point in the history
  • Loading branch information
robertgodfrey committed Nov 10, 2023
1 parent 765593c commit f65806c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
39 changes: 13 additions & 26 deletions my-app/src/components/manage-org/modals/AddOrgModal.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import PropTypes from "prop-types";
import { useState, useEffect } from "react";
import { useForm } from "react-hook-form";
import { ISLANDS } from "@/constants/constants";
import { toast } from "react-toastify";

const AddOrgModal = ({ id }) => {
const { register, handleSubmit, reset } = useForm();
Expand Down Expand Up @@ -28,37 +30,25 @@ const AddOrgModal = ({ id }) => {
location: org.location,
}),
});

setStatus(null);
document.getElementById(id).close();
if (res.status === 200) {
setStatus({
msg: "success",
body: "Successfully added organization ✅",
});
toast.success('Successfully added organization');
reset();
console.log("Successfully added organization");
} else {
throw new Error("Failed to add organization.");
toast.error('Failed to add organization');
}
} catch (err) {
setStatus({
msg: "error",
body: " Failed to add organization ❌",
});
} finally {
setTimeout(() => {
setStatus(null);
console.log("status reset");
}, 3000);
toast.error('Failed to add organization');
}
}

function onSubmit(data) {
console.log(data);
addOrganization(data);
}

return (
<dialog id={id} className="modal modal-bottom sm:modal-middle">
<dialog id={id} className="modal modal-bottom sm:modal-middle z-50">
<div className="modal-box">
<h3 className="pb-5 font-bold">ADD ORGANIZATION</h3>
<form onSubmit={handleSubmit(onSubmit)}>
Expand All @@ -81,11 +71,10 @@ const AddOrgModal = ({ id }) => {
{...register("location", { required: true })}
className="select select-bordered"
>
<option disabled>Choose your location</option>
<option>Oahu</option>
<option>Maui</option>
<option>Big Island</option>
<option>Kauai</option>
<option disabled>Choose location</option>
{ISLANDS.map((island, index) => {
if (island !== 'At-sea Offshore') return <option key={index}>{island}</option>;
})}
</select>
</div>
<div className="modal-action">
Expand All @@ -101,8 +90,6 @@ const AddOrgModal = ({ id }) => {
</button>
</div>
</form>
{status && status.msg === "success" && <div>{status.body}</div>}
{status && status.msg === "error" && <div>{status.body}</div>}
{status && status.msg === "loading" && (
<div className="flex items-center gap-2">
<span className="loading loading-spinner" />
Expand All @@ -111,7 +98,7 @@ const AddOrgModal = ({ id }) => {
)}
</div>
<form method="dialog" className="modal-backdrop">
<button>close</button>
<button className="cursor-default">close</button>
</form>
</dialog>
);
Expand Down
7 changes: 4 additions & 3 deletions my-app/src/components/manage-org/modals/DeleteOrgModal.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PropTypes from "prop-types";
import { toast } from "react-toastify";

const DeleteOrgModal = ({ id, org }) => {
async function deleteOrganization(orgData) {
Expand All @@ -17,12 +18,12 @@ const DeleteOrgModal = ({ id, org }) => {
);

if (res.ok && res2.ok) {
console.log("Successfully deleted organization");
toast.success("Successfully deleted organization");
} else {
throw new Error("Failed to delete organization.");
toast.error("Failed to delete organization")
}
} catch (err) {
console.log(err);
toast.error("Failed to delete organization")
}
}
console.log(org);
Expand Down
2 changes: 1 addition & 1 deletion my-app/src/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const App = ({ Component, pageProps: { session, ...pageProps } }) => {
</div>
<MobileNavbar />
<ToastContainer />
<DialogflowChatWidget/>
<DialogflowChatWidget />
</div>
</SessionProvider>
</>
Expand Down
23 changes: 20 additions & 3 deletions my-app/src/pages/api/mongo/organization/add-organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,32 @@ export default async function handler(req, res) {
try {
if (req.method === "POST") {
const { name, location } = await req.body;
const associatedNode = () => {
switch (location) {
case "Big Island":
return "Big Island Node";
case "Maui":
case "Molokai":
case "Lanai":
return "Maui Node";
case "Kauai":
return "Kauai Node";
default:
return "CMDR Hub";
}
}
await connectDB();
await Organization.create({
name,
location
location,
associatedNode: associatedNode(),
});
res.status(200).json({msg: "Organization added successfully"});
} else {
res.status(405).json({ error: "Method not allowed" });
}
res.status(200).json({ msg: "Item added successfully!" });
} catch (error) {
console.log("error", error);
res.status(500).json({ error: "Unable to add item to database." });
res.status(500).json({ error: "Unable to add organization" });
}
}

0 comments on commit f65806c

Please sign in to comment.