1
1
import React , { useEffect , useState } from "react" ;
2
- import { useSupabaseClient } from "@supabase/auth-helpers-react" ;
2
+ import { useSession , useSupabaseClient } from "@supabase/auth-helpers-react" ;
3
3
4
4
interface Structure {
5
5
id : number ;
@@ -10,13 +10,14 @@ interface Structure {
10
10
11
11
interface StructureSelectionProps {
12
12
onStructureSelected : ( structure : Structure ) => void ;
13
+ planetSectorId : number ;
13
14
} ;
14
15
15
- const StructureSelection : React . FC < StructureSelectionProps > = ( { onStructureSelected } ) => {
16
+ const StructureSelection : React . FC < StructureSelectionProps > = ( { onStructureSelected, planetSectorId } ) => {
16
17
const supabase = useSupabaseClient ( ) ;
18
+ const session = useSession ( ) ;
17
19
18
20
const [ structures , setStructures ] = useState < Structure [ ] > ( [ ] ) ;
19
-
20
21
const [ isCalloutOpen , setIsCalloutOpen ] = useState ( false ) ;
21
22
22
23
const fetchStructures = async ( ) => {
@@ -28,14 +29,41 @@ const StructureSelection: React.FC<StructureSelectionProps> = ({ onStructureSele
28
29
29
30
if ( data ) {
30
31
setStructures ( data ) ;
31
- } ;
32
+ }
32
33
33
34
if ( error ) {
34
35
console . error ( error . message ) ;
35
- } ;
36
+ }
36
37
} catch ( error ) {
37
38
console . error ( error . message ) ;
38
- } ;
39
+ }
40
+ } ;
41
+
42
+ const createInventoryUserEntry = async ( structure : Structure ) => {
43
+ if ( session && planetSectorId ) {
44
+ try {
45
+ const { data, error } = await supabase
46
+ . from ( 'inventoryUSERS' )
47
+ . upsert ( [
48
+ {
49
+ item : structure . id ,
50
+ owner : session . user . id ,
51
+ quantity : 1 , // You can adjust the quantity as needed
52
+ planetSector : planetSectorId ,
53
+ } ,
54
+ ] ) ;
55
+
56
+ if ( data ) {
57
+ console . log ( 'Inventory user entry created:' , data ) ;
58
+ }
59
+
60
+ if ( error ) {
61
+ console . error ( error . message ) ;
62
+ }
63
+ } catch ( error ) {
64
+ console . error ( error . message ) ;
65
+ }
66
+ }
39
67
} ;
40
68
41
69
useEffect ( ( ) => {
@@ -44,6 +72,7 @@ const StructureSelection: React.FC<StructureSelectionProps> = ({ onStructureSele
44
72
45
73
const handleStructureClick = ( structure : Structure ) => {
46
74
onStructureSelected ( structure ) ;
75
+ createInventoryUserEntry ( structure ) ;
47
76
setIsCalloutOpen ( false ) ;
48
77
} ;
49
78
@@ -67,7 +96,7 @@ const StructureSelection: React.FC<StructureSelectionProps> = ({ onStructureSele
67
96
onClick = { ( ) => handleStructureClick ( structure ) }
68
97
>
69
98
< div className = "flex items-center space-x-2" >
70
- < img src = { structure . icon_url } alt = { structure . name } className = "w-9 h-9 " />
99
+ < img src = { structure . icon_url } alt = { structure . name } className = "w-8 h-8 " />
71
100
< span className = "font-bold" > { structure . name } </ span >
72
101
</ div >
73
102
< span className = "text-gray-500" > { structure . description } </ span >
@@ -80,14 +109,14 @@ const StructureSelection: React.FC<StructureSelectionProps> = ({ onStructureSele
80
109
) ;
81
110
} ;
82
111
83
- export default function StructureComponent ( ) {
112
+ export default function StructureComponent ( { sectorId } ) {
84
113
const handleStructureSelected = ( structure ) => {
85
114
console . log ( 'Selected structure: ' , structure ) ;
86
115
} ;
87
116
88
117
return (
89
118
< div >
90
- < StructureSelection onStructureSelected = { handleStructureSelected } />
119
+ < StructureSelection onStructureSelected = { handleStructureSelected } planetSectorId = { sectorId } />
91
120
</ div >
92
121
) ;
93
122
} ;
0 commit comments