diff --git a/packages/components/src/api/weights/GetWeight.ts b/packages/components/src/api/weights/GetWeight.ts index c7013c3605..3b30093fb3 100644 --- a/packages/components/src/api/weights/GetWeight.ts +++ b/packages/components/src/api/weights/GetWeight.ts @@ -1,4 +1,4 @@ -import { ApiConfig } from "../ApiConfig"; +import MakeRequest from "../MakeRequest"; import { UserDetailsLight } from "../_common/UserDetailsLight"; import { WeightCategory } from "../_common/enums/WeightCategory"; import { WeightType } from "../_common/enums/WeightType"; @@ -21,36 +21,15 @@ export interface Weight { maybe_cover_image_public_bucket_path: string | null; version: number; } + +export interface GetWeightRequest {} + export interface GetWeightResponse { success: boolean; - weight?: Weight; + media_file?: Weight; } -export async function GetWeight( - weightToken: string -): Promise { - const endpoint = new ApiConfig().getWeight(weightToken); - - return await fetch(endpoint, { - method: "GET", - headers: { - Accept: "application/json", - }, - credentials: "include", - }) - .then(res => res.json()) - .then(res => { - const response: GetWeightResponse = res; - - if (response && response.success && response.weight) { - response.weight.created_at = new Date(response.weight.created_at); - response.weight.updated_at = new Date(response.weight.updated_at); - return response; - } else { - return { success: false }; - } - }) - .catch(e => { - return { success: false }; - }); -} +export const GetWeight = MakeRequest({ + method: "GET", + routingFunction: (weightToken: string) => `/v1/weights/weight/${weightToken}`, +}); \ No newline at end of file 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 b82ddb33e7..7623814c2c 100644 --- a/packages/fakeyou.com/src/v2/view/pages/weight/WeightPage.tsx +++ b/packages/fakeyou.com/src/v2/view/pages/weight/WeightPage.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useEffect, useState } from "react"; +import React, { useEffect, useState } from "react"; import { Link, useParams } from "react-router-dom"; import { SessionWrapper } from "@storyteller/components/src/session/SessionWrapper"; import { Weight } from "@storyteller/components/src/api/weights/GetWeight"; @@ -62,25 +62,24 @@ export default function WeightPage({ const [weight, setWeight] = useState(null); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(false); - const timeUpdated = useTimeAgo(weight?.updated_at.toISOString() || ""); + const timeUpdated = useTimeAgo(weight?.updated_at?.toISOString() || ""); const [isShareModalOpen, setIsShareModalOpen] = useState(false); const [buttonLabel, setButtonLabel] = useState("Copy"); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); - const getWeight = useCallback(async (mediaFileToken: string) => { - let result = await GetWeight(mediaFileToken); - if (result.weight) { - setWeight(result.weight); - setIsLoading(false); - } else { - setError(true); - setIsLoading(false); - } - }, []); - useEffect(() => { - getWeight(weight_token); - }, [weight_token, getWeight]); + if (weight_token && !weight && isLoading) { + GetWeight(weight_token,{}) + .then((res: any) => { + console.log("🏋️",res); + setIsLoading(false); + setWeight(res); + }) + .catch((err) => { + setError(err); + }); + } + },[isLoading, weight, weight_token]); function renderWeightComponent(weight: Weight) { switch (weight.weights_category) { @@ -245,8 +244,8 @@ export default function WeightPage({ property: "Visibility", value: weight.creator_set_visibility.toString(), }, - { property: "Created at", value: weight.created_at.toString() }, - { property: "Updated at", value: weight.updated_at.toString() }, + { property: "Created at", value: weight.created_at?.toString() || "" }, + { property: "Updated at", value: weight.updated_at?.toString() || "" }, ]; const imageDetails = [ @@ -256,8 +255,8 @@ export default function WeightPage({ property: "Visibility", value: weight.creator_set_visibility.toString(), }, - { property: "Created at", value: weight.created_at.toString() }, - { property: "Updated at", value: weight.updated_at.toString() }, + { property: "Created at", value: weight.created_at?.toString() || "" }, + { property: "Updated at", value: weight.updated_at?.toString() || "" }, //more to add for image/stable diffusion details ];