From ba8399a0dea2f29f939eec75b1a4a4f2da6f0dfb Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Tue, 24 Oct 2023 18:27:14 -0300 Subject: [PATCH] feat(market-card.tsx): add useContext, useRef, and useState imports to improve code readability and maintainability feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support for process.env.PORT environment variable to be able to run app on a configurable port feat(market-card.tsx): add support --- .../StorePage/components/market-card.tsx | 70 ++++++++++++++++--- src/frontend/src/pages/StorePage/index.tsx | 45 +----------- 2 files changed, 61 insertions(+), 54 deletions(-) diff --git a/src/frontend/src/pages/StorePage/components/market-card.tsx b/src/frontend/src/pages/StorePage/components/market-card.tsx index 25343c090..13e4eeb2f 100644 --- a/src/frontend/src/pages/StorePage/components/market-card.tsx +++ b/src/frontend/src/pages/StorePage/components/market-card.tsx @@ -1,5 +1,5 @@ import { Link, ToyBrick } from "lucide-react"; -import { useState } from "react"; +import { useContext, useRef, useState } from "react"; import IconComponent from "../../../components/genericIconComponent"; import { Badge } from "../../../components/ui/badge"; import { Button } from "../../../components/ui/button"; @@ -10,16 +10,65 @@ import { CardHeader, CardTitle, } from "../../../components/ui/card"; +import { TabsContext } from "../../../contexts/tabsContext"; +import { getComponent, saveFlowStore } from "../../../controllers/API"; +import { FlowType } from "../../../types/flow"; import { FlowComponent } from "../../../types/store"; +import cloneFLowWithParent from "../../../utils/storeUtils"; -export const MarketCardComponent = ({ - data, - onAdd, -}: { - data: FlowComponent; - onAdd: () => void; -}) => { +export const MarketCardComponent = ({ data }: { data: FlowComponent }) => { const [added, setAdded] = useState(false); + const { addFlow } = useContext(TabsContext); + const flowData = useRef(); + + function handleAdd() { + getComponent(data.id).then( + (res) => { + console.log(res); + const newFLow = cloneFLowWithParent( + res.data, + res.id, + data.is_component + ); + flowData.current = newFLow; + console.log(newFLow); + saveFlowStore(newFLow) + .then(() => setAdded(true)) + .catch((error) => { + console.error(error); + }); + }, + (error) => { + console.log(error); + } + ); + } + + function handleInstall() { + addFlow(true, flowData.current!); + } + + function handleFork(flowId: string, is_component: boolean) { + getComponent(flowId).then( + (res) => { + console.log(res); + const newFLow = cloneFLowWithParent(res.data, res.id, is_component); + console.log(newFLow); + saveFlowStore(newFLow).then( + (res) => { + console.log(JSON.parse(JSON.stringify(res))); + addFlow(true, newFLow); + }, + (error) => { + console.error(error); + } + ); + }, + (error) => { + console.log(error); + } + ); + } return ( @@ -97,11 +146,10 @@ export const MarketCardComponent = ({ className="whitespace-nowrap " onClick={() => { if (!added) { - setAdded(true); + handleAdd(); } else { - //download + handleInstall(); } - onAdd(); }} > { - console.log(res); - const newFLow = cloneFLowWithParent(res.data, res.id, is_component); - console.log(newFLow); - saveFlowStore(newFLow).then( - (res) => { - console.log(JSON.parse(JSON.stringify(res))); - addFlow(true, newFLow); - }, - (error) => { - console.error(error); - } - ); - }, - (error) => { - console.log(error); - } - ); - } - return ( <>
@@ -223,21 +195,8 @@ export default function StorePage(): JSX.Element { filteredCategories.has(f.is_component) ) .map((item, idx) => ( - { - handleFork(item.id, item.is_component); - }} - /> + ))} - )}