From 1f43b08b7f961d69c5492b5333858e733e75b067 Mon Sep 17 00:00:00 2001 From: Liam Arbuckle Date: Mon, 27 Nov 2023 13:45:40 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B6=E2=98=98=EF=B8=8F=20=E2=86=9D=20Ca?= =?UTF-8?q?n=20now=20create=20structures=20on=20each=20sector!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Planets/Activities/StructureCreate.tsx | 47 +++++++++++++++---- .../Base/IndividualBasePlanetSector.tsx | 2 + pages/planets/[id].tsx | 5 +- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/components/Content/Planets/Activities/StructureCreate.tsx b/components/Content/Planets/Activities/StructureCreate.tsx index 697dc5ae..ae88c210 100644 --- a/components/Content/Planets/Activities/StructureCreate.tsx +++ b/components/Content/Planets/Activities/StructureCreate.tsx @@ -1,5 +1,5 @@ import React, { useEffect, useState } from "react"; -import { useSupabaseClient } from "@supabase/auth-helpers-react"; +import { useSession, useSupabaseClient } from "@supabase/auth-helpers-react"; interface Structure { id: number; @@ -10,13 +10,14 @@ interface Structure { interface StructureSelectionProps { onStructureSelected: (structure: Structure) => void; + planetSectorId: number; }; -const StructureSelection: React.FC = ({ onStructureSelected }) => { +const StructureSelection: React.FC = ({ onStructureSelected, planetSectorId }) => { const supabase = useSupabaseClient(); + const session = useSession(); const [structures, setStructures] = useState([]); - const [isCalloutOpen, setIsCalloutOpen] = useState(false); const fetchStructures = async () => { @@ -28,14 +29,41 @@ const StructureSelection: React.FC = ({ onStructureSele if (data) { setStructures(data); - }; + } if (error) { console.error(error.message); - }; + } } catch (error) { console.error(error.message); - }; + } + }; + + const createInventoryUserEntry = async (structure: Structure) => { + if (session && planetSectorId) { + try { + const { data, error } = await supabase + .from('inventoryUSERS') + .upsert([ + { + item: structure.id, + owner: session.user.id, + quantity: 1, // You can adjust the quantity as needed + planetSector: planetSectorId, + }, + ]); + + if (data) { + console.log('Inventory user entry created:', data); + } + + if (error) { + console.error(error.message); + } + } catch (error) { + console.error(error.message); + } + } }; useEffect(() => { @@ -44,6 +72,7 @@ const StructureSelection: React.FC = ({ onStructureSele const handleStructureClick = (structure: Structure) => { onStructureSelected(structure); + createInventoryUserEntry(structure); setIsCalloutOpen(false); }; @@ -67,7 +96,7 @@ const StructureSelection: React.FC = ({ onStructureSele onClick={() => handleStructureClick(structure)} >
- {structure.name} + {structure.name} {structure.name}
{structure.description} @@ -80,14 +109,14 @@ const StructureSelection: React.FC = ({ onStructureSele ); }; -export default function StructureComponent () { +export default function StructureComponent({ sectorId }) { const handleStructureSelected = (structure) => { console.log('Selected structure: ', structure); }; return (
- +
); }; \ No newline at end of file diff --git a/components/Content/Planets/Base/IndividualBasePlanetSector.tsx b/components/Content/Planets/Base/IndividualBasePlanetSector.tsx index ce49745b..7e2a1da6 100644 --- a/components/Content/Planets/Base/IndividualBasePlanetSector.tsx +++ b/components/Content/Planets/Base/IndividualBasePlanetSector.tsx @@ -5,6 +5,7 @@ import Card from "../../../Card"; import RoverImageGallery, { RoverImage, RoverImageNoHandle } from "../PlanetData/RandomRoverImage"; import axios from "axios"; import { RoverContentPostForm } from "../../CreatePostForm"; +import StructureComponent from "../Activities/StructureCreate"; export default function BasePlanetSector({ sectorid }: { sectorid: string }) { const router = useRouter(); @@ -198,6 +199,7 @@ export default function BasePlanetSector({ sectorid }: { sectorid: string }) {
+ {/* {imageUrl ? ( <> Rover image diff --git a/pages/planets/[id].tsx b/pages/planets/[id].tsx index 9121ce8e..04bf0a02 100644 --- a/pages/planets/[id].tsx +++ b/pages/planets/[id].tsx @@ -94,7 +94,7 @@ export default function PlanetIdPage () { return ( <> - {/* */} +
@@ -106,8 +106,7 @@ export default function PlanetIdPage () {
-
+ />