added GetWeight fetch to weight page

This commit is contained in:
Victor committed 2023-12-22 12:54:28 -05:00
1 parent f207a0fd22
commit 5c2782e638
2 files changed
+27 -49

No files matched your search

@@ -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<GetWeightResponse> {
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<string,GetWeightRequest,GetWeightResponse,{}>({
method: "GET",
routingFunction: (weightToken: string) => `/v1/weights/weight/${weightToken}`,
});
@@ -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<Weight | undefined | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(true);
const [error, setError] = useState<boolean>(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
];