From 0df990483623eb2c04bdea35ebcf37ecc41bcf43 Mon Sep 17 00:00:00 2001 From: WT000 <55751558+WT000@users.noreply.github.com> Date: Fri, 14 Apr 2023 20:12:09 +0100 Subject: [PATCH 1/6] Fix formatting on edit home --- src/components/forms/HomeForm/HomeForm.tsx | 21 +++++++++++++++++-- .../InstructionsLayout/InstructionsLayout.tsx | 2 +- src/pages/homes/[id]/index.tsx | 4 ++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/components/forms/HomeForm/HomeForm.tsx b/src/components/forms/HomeForm/HomeForm.tsx index d94695f..9bf735a 100644 --- a/src/components/forms/HomeForm/HomeForm.tsx +++ b/src/components/forms/HomeForm/HomeForm.tsx @@ -7,7 +7,6 @@ import Tile, { TileType } from "../../Tile/Tile"; import { useEffect, useState } from "react"; import Role from "../../../lib/utils/roles"; import PhotoInputLayout from "../../layouts/PhotoInputLayout/PhotoInputLayout"; -import ImageLayout from "../../layouts/ImageLayout/ImageLayout"; export interface HomeFormData { name: string; @@ -169,7 +168,7 @@ export default function HomeForm(props: HomeFormProps) { {/* Instructions */} {/* May need a custom size set on md: breakpoint */} - + + + {/* To be delegates list... */} + + } + text={"Home Name"} + type={"text"} + name={"name"} + placeholder={"My New Home..."} + register={register} + registerSettings={{ + required: true, + minLength: 4, + }} + errors={errors.name} + errorMessage={"*Must be at least 4 characters."} + /> +
+ + )}
diff --git a/src/pages/homes/[id]/index.tsx b/src/pages/homes/[id]/index.tsx index 9bc9f68..3861d0a 100644 --- a/src/pages/homes/[id]/index.tsx +++ b/src/pages/homes/[id]/index.tsx @@ -45,8 +45,17 @@ export default function Index(props) { const readings = props.readings ? JSON.parse(props.readings) : null; const bookings = props.bookings ? JSON.parse(props.bookings) : null; const delegates = props.delegates ? JSON.parse(props.delegates) : null; + const delegateReadingCount = props.delegateCounts ? Object.assign({}, ...(JSON.parse(props.delegateCounts).map(item => ({ [item._id]: item }) ))) : null; const home = props.home; + delegates?.forEach(delegate => { + if (delegateReadingCount.hasOwnProperty(delegate._id)) { + delegate["readingCount"] = delegateReadingCount[delegate._id].count; + } else { + delegate["readingCount"] = 0; + } + }); + // const startDate = getDayMonth(new Date(props?.booking?.startDateTime)); // const endDate = getDayMonth(new Date(props?.booking?.endDateTime), true); const ascendingDates = [...readings]; @@ -75,8 +84,8 @@ export default function Index(props) { let delegateItems = [] delegates.map((item, index) => { - let interactive = { onClick: () => {console.log("Clicked!");}, text: "Undo" } - delegateItems.push(, interactive: interactive })}>) + let interactive = { onClick: () => {console.log("Clicked!");}} + delegateItems.push(, interactive: interactive })}>) }) const stats = [ @@ -243,6 +252,20 @@ export async function getServerSideProps({ req, res, params }) { const bookings = await Booking.find({home: h._id, isDeleted: false}).sort({"createdAt": -1}).lean() const delegates = h.delegates; const userRole = getRole(session, hNoDelegates); + + const delegateReadingCount = await Reading.aggregate([ + { + $match: { + home: h._id + } + }, + { + $group: { + _id: "$user", + count: { $sum: 1 } + } + }, + ]) if (userRole === Role.Guest) { return { @@ -264,6 +287,7 @@ export async function getServerSideProps({ req, res, params }) { readings: JSON.stringify(readings), bookings: JSON.stringify(bookings), delegates: JSON.stringify(delegates), + delegateCounts: JSON.stringify(delegateReadingCount), userRole: userRole, averagePerDay: averagePerDay ?? 0.00, }, From b996434cd7ebcb45ad65565d72c41de54f95691e Mon Sep 17 00:00:00 2001 From: WT000 <55751558+WT000@users.noreply.github.com> Date: Sat, 15 Apr 2023 11:42:30 +0100 Subject: [PATCH 5/6] Note on delegate information --- src/pages/homes/[id]/index.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/pages/homes/[id]/index.tsx b/src/pages/homes/[id]/index.tsx index 3861d0a..dbeb698 100644 --- a/src/pages/homes/[id]/index.tsx +++ b/src/pages/homes/[id]/index.tsx @@ -48,6 +48,8 @@ export default function Index(props) { const delegateReadingCount = props.delegateCounts ? Object.assign({}, ...(JSON.parse(props.delegateCounts).map(item => ({ [item._id]: item }) ))) : null; const home = props.home; + // Would be better to build a big aggregate query in the future that'll have this information with the delegate + // However, as we're limited on time, this works as a proof of concept delegates?.forEach(delegate => { if (delegateReadingCount.hasOwnProperty(delegate._id)) { delegate["readingCount"] = delegateReadingCount[delegate._id].count; From fe36a8dd551307a1032365285b5a9578ab544989 Mon Sep 17 00:00:00 2001 From: WT000 <55751558+WT000@users.noreply.github.com> Date: Mon, 17 Apr 2023 10:13:48 +0100 Subject: [PATCH 6/6] Fix merge fix formatting --- src/pages/homes/[id]/index.tsx | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/pages/homes/[id]/index.tsx b/src/pages/homes/[id]/index.tsx index 39c9ed3..64d40c5 100644 --- a/src/pages/homes/[id]/index.tsx +++ b/src/pages/homes/[id]/index.tsx @@ -33,7 +33,6 @@ import Notification from "../../../components/Notification/Notifications"; import Popup from "../../../components/Popup/Popup"; import QRCode from "../../../components/QRCode/QRCode"; import ReadingPopup from "../../../components/layouts/ReadingPopupLayout/ReadingPopupLayout"; -import { useRouter } from "next/router"; function displayCost(cost) { let costString = "0" @@ -65,7 +64,6 @@ export default function Index(props) { // const startDate = getDayMonth(new Date(props?.booking?.startDateTime)); // const endDate = getDayMonth(new Date(props?.booking?.endDateTime), true); - const router = useRouter(); const [currentPath, setCurrentPath] = useState(""); useEffect(() => {if (window) {setCurrentPath(window.location.protocol + "//" + window.location.hostname)}}); const [popupVisible, setPopupVisible] = useState(false); @@ -170,8 +168,7 @@ export default function Index(props) { icon={} textLine1="Edit Home" textLine2="Details">} - clickable={true} onClick={() => router.push(`/homes/${home._id}/edit`)}> - + clickable={true} onClick={() => router.push(`/homes/${home._id}/edit`)}> } @@ -182,8 +179,7 @@ export default function Index(props) { setPopupData({ text: `${currentPath}/auth/guest?name=${home.name}` }) - }}> - + }}>