From 084a8f2eb800109cdfc3a829d058f1595b64d70e Mon Sep 17 00:00:00 2001 From: Ayobami Date: Tue, 1 Jul 2025 18:36:28 +0100 Subject: [PATCH] ISSUE 9: preload space details into input UI --- .../Spaces/Edit/EditPropertySpacePage.jsx | 387 +++++++++++++----- 1 file changed, 275 insertions(+), 112 deletions(-) diff --git a/src/pages/Host/Spaces/Edit/EditPropertySpacePage.jsx b/src/pages/Host/Spaces/Edit/EditPropertySpacePage.jsx index d2f7ca1..dd540c7 100644 --- a/src/pages/Host/Spaces/Edit/EditPropertySpacePage.jsx +++ b/src/pages/Host/Spaces/Edit/EditPropertySpacePage.jsx @@ -9,7 +9,12 @@ import MkdSDK from "@/utils/MkdSDK"; import { useContext } from "react"; import { GlobalContext } from "@/globalContext"; import { useSearchParams } from "react-router-dom"; -import { SPACE_CATEGORY_SIZES, NOTIFICATION_STATUS, NOTIFICATION_TYPE, SPACE_STATUS } from "@/utils/constants"; +import { + SPACE_CATEGORY_SIZES, + NOTIFICATION_STATUS, + NOTIFICATION_TYPE, + SPACE_STATUS, +} from "@/utils/constants"; import CustomLocationAutoCompleteV2 from "@/components/CustomLocationAutoCompleteV2"; import CustomSelectV2 from "@/components/CustomSelectV2"; import CounterV2 from "@/components/CounterV2"; @@ -24,7 +29,8 @@ const ctrl = new AbortController(); const EditPropertySpacePage = () => { const { dispatch: authDispatch } = useContext(AuthContext); const { spaceData, dispatch } = useSpaceContext(); - const { dispatch: globalDispatch, state: globalState } = useContext(GlobalContext); + const { dispatch: globalDispatch, state: globalState } = + useContext(GlobalContext); const [searchParams] = useSearchParams(); const mode = searchParams.get("mode"); const { id } = useParams(); @@ -41,7 +47,11 @@ const EditPropertySpacePage = () => { rate: yup.number().typeError("Must be a number").positive().integer(), description: yup.string().required("This field is required"), rule: yup.string(), - max_capacity: yup.number().required("This field is required").min(1).typeError("This field is required"), + max_capacity: yup + .number() + .required("This field is required") + .min(1) + .typeError("This field is required"), additional_guest_rate: yup.string(), }); @@ -79,7 +89,12 @@ const EditPropertySpacePage = () => { const where = [`ergo_property_spaces.id = ${id}`]; const user_id = localStorage.getItem("user"); try { - const result = await sdk.callRawAPI("/v2/api/custom/ergo/popular/PAGINATE", { page: 1, limit: 1, user_id: Number(user_id), where, all: true }, "POST", ctrl.signal); + const result = await sdk.callRawAPI( + "/v2/api/custom/ergo/popular/PAGINATE", + { page: 1, limit: 1, user_id: Number(user_id), where, all: true }, + "POST", + ctrl.signal + ); if (Array.isArray(result.list) && result.list.length > 0) { setCurrSpace(result.list[0]); } @@ -102,10 +117,29 @@ const EditPropertySpacePage = () => { } }, []); + useEffect(() => { + if (mode === "edit" && currSpace && Object.keys(currSpace).length > 0) { + setValue("category", currSpace.space_id ?? ""); + setValue("id", currSpace.id ?? ""); + setValue("name", currSpace.name ?? ""); + setValue("rate", currSpace.rate ?? ""); + setValue("max_capacity", currSpace.max_capacity ?? 0); + setValue("description", currSpace.description ?? ""); + setValue("rule", currSpace.rule ?? ""); + setValue("zip", currSpace.zip ?? ""); + setValue("country", currSpace.country ?? ""); + setValue("city", currSpace.city ?? ""); + setValue("address_line_1", currSpace.address_line_1 ?? ""); + setValue("address_line_2", currSpace.address_line_2 ?? ""); + setValue("additional_guest_rate", currSpace.additional_guest_rate ?? ""); + setValue("size", currSpace.size ?? 0); + } + }, [currSpace, mode, setValue]); + const onSubmit = async (data) => { - const result = extractLocationInfo(data?.city) - data.city = (result[0]); - data.country = (result[1]); + const result = extractLocationInfo(data?.city); + data.city = result[0]; + data.country = result[1]; console.log("submitting", data); const host_id = localStorage.getItem("user"); globalDispatch({ type: "START_LOADING" }); @@ -127,7 +161,7 @@ const EditPropertySpacePage = () => { name: data.name, rule: data.rule, }, - "PUT", + "PUT" ); sdk.setTable("property_spaces"); @@ -143,7 +177,7 @@ const EditPropertySpacePage = () => { additional_guest_rate: data.additional_guest_rate, size: hasSizes ? data.size : SPACE_CATEGORY_SIZES.UNSET, }, - "PUT", + "PUT" ); // create notification @@ -158,7 +192,7 @@ const EditPropertySpacePage = () => { type: NOTIFICATION_TYPE.EDIT_PROPERTY_SPACE, status: NOTIFICATION_STATUS.NOT_ADDRESSED, }, - "POST", + "POST" ); } if (draftType === "continue") { @@ -168,7 +202,10 @@ const EditPropertySpacePage = () => { } } catch (err) { tokenExpireError(authDispatch, err.message); - globalDispatch({ type: "SHOW_ERROR", payload: { heading: "Edit Space Failed", message: err.message } }); + globalDispatch({ + type: "SHOW_ERROR", + payload: { heading: "Edit Space Failed", message: err.message }, + }); } globalDispatch({ type: "STOP_LOADING" }); }; @@ -181,166 +218,268 @@ const EditPropertySpacePage = () => { { label: "X-Large", value: SPACE_CATEGORY_SIZES.X_LARGE }, ]; const category = watch("category"); - const hasSizes = globalState.spaceCategories.find((ctg) => ctg.id == Number(category))?.has_sizes == 1; + const hasSizes = + globalState.spaceCategories.find((ctg) => ctg.id == Number(category)) + ?.has_sizes == 1; return ( -
+
-

Space Details

-
+

Space Details

+
-
+
setValue("address_line_1", val)} - name="address_line_1" - className={`w-full rounded border py-2 px-3 leading-tight text-gray-700 ${errors.address_line_1?.message ? "border-red-500 focus:outline-red-500" : "focus-within:outline-primary"}`} - placeholder="" + name='address_line_1' + className={`w-full rounded border px-3 py-2 leading-tight text-gray-700 ${ + errors.address_line_1?.message + ? "border-red-500 focus:outline-red-500" + : "focus-within:outline-primary" + }`} + placeholder='' hideIcons suggestionType={["(cities)"]} />
-
+
setValue("address_line_2", val)} - name="address_line_2" - className={`w-full rounded border py-2 px-3 leading-tight text-gray-700 ${errors.address_line_2?.message ? "border-red-500 focus:outline-red-500" : "focus-within:outline-primary"}`} - placeholder="" + name='address_line_2' + className={`w-full rounded border px-3 py-2 leading-tight text-gray-700 ${ + errors.address_line_2?.message + ? "border-red-500 focus:outline-red-500" + : "focus-within:outline-primary" + }`} + placeholder='' hideIcons suggestionType={["(cities)"]} />
-
+
setValue("city", val)} - name="city" - className={`w-full rounded border py-2 px-3 leading-tight text-gray-700 ${errors.city?.message ? "border-red-500 focus:outline-red-500" : "focus-within:outline-primary"}`} - placeholder="" + name='city' + className={`w-full rounded border px-3 py-2 leading-tight text-gray-700 ${ + errors.city?.message + ? "border-red-500 focus:outline-red-500" + : "focus-within:outline-primary" + }`} + placeholder='' hideIcons suggestionType={["(cities)"]} />
-
+
- -
+ +
-
+
-
- $ +
+ + $ +
-
+
-
+
-
-

* Max number of guests {errors.max_capacity?.message ? {errors.max_capacity?.message} : ""}

+
+

+ * Max number of guests{" "} + {errors.max_capacity?.message ? ( + + {errors.max_capacity?.message} + + ) : ( + "" + )} +

setValue("max_capacity", val)} /> @@ -348,58 +487,82 @@ const EditPropertySpacePage = () => { {hasSizes && (
)} -
+
-
- $ +
+ + $ +
-
+