From fc513eec11da1a1966c9b2b0e4e239c94a3b4fc3 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 11 Jan 2024 01:18:57 -0800 Subject: [PATCH] added ratings/bookmarks to WeightPage --- .../common/Card/AudioCard/AudioCard.tsx | 2 +- .../common/Card/ImageCard/ImageCard.tsx | 2 +- .../hooks/useBatchContent/useBatchContent.tsx | 16 +++++++++---- .../src/hooks/useBookmarks/useBookmarks.tsx | 10 ++++++++ .../src/hooks/useRatings/useRatings.tsx | 2 +- .../hooks/useWeightFetch/useWeightFetch.tsx | 12 ++++++---- .../v2/view/pages/explore/tabs/WeightsTab.tsx | 13 +--------- .../profile/profile_view/tabs/WeightsTab.tsx | 4 ++-- .../src/v2/view/pages/weight/WeightPage.tsx | 24 ++++++++++++------- 9 files changed, 51 insertions(+), 34 deletions(-) diff --git a/packages/fakeyou.com/src/components/common/Card/AudioCard/AudioCard.tsx b/packages/fakeyou.com/src/components/common/Card/AudioCard/AudioCard.tsx index 0c13c0543f..a64d5a4309 100644 --- a/packages/fakeyou.com/src/components/common/Card/AudioCard/AudioCard.tsx +++ b/packages/fakeyou.com/src/components/common/Card/AudioCard/AudioCard.tsx @@ -36,7 +36,7 @@ export default function AudioCard({ type === "media" ? `/media/${data.token}` : `/weight/${data.weight_token || data.details.entity_token}${ - origin ? "?origin=" + origin + "&ehhh=mehh" : "" + origin ? "?origin=" + origin : "" }`; const handleInnerClick = (event: any) => { diff --git a/packages/fakeyou.com/src/components/common/Card/ImageCard/ImageCard.tsx b/packages/fakeyou.com/src/components/common/Card/ImageCard/ImageCard.tsx index 47e239960f..0c16a1e6fb 100644 --- a/packages/fakeyou.com/src/components/common/Card/ImageCard/ImageCard.tsx +++ b/packages/fakeyou.com/src/components/common/Card/ImageCard/ImageCard.tsx @@ -33,7 +33,7 @@ export default function ImageCard({ type === "media" ? `/media/${data.token}` : `/weight/${data.weight_token || data.details.entity_token}${ - origin ? "?origin=" + origin + "&ehhh=mehh" : "" + origin ? "?origin=" + origin : "" }`; const handleInnerClick = (event: any) => { diff --git a/packages/fakeyou.com/src/hooks/useBatchContent/useBatchContent.tsx b/packages/fakeyou.com/src/hooks/useBatchContent/useBatchContent.tsx index fe4eaed073..f6cc85233d 100644 --- a/packages/fakeyou.com/src/hooks/useBatchContent/useBatchContent.tsx +++ b/packages/fakeyou.com/src/hooks/useBatchContent/useBatchContent.tsx @@ -4,6 +4,7 @@ import { FetchStatus } from "@storyteller/components/src/api/_common/SharedFetch interface Props { checker: any, fetcher: any, + modLibrary?: any, onPass?: any, onFail?: any, resultsKey: string @@ -23,6 +24,7 @@ interface Gather { export default function useBatchContent({ checker, fetcher, + modLibrary = (current: any, res: any, entity_token: string ) => current, onPass, onFail, resultsKey }: Props) { @@ -30,17 +32,21 @@ export default function useBatchContent({ const [busyList, busyListSet] = useState({}); const [status, statusSet] = useState(FetchStatus.ready); - const gather = ({ expand, key, modLibrary = (x: any, res: any, entity_token: string ) => x, res }: Gather) => { - let tokens = res.results.map((item: any) => item[key]); - // console.log("🪙",res.results, tokens); + const gather = ({ expand, key, res }: Gather) => { + let tokens = res.results ? res.results.map((item: any) => item[key]) : [res[key]]; + // console.log("🪙",fetcher); busyListSet(tokens.reduce((obj = {},token = "") => ({ ...obj, [token]: true }),{})); // add current batch to busy list fetcher("",{},{ tokens }).then((batchRes: any) => { - // console.log("😡",batchRes,resultsKey); - if (batchRes.success && batchRes[resultsKey]) { + + console.log("🪙", batchRes.success && !!batchRes[resultsKey]); + if (batchRes.success && !!batchRes[resultsKey]) { + let newBatch = batchRes[resultsKey].reduce((obj = {}, { entity_token = "", ...current }) => ({ ...obj, [entity_token]: { ...modLibrary(current, res, entity_token) } }),{}); + + console.log("😡", newBatch); busyListSet({}); // this should be a for each key in tokens delete from busyList, but this is fine for now listSet((list: any) => expand ? { ...list, ...newBatch } : newBatch); } diff --git a/packages/fakeyou.com/src/hooks/useBookmarks/useBookmarks.tsx b/packages/fakeyou.com/src/hooks/useBookmarks/useBookmarks.tsx index 3932d1e65e..96b6daf7e6 100644 --- a/packages/fakeyou.com/src/hooks/useBookmarks/useBookmarks.tsx +++ b/packages/fakeyou.com/src/hooks/useBookmarks/useBookmarks.tsx @@ -25,6 +25,16 @@ export default function useBookmarks() { const { busyList, gather, list, status, toggle } = useBatchContent({ checker: ({ maybe_bookmark_token }: any) => !!maybe_bookmark_token, fetcher: GetBookmarks, + modLibrary: (current: any, res: any, entity_token: string) => { + // let thing = res.results ? res.results.find((item: any, i: number) => + // item.weight_token === entity_token + // ) + let { positive_rating_count } = res.results ? res.results.find((item: any, i: number) => + item.weight_token === entity_token + ).stats : res.stats; + + return { ...current, positive_rating_count }; + }, onPass: { fetch: (entity_token: string, entity_type: string, lib: any) => { let bookmarkToken = lib[entity_token].maybe_bookmark_token; diff --git a/packages/fakeyou.com/src/hooks/useRatings/useRatings.tsx b/packages/fakeyou.com/src/hooks/useRatings/useRatings.tsx index be12ec206b..7d6bb86bf4 100644 --- a/packages/fakeyou.com/src/hooks/useRatings/useRatings.tsx +++ b/packages/fakeyou.com/src/hooks/useRatings/useRatings.tsx @@ -46,7 +46,7 @@ export default function useRatings() { resultsKey: "ratings" }); - console.log("🔮",list); + // console.log("🔮",list); return { busyList, gather, list, status, toggleStatus, toggleStatusSet, toggle }; }; \ No newline at end of file diff --git a/packages/fakeyou.com/src/hooks/useWeightFetch/useWeightFetch.tsx b/packages/fakeyou.com/src/hooks/useWeightFetch/useWeightFetch.tsx index 246b751654..b885972460 100644 --- a/packages/fakeyou.com/src/hooks/useWeightFetch/useWeightFetch.tsx +++ b/packages/fakeyou.com/src/hooks/useWeightFetch/useWeightFetch.tsx @@ -7,11 +7,14 @@ import { DeleteWeight } from "@storyteller/components/src/api/weights/DeleteWeig import { useCoverImgUpload } from "hooks"; interface Props { - onRemove?: (x: any) => void; - token: string; + onSuccess?: (res: Weight) => any, + onRemove?: (x: any) => void, + token: string } -export default function useWeightFetch({ onRemove = () => {}, token }: Props) { +const n = (x?: any) => {}; + +export default function useWeightFetch({ onRemove = n, onSuccess = n, token }: Props) { const [data, setData] = useState(null); const [status, statusSet] = useState(FetchStatus.ready); const [writeStatus, writeStatusSet] = useState(FetchStatus.paused); @@ -77,13 +80,14 @@ export default function useWeightFetch({ onRemove = () => {}, token }: Props) { titleSet(resTitle); descriptionMDSet(description_markdown); visibilitySet(creator_set_visibility); + onSuccess(res); setData(res); }) .catch(err => { statusSet(FetchStatus.error); }); } - }, [status, token, data]); + }, [status, onSuccess, token, data]); return { coverImg, data, fetchError, isLoading, descriptionMD, onChange, remove, status, title, update, visibility, writeStatus }; }; diff --git a/packages/fakeyou.com/src/v2/view/pages/explore/tabs/WeightsTab.tsx b/packages/fakeyou.com/src/v2/view/pages/explore/tabs/WeightsTab.tsx index 106fb2fd26..ab391c50fb 100644 --- a/packages/fakeyou.com/src/v2/view/pages/explore/tabs/WeightsTab.tsx +++ b/packages/fakeyou.com/src/v2/view/pages/explore/tabs/WeightsTab.tsx @@ -38,18 +38,7 @@ export default function WeightsTab() { onInputChange: () => setShowMasonryGrid(false), onSuccess: (res) => { bookmarks.gather({ res, expand: true, key: "weight_token" }); // expand rather than replace for lazy loading - ratings.gather({ - res, - expand: true, - key: "weight_token", - modLibrary: (current: any, res: any, entity_token: string) => { - let { positive_rating_count } = res.results.find((item: any, i: number) => - item.weight_token === entity_token - ).stats; - - return { ...current, positive_rating_count }; - } - }); + ratings.gather({ res, expand: true, key: "weight_token" }); setShowMasonryGrid(true); }, requestList: true, diff --git a/packages/fakeyou.com/src/v2/view/pages/profile/profile_view/tabs/WeightsTab.tsx b/packages/fakeyou.com/src/v2/view/pages/profile/profile_view/tabs/WeightsTab.tsx index 83bcdf42d5..1aeb63f60a 100644 --- a/packages/fakeyou.com/src/v2/view/pages/profile/profile_view/tabs/WeightsTab.tsx +++ b/packages/fakeyou.com/src/v2/view/pages/profile/profile_view/tabs/WeightsTab.tsx @@ -49,8 +49,8 @@ export default function WeightsTab({ username }: { username: string }) { listSet, onInputChange: () => setShowMasonryGrid(false), onSuccess: (res) => { - bookmarks.gather({ res, key: "weight_token" }); - // ratings.gather(res); + bookmarks.gather({ res, key: "weight_token" }); // expand rather than replace for lazy loading + ratings.gather({ res, key: "weight_token" }); setShowMasonryGrid(true); }, requestList: true, 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 a0a92091b2..e69d07c016 100644 --- a/packages/fakeyou.com/src/v2/view/pages/weight/WeightPage.tsx +++ b/packages/fakeyou.com/src/v2/view/pages/weight/WeightPage.tsx @@ -33,7 +33,7 @@ import TtsInferencePanel from "./inference_panels/TtsInferencePanel"; import Modal from "components/common/Modal"; import SocialButton from "components/common/SocialButton"; import Input from "components/common/Input"; -import { useBookmarks, useWeightFetch } from "hooks"; +import { useBookmarks, useWeightFetch, useRatings } from "hooks"; import useWeightTypeInfo from "hooks/useWeightTypeInfo/useWeightTypeInfo"; import moment from "moment"; import WeightCoverImage from "components/common/WeightCoverImage"; @@ -67,6 +67,8 @@ export default function WeightPage({ const { weight_token } = useParams<{ weight_token: string }>(); const origin = search ? new URLSearchParams(search).get("origin") : ""; const history = useHistory(); + const bookmarks = useBookmarks(); + const ratings = useRatings(); const { data: weight, descriptionMD, @@ -78,6 +80,10 @@ export default function WeightPage({ onRemove: () => { history.push(origin || ""); }, + onSuccess: (res: any) => { + bookmarks.gather({ res, key: "weight_token" }); // expand rather than replace for lazy loading + ratings.gather({ res, key: "weight_token" }); + }, token: weight_token, }); @@ -88,8 +94,6 @@ export default function WeightPage({ const [buttonLabel, setButtonLabel] = useState("Copy"); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); - const bookmarks = useBookmarks(); - const bucketConfig = new BucketConfig(); const weightTypeInfo = useWeightTypeInfo( @@ -307,6 +311,7 @@ export default function WeightPage({ ); } + // const handleBookmark = () => { // return bookmarks.toggle(); // this function checks if the bookmark exists, truthy = deleted, falsy = created // }; @@ -375,20 +380,23 @@ export default function WeightPage({