Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Resource to the resource table after creating a new one #213

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions src/deskstar-frontend/components/AddResourceModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
createDeskType,
createFloor,
createRoom,
getAllDesks,
getDesks,
getFloors,
getRooms,
} from "../lib/api/ResourceService";
Expand All @@ -17,13 +19,29 @@ import { IRoom } from "../types/room";
import { toast } from "react-toastify";
import FilterListbox from "./FilterListbox";
import Input from "./forms/Input";
import { classes } from "../lib/helpers";
import { IDesk } from "../types/desk";

const AddResourceModal = ({
buildings: origBuildings,
deskTypes: origDeskTypes,
setLocations: setParentLocations,
setBuildings: setParentBuildings,
desks,
setDesks: setParentDesks,
setDeskTypes: setParentDeskTypes,
setFloors: setParentFloors,
setRooms: setParentRooms,
}: {
buildings: IBuilding[];
deskTypes: IDeskType[];
setLocations: (locations: ILocation[]) => void;
setBuildings: (buildings: IBuilding[]) => void;
desks: IDesk[];
setDesks: (desks: IDesk[]) => void;
setDeskTypes: (deskTypes: IDeskType[]) => void;
setFloors: (floors: IFloor[]) => void;
setRooms: (rooms: IRoom[]) => void;
}) => {
let { data: session } = useSession();

Expand Down Expand Up @@ -93,7 +111,9 @@ const AddResourceModal = ({
if (!session) return [];

try {
const resFloors = await getFloors(session, selectedBuilding.buildingId);
const resFloors = (
await getFloors(session, selectedBuilding.buildingId)
).filter((floor) => !floor.isMarkedForDeletion);
setFloors(resFloors);
} catch (error) {
toast.error(`${error}`);
Expand All @@ -112,7 +132,10 @@ const AddResourceModal = ({
if (!session) return [];

try {
const resRooms = await getRooms(session, selectedFloor.floorId);
const resRooms = await (
await getRooms(session, selectedFloor.floorId)
).filter((room) => !room.isMarkedForDeletion);

setRooms(resRooms);
} catch (error) {
toast.error(`${error}`);
Expand Down Expand Up @@ -146,7 +169,8 @@ const AddResourceModal = ({
});
if (res.message.toLowerCase().includes("success")) {
origBuildings.push(res.data as IBuilding);
setBuildings([...buildings, res.data as IBuilding]);
setBuildings(origBuildings);
setParentBuildings(origBuildings);
const tmp = new Map<string, ILocation>();
[
...locations,
Expand All @@ -155,6 +179,7 @@ const AddResourceModal = ({
tmp.set(element.locationName, { locationName: element.locationName })
);
setLocations(Array.from(tmp.values()));
setParentLocations(Array.from(tmp.values()));
toast.success(res.message);
} else {
toast.error(res.message);
Expand Down Expand Up @@ -186,6 +211,7 @@ const AddResourceModal = ({

if (res.message.toLowerCase().includes("success")) {
setFloors([...floors, res.data as IFloor]);
setParentFloors([...floors, res.data as IFloor]);
toast.success(res.message);
} else {
toast.error(res.message);
Expand Down Expand Up @@ -219,6 +245,7 @@ const AddResourceModal = ({
});
if (res.message.toLowerCase().includes("success")) {
setRooms([...rooms, res.data as IRoom]);
setParentRooms([...rooms, res.data as IRoom]);
toast.success(res.message);
} else {
toast.error(res.message);
Expand All @@ -238,6 +265,7 @@ const AddResourceModal = ({

if (res.message.toLowerCase().includes("success")) {
setDeskTypes([...deskTypes, res.data as IDeskType]);
setParentDeskTypes([...deskTypes, res.data as IDeskType]);
toast.success(res.message);
} else {
toast.error(res.message);
Expand Down Expand Up @@ -279,6 +307,10 @@ const AddResourceModal = ({
roomId: room?.roomId,
});
if (res.message.toLowerCase().includes("success")) {
const desk = (await getDesks(session)).find(
(d) => d.deskId === (res.data as any).deskId
);
if (desk) setParentDesks([...desks, desk]);
toast.success(res.message);
} else {
toast.error(res.message);
Expand Down Expand Up @@ -565,7 +597,10 @@ const AddResourceModal = ({
placeholder="Desk Type Name"
/>
<a
className="btn text-black bg-primary hover:bg-secondary border-primary hover:border-secondary float-right"
className={classes(
isLoading ? "disabled" : "",
"btn text-black bg-primary hover:bg-secondary border-primary hover:border-secondary float-right"
)}
onClick={() => addDeskType()}
>
Confirm
Expand Down
5 changes: 0 additions & 5 deletions src/deskstar-frontend/lib/api/ResourceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ export async function updateBuilding(
message: body || "An error occured.",
};
} else {
console.log(body);
result = {
response: ResourceResponse.Success,
data: JSON.parse(body) as IBuilding,
Expand Down Expand Up @@ -343,7 +342,6 @@ export async function updateFloor(
message: body || "An error occured.",
};
} else {
console.log(body);
result = {
response: ResourceResponse.Success,
data: JSON.parse(body) as IFloor,
Expand Down Expand Up @@ -379,7 +377,6 @@ export async function updateRoom(
message: body || "An error occured.",
};
} else {
console.log(body);
const parsed = JSON.parse(body);
const room = parsed as IRoom;
room.building = parsed["buildingName"];
Expand Down Expand Up @@ -419,7 +416,6 @@ export async function updateDesk(
message: body || "An error occured.",
};
} else {
console.log(body);
const parsed = JSON.parse(body);
const desk = parsed as IDesk;
desk.deskTyp = parsed["deskTypeName"];
Expand Down Expand Up @@ -458,7 +454,6 @@ export async function updateDeskType(
message: body || "An error occured.",
};
} else {
console.log(body);
result = {
response: ResourceResponse.Success,
data: JSON.parse(body) as IDeskType,
Expand Down
35 changes: 27 additions & 8 deletions src/deskstar-frontend/pages/resources/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ const ResourceOverview = ({
}) => {
let { data: session } = useSession();

const locations: ILocation[] = origBuildings.map((building) => ({
locationName: building.location,
}));
const [locations, setLocations] = useState<ILocation[]>(
origBuildings.map((building) => ({
locationName: building.location,
}))
);

const router = useRouter();

Expand Down Expand Up @@ -484,6 +486,13 @@ const ResourceOverview = ({
<AddResourceModal
buildings={origBuildings}
deskTypes={origDeskTypes}
desks={desks}
setDesks={setDesks}
setFloors={setFloors}
setRooms={setRooms}
setBuildings={setBuildings}
setDeskTypes={setDeskTypes}
setLocations={setLocations}
/>
</div>
</div>
Expand Down Expand Up @@ -737,11 +746,21 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
};

try {
const buildings = await getBuildings(session);
const floors = await getFloors(session);
const rooms = await getRooms(session);
const desks = await getDesks(session);
const deskTypes = await getDeskTypes(session);
const buildings = await (
await getBuildings(session)
).filter((b) => !b.isMarkedForDeletion);
const floors = await (
await getFloors(session)
).filter((b) => !b.isMarkedForDeletion);
const rooms = await (
await getRooms(session)
).filter((b) => !b.isMarkedForDeletion);
const desks = await (
await getDesks(session)
).filter((d) => !d.isMarkedForDeletion);
const deskTypes = await (
await getDeskTypes(session)
).filter((d) => !d.isMarkedForDeletion);
return {
props: {
buildings,
Expand Down