From 085172b88b8a38163bec658c8707fcbb6bffbbac Mon Sep 17 00:00:00 2001 From: BFlat Date: Sat, 30 Dec 2023 00:07:28 +0700 Subject: [PATCH] weight response fix visibility --- .../hooks/useWeightFetch/useWeightFetch.tsx | 90 +++++++++++-------- .../src/v2/view/pages/weight/WeightPage.tsx | 19 ++-- 2 files changed, 69 insertions(+), 40 deletions(-) diff --git a/packages/fakeyou.com/src/hooks/useWeightFetch/useWeightFetch.tsx b/packages/fakeyou.com/src/hooks/useWeightFetch/useWeightFetch.tsx index 0f7543c1f6..35c882a478 100644 --- a/packages/fakeyou.com/src/hooks/useWeightFetch/useWeightFetch.tsx +++ b/packages/fakeyou.com/src/hooks/useWeightFetch/useWeightFetch.tsx @@ -1,7 +1,10 @@ -import { useEffect, useState } from 'react'; -import { useHistory } from 'react-router-dom'; +import { useEffect, useState } from "react"; +import { useHistory } from "react-router-dom"; import { FetchStatus } from "@storyteller/components/src/api/_common/SharedFetchTypes"; -import { GetWeight, Weight } from "@storyteller/components/src/api/weights/GetWeight"; +import { + GetWeight, + Weight, +} from "@storyteller/components/src/api/weights/GetWeight"; import { UpdateWeight } from "@storyteller/components/src/api/weights/UpdateWeight"; import { DeleteWeight } from "@storyteller/components/src/api/weights/DeleteWeight"; @@ -17,66 +20,83 @@ export default function useWeightFetch({ onRemove = () => {}, token }: Props) { const [title, titleSet] = useState(""); const [visibility, visibilitySet] = useState("public"); const [descriptionMD, descriptionMDSet] = useState(""); - const isLoading = status === FetchStatus.ready || status === FetchStatus.in_progress; + const isLoading = + status === FetchStatus.ready || status === FetchStatus.in_progress; const fetchError = status === FetchStatus.error; const history = useHistory(); const onChange = ({ target }: { target: { name: string; value: any } }) => { - const todo: { [key: string]: (x: any) => void } = { descriptionMDSet, titleSet, visibilitySet }; + const todo: { [key: string]: (x: any) => void } = { + descriptionMDSet, + titleSet, + visibilitySet, + }; todo[target.name + "Set"](target.value); }; const update = () => { writeStatusSet(FetchStatus.in_progress); - UpdateWeight(token,{ + UpdateWeight(token, { description_markdown: descriptionMD, description_rendered_html: data?.description_rendered_html || "", title, visibility, weight_category: data?.weights_category || "", - weight_type: data?.weights_type || "" + weight_type: data?.weights_type || "", }) - .then((res: any) => { - console.log("📝",res); - writeStatusSet(FetchStatus.success); - history.replace(`/weight/${ token }`); - }) - .catch(err => { - writeStatusSet(FetchStatus.error); - }); + .then((res: any) => { + writeStatusSet(FetchStatus.success); + history.replace(`/weight/${token}`); + }) + .catch(err => { + writeStatusSet(FetchStatus.error); + }); }; const remove = () => { writeStatusSet(FetchStatus.in_progress); - DeleteWeight(token,{ + DeleteWeight(token, { as_mod: true, - set_delete: true - }) - .then((res: any) => { + set_delete: true, + }).then((res: any) => { writeStatusSet(FetchStatus.success); - console.log("✂️",res); onRemove(res); }); }; - + useEffect(() => { if (token && !data && status === FetchStatus.ready) { statusSet(FetchStatus.in_progress); GetWeight(token, {}) - .then((res: any) => { - let { creator_set_visibility, description_markdown, title: resTitle, ...response } = res; - console.log("🏋️", res, status); - statusSet(FetchStatus.success); - titleSet(resTitle); - descriptionMDSet(description_markdown); - visibilitySet(creator_set_visibility); - setData(response); - }) - .catch(err => { - statusSet(FetchStatus.error); - }); + .then((res: any) => { + let { + creator_set_visibility, + description_markdown, + title: resTitle, + } = res; + statusSet(FetchStatus.success); + titleSet(resTitle); + descriptionMDSet(description_markdown); + visibilitySet(creator_set_visibility); + setData(res); + }) + .catch(err => { + statusSet(FetchStatus.error); + }); } }, [status, token, data]); - return { data, fetchError, isLoading, descriptionMD, onChange, remove, status, title, update, visibility, writeStatus }; -}; \ No newline at end of file + return { + data, + fetchError, + isLoading, + descriptionMD, + onChange, + remove, + status, + title, + update, + visibility, + writeStatus, + }; +} diff --git a/packages/fakeyou.com/src/v2/view/pages/weight/WeightPage.tsx b/packages/fakeyou.com/src/v2/view/pages/weight/WeightPage.tsx index 11abaee32e..82258feb99 100644 --- a/packages/fakeyou.com/src/v2/view/pages/weight/WeightPage.tsx +++ b/packages/fakeyou.com/src/v2/view/pages/weight/WeightPage.tsx @@ -67,9 +67,18 @@ export default function WeightPage({ const { weight_token } = useParams<{ weight_token: string }>(); const origin = search ? new URLSearchParams(search).get("origin") : ""; const history = useHistory(); - const { data: weight, descriptionMD, fetchError, isLoading, title, remove } = useWeightFetch({ - onRemove: () => { history.push(origin || ""); }, - token: weight_token + const { + data: weight, + descriptionMD, + fetchError, + isLoading, + title, + remove, + } = useWeightFetch({ + onRemove: () => { + history.push(origin || ""); + }, + token: weight_token, }); const timeUpdated = moment(weight?.updated_at || "").fromNow(); @@ -230,7 +239,7 @@ export default function WeightPage({ { property: "Category", value: weightCategory }, { property: "Visibility", - value: weight.creator_set_visibility?.toString() || "", + value: weight.creator_set_visibility, }, { property: "Created at", value: weight.created_at?.toString() || "" }, { property: "Updated at", value: weight.updated_at?.toString() || "" }, @@ -241,7 +250,7 @@ export default function WeightPage({ { property: "Category", value: weightCategory }, { property: "Visibility", - value: weight.creator_set_visibility?.toString() || "", + value: weight.creator_set_visibility, }, { property: "Created at", value: dateCreated || "" }, { property: "Updated at", value: dateUpdated || "" },