weight response fix visibility

This commit is contained in:
BFlat committed 2023-12-30 00:07:28 +07:00
1 parent c32203bb6d
commit 085172b88b
2 files changed
+69 -40

No files matched your search

@@ -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 };
};
return {
data,
fetchError,
isLoading,
descriptionMD,
onChange,
remove,
status,
title,
update,
visibility,
writeStatus,
};
}
@@ -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 || "" },