@@ -4,127 +4,176 @@ import React, { useEffect, useState } from "react";
4
4
5
5
export default function CreateBasePlanetSector ( ) {
6
6
const supabase = useSupabaseClient ( ) ;
7
- const session = useSession ( ) ;
7
+ const session = useSession ( ) ;
8
8
9
- const [ userPlanet , setUserPlanet ] = useState ( null ) ;
9
+ const [ userPlanet , setUserPlanet ] = useState ( null ) ;
10
10
11
- // Get the planet that the user owns
11
+ // Get the planet that the user owns
12
+ useEffect ( ( ) => {
12
13
const fetchUserPlanet = async ( ) => {
13
- if ( ! session ) {
14
- return ;
15
- } ;
14
+ if ( ! session ) return ;
16
15
17
- try {
18
- const { data, error } = await supabase
19
- . from ( 'profiles' )
20
- . select ( '*' )
21
- . eq ( 'id' , session ?. user ?. id )
22
- . single ( ) ;
16
+ try {
17
+ const { data, error } = await supabase
18
+ . from ( 'profiles' )
19
+ . select ( '*' )
20
+ . eq ( 'id' , session ?. user ?. id )
21
+ . single ( ) ;
23
22
24
- if ( data ) {
25
- setUserPlanet ( data . location ) ;
26
- } ;
23
+ if ( data ) {
24
+ setUserPlanet ( data . location ) ;
25
+ }
27
26
28
- if ( error ) {
29
- throw error ;
30
- } ;
31
- } catch ( error : any ) {
32
- console . error ( error . message ) ;
33
- } ;
27
+ if ( error ) {
28
+ throw error ;
29
+ }
30
+ } catch ( error ) {
31
+ console . error ( error . message ) ;
32
+ }
34
33
} ;
35
34
36
35
fetchUserPlanet ( ) ;
37
-
38
- const createSector = async ( ) => {
39
- if ( session ) {
40
- fetchUserPlanet ( ) ;
41
-
42
- // Map resource names to corresponding inventoryITEMS ids
43
- const resourceToIdMap = {
44
- "Silicates" : 13 ,
45
- "Alloy" : 12 ,
46
- "Iron" : 11 ,
47
- "Fuel" : 10 ,
48
- "Water" : 9 ,
49
- "Coal" : 11 ,
50
- } ;
51
-
52
- // Choose between Silicates and Coal for testing
53
- const depositResource = Math . random ( ) < 0.5 ? "Silicates" : "Coal" ;
54
-
55
- // Get the corresponding id from the map
56
- const depositRowId = resourceToIdMap [ depositResource ] ;
57
-
58
- const response = await supabase . from ( 'basePlanetSectors' ) . upsert ( [
59
- {
60
- anomaly : userPlanet ,
61
- owner : session ?. user ?. id ,
62
- deposit : depositRowId , // Use the id instead of the resource name
63
- coverUrl : "https://mars.nasa.gov/mars2020-raw-images/pub/ods/surface/sol/00090/ids/edr/browse/edl/EBE_0090_0674952393_193ECM_N0040048EDLC00090_0030LUJ01_1200.jpg" ,
64
- explored : false ,
65
- } ,
66
- ] ) ;
67
-
68
- if ( response . error ) {
69
- console . error ( response . error ) ;
70
- } else {
71
- // Handle success
72
- }
73
- }
74
- } ;
75
-
76
- return (
77
- < div >
78
- < pre > { userPlanet } </ pre >
79
- < button onClick = { createSector } > Create sector</ button >
80
- </ div >
81
- ) ;
36
+ } , [ session , supabase ] ) ;
37
+
38
+ const createSector = async ( ) => {
39
+ if ( session ) {
40
+ // Map resource names to corresponding inventoryITEMS ids
41
+ const resourceToIdMap = {
42
+ "Silicates" : 13 ,
43
+ "Alloy" : 12 ,
44
+ "Iron" : 11 ,
45
+ "Fuel" : 10 ,
46
+ "Water" : 9 ,
47
+ "Coal" : 11 ,
48
+ } ;
49
+
50
+ // Choose between Silicates and Coal for testing
51
+ const depositResource = Math . random ( ) < 0.5 ? "Silicates" : "Coal" ;
52
+
53
+ // Get the corresponding id from the map
54
+ const depositRowId = resourceToIdMap [ depositResource ] ;
55
+
56
+ const response = await supabase . from ( 'basePlanetSectors' ) . upsert ( [
57
+ {
58
+ anomaly : userPlanet ,
59
+ owner : session ?. user ?. id ,
60
+ deposit : depositRowId , // Use the id instead of the resource name
61
+ coverUrl : "https://mars.nasa.gov/mars2020-raw-images/pub/ods/surface/sol/00090/ids/edr/browse/edl/EBE_0090_0674952393_193ECM_N0040048EDLC00090_0030LUJ01_1200.jpg" ,
62
+ explored : false ,
63
+ } ,
64
+ ] ) ;
65
+
66
+ if ( response . error ) {
67
+ console . error ( response . error ) ;
68
+ } else {
69
+ // Handle success
70
+ }
71
+ }
72
+ } ;
73
+
74
+ return (
75
+ < div className = "mt-4" >
76
+ < button
77
+ onClick = { createSector }
78
+ className = "bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
79
+ >
80
+ Create Sector
81
+ </ button >
82
+ </ div >
83
+ ) ;
82
84
} ;
83
85
84
86
export function UserOwnedSectorGrid ( ) {
85
87
const supabase = useSupabaseClient ( ) ;
86
88
const session = useSession ( ) ;
87
-
89
+
88
90
const [ sectorData , setSectorData ] = useState ( [ ] ) ;
89
-
91
+
90
92
useEffect ( ( ) => {
91
- const fetchUserSectorImages = async ( ) => {
92
- if ( session ) {
93
- try {
94
- const { data, error } = await supabase
95
- . from ( "basePlanetSectors" )
96
- . select ( 'id, coverUrl' )
97
- . eq ( 'owner' , session ?. user ?. id ) ;
98
-
99
- if ( data ) {
100
- setSectorData ( data ) ;
101
- } ;
102
-
103
- if ( error ) {
104
- throw error ;
105
- } ;
106
- } catch ( error ) {
107
- console . error ( error . message ) ;
108
- } ;
109
- } ;
110
- } ;
111
-
112
- fetchUserSectorImages ( ) ;
93
+ const fetchUserSectorImages = async ( ) => {
94
+ if ( session ) {
95
+ try {
96
+ const { data, error } = await supabase
97
+ . from ( "basePlanetSectors" )
98
+ . select ( 'id, coverUrl' )
99
+ . eq ( 'owner' , session ?. user ?. id ) ;
100
+
101
+ if ( data ) {
102
+ setSectorData ( data ) ;
103
+ }
104
+
105
+ if ( error ) {
106
+ throw error ;
107
+ }
108
+ } catch ( error ) {
109
+ console . error ( error . message ) ;
110
+ }
111
+ }
112
+ } ;
113
+
114
+ fetchUserSectorImages ( ) ;
113
115
} , [ session , supabase ] ) ;
114
-
116
+
115
117
return (
116
- < div className = "grid grid-cols-4 gap-2 p -4" >
117
- { sectorData . map ( ( item ) => (
118
- < Link href = { `/planets/sector/${ item . id } ` } > < div
119
- key = { item . id }
120
- className = "relative overflow-hidden bg-center bg-cover"
121
- style = { {
122
- backgroundImage : `url( ${ item . coverUrl } )` ,
123
- paddingBottom : '100%' ,
124
- // backgroundPosition: `-${(index % 4) * 25}% -${Math.floor(index / 4) * 25}%`,
125
- } }
126
- > </ div > </ Link >
127
- ) ) }
128
- </ div >
118
+ < div className = "grid grid-cols-4 gap-4" >
119
+ { sectorData . map ( ( item ) => (
120
+ < Link legacyBehavior key = { item . id } href = { `/planets/sector/${ item . id } ` } passHref >
121
+ < a className = "block aspect-w-3 aspect-h-2 bg-gray-200 hover:bg-gray-300" >
122
+ < img
123
+ src = { item . coverUrl }
124
+ alt = "Sector"
125
+ className = "object-cover w-full h-full"
126
+ />
127
+ </ a >
128
+ </ Link >
129
+ ) ) }
130
+ </ div >
129
131
) ;
132
+ } ;
133
+
134
+ export function AllSectors ( ) {
135
+ const supabase = useSupabaseClient ( ) ;
136
+ const session = useSession ( ) ;
137
+
138
+ const [ sectorData , setSectorData ] = useState ( [ ] ) ;
139
+
140
+ useEffect ( ( ) => {
141
+ const fetchSectorContent = async ( ) => {
142
+ if ( session ) {
143
+ try {
144
+ const { data, error } = await supabase
145
+ . from ( "basePlanetSectors" )
146
+ . select ( 'id, coverUrl' ) ;
147
+
148
+ if ( data ) {
149
+ setSectorData ( data ) ;
150
+ }
151
+
152
+ if ( error ) {
153
+ throw error ;
154
+ }
155
+ } catch ( error ) {
156
+ console . error ( error . message ) ;
157
+ }
158
+ }
159
+ } ;
160
+
161
+ fetchSectorContent ( ) ;
162
+ } , [ session , supabase ] ) ;
163
+
164
+ return (
165
+ < div className = "grid grid-cols-4 gap-4" >
166
+ { sectorData . map ( ( item ) => (
167
+ < Link legacyBehavior key = { item . id } href = { `/planets/sector/${ item . id } ` } passHref >
168
+ < a className = "block aspect-w-3 aspect-h-2 bg-gray-200 hover:bg-gray-300" >
169
+ < img
170
+ src = { item . coverUrl }
171
+ alt = "Sector"
172
+ className = "object-cover w-full h-full"
173
+ />
174
+ </ a >
175
+ </ Link >
176
+ ) ) }
177
+ </ div >
178
+ ) ;
130
179
} ;
0 commit comments