@@ -7,88 +7,121 @@ export default function CreateBasePlanetSector() {
7
7
const supabase = useSupabaseClient ( ) ;
8
8
const session = useSession ( ) ;
9
9
const [ userPlanet , setUserPlanet ] = useState ( null ) ;
10
-
10
+ const [ imageUrl , setImageUrl ] = useState ( '' ) ;
11
+
11
12
useEffect ( ( ) => {
12
- const fetchUserPlanet = async ( ) => {
13
- if ( ! session ) return ;
14
-
13
+ const fetchUserPlanet = async ( ) => {
14
+ if ( ! session ) return ;
15
+
16
+ try {
17
+ const { data, error } = await supabase
18
+ . from ( 'profiles' )
19
+ . select ( '*' )
20
+ . eq ( 'id' , session ?. user ?. id )
21
+ . single ( ) ;
22
+
23
+ if ( data ) {
24
+ setUserPlanet ( data . location ) ;
25
+ }
26
+
27
+ if ( error ) {
28
+ throw error ;
29
+ }
30
+ } catch ( error ) {
31
+ console . error ( error . message ) ;
32
+ }
33
+ } ;
34
+
35
+ fetchUserPlanet ( ) ;
36
+ } , [ session , supabase ] ) ;
37
+
38
+ const fetchRoverImage = async ( ) => {
39
+ const apiKey = 'iT0FQTZKpvadCGPzerqXdO5F4b62arNBOP0dtkXE' ;
40
+ const date = Math . floor ( Math . random ( ) * 200 ) + 1 ; // Generate a random date
41
+
15
42
try {
16
- const { data, error } = await supabase
17
- . from ( 'profiles' )
18
- . select ( '*' )
19
- . eq ( 'id' , session ?. user ?. id )
20
- . single ( ) ;
21
-
22
- if ( data ) {
23
- setUserPlanet ( data . location ) ;
24
- }
25
-
26
- if ( error ) {
27
- throw error ;
28
- }
43
+ const apiUrl = `https://api.nasa.gov/mars-photos/api/v1/rovers/perseverance/photos?sol=${ date } &api_key=${ apiKey } ` ;
44
+ const response = await axios . get ( apiUrl ) ;
45
+
46
+ if ( response . data . photos && response . data . photos . length > 0 ) {
47
+ const firstImage = response . data . photos [ 0 ] . img_src ;
48
+ setImageUrl ( firstImage ) ;
49
+ } else {
50
+ console . error ( 'No images found for the given date & rover:' , date ) ;
51
+ // Retry fetching image with a new date
52
+ fetchRoverImage ( ) ;
53
+ }
29
54
} catch ( error ) {
30
- console . error ( error . message ) ;
55
+ console . error ( 'Error fetching image:' , error ) ;
56
+ // Retry fetching image with a new date
57
+ fetchRoverImage ( ) ;
31
58
}
32
- } ;
33
-
34
- fetchUserPlanet ( ) ;
35
- } , [ session , supabase ] ) ;
36
-
59
+ } ;
60
+
61
+ useEffect ( ( ) => {
62
+ fetchRoverImage ( ) ;
63
+ } , [ session ] ) ;
64
+
37
65
const createSector = async ( ) => {
38
- if ( session ) {
39
- try {
40
- // Map resource names to corresponding inventoryITEMS ids
41
- const resourceToIdMap = {
42
- "Coal" : 11 ,
43
- "Silicates" : 13 ,
44
- "Iron" : 15 ,
45
- "Alloy" : 17 ,
46
- "Fuel" : 18 ,
47
- "Copper" : 19 ,
48
- "Chromium" : 20 ,
49
- "Nickel" : 16 ,
50
- "Water" : 21 ,
51
- } ;
52
-
53
- // Randomly choose a mineral from the resourceToIdMap
54
- const minerals = Object . keys ( resourceToIdMap ) ;
55
- const randomMineral = minerals [ Math . floor ( Math . random ( ) * minerals . length ) ] ;
56
-
57
- // Get the corresponding id from the map
58
- const depositRowId = resourceToIdMap [ randomMineral ] ;
59
-
60
- const response = await supabase . from ( 'basePlanetSectors' ) . upsert ( [
61
- {
62
- anomaly : userPlanet ,
63
- owner : session ?. user ?. id ,
64
- deposit : depositRowId , // Use the id instead of the resource name
65
- 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" ,
66
- explored : false ,
67
- } ,
68
- ] ) ;
69
-
70
- if ( response . error ) {
71
- console . error ( response . error ) ;
72
- } else {
73
- // Handle success
74
- }
75
- } catch ( error ) {
76
- console . error ( "Error creating sector:" , error . message ) ;
66
+ if ( ! imageUrl ) {
67
+ console . error ( 'No image available. Please wait until an image is fetched.' ) ;
68
+ return ;
69
+ }
70
+
71
+ if ( session ) {
72
+ try {
73
+ // Map resource names to corresponding inventoryITEMS ids
74
+ const resourceToIdMap = {
75
+ "Coal" : 11 ,
76
+ "Silicates" : 13 ,
77
+ "Iron" : 15 ,
78
+ "Alloy" : 17 ,
79
+ "Fuel" : 18 ,
80
+ "Copper" : 19 ,
81
+ "Chromium" : 20 ,
82
+ "Nickel" : 16 ,
83
+ "Water" : 21 ,
84
+ } ;
85
+
86
+ // Randomly choose a mineral from the resourceToIdMap
87
+ const minerals = Object . keys ( resourceToIdMap ) ;
88
+ const randomMineral = minerals [ Math . floor ( Math . random ( ) * minerals . length ) ] ;
89
+
90
+ // Get the corresponding id from the map
91
+ const depositRowId = resourceToIdMap [ randomMineral ] ;
92
+
93
+ const response = await supabase . from ( 'basePlanetSectors' ) . upsert ( [
94
+ {
95
+ anomaly : userPlanet ,
96
+ owner : session ?. user ?. id ,
97
+ deposit : depositRowId , // Use the id instead of the resource name
98
+ coverUrl : imageUrl ,
99
+ explored : false ,
100
+ } ,
101
+ ] ) ;
102
+
103
+ if ( response . error ) {
104
+ console . error ( response . error ) ;
105
+ } else {
106
+ // Handle success
107
+ }
108
+ } catch ( error ) {
109
+ console . error ( "Error creating sector:" , error . message ) ;
110
+ }
77
111
}
78
- }
79
112
} ;
80
-
113
+
81
114
return (
82
- < div className = "mt-4" >
83
- < button
84
- onClick = { createSector }
85
- className = "bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
86
- >
87
- Create Sector
88
- </ button >
89
- </ div >
115
+ < div className = "mt-4" >
116
+ < button
117
+ onClick = { createSector }
118
+ className = "bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
119
+ >
120
+ Create Sector
121
+ </ button >
122
+ </ div >
90
123
) ;
91
- } ;
124
+ } ;
92
125
93
126
export function UserOwnedSectorGrid ( ) {
94
127
const supabase = useSupabaseClient ( ) ;
@@ -138,32 +171,6 @@ export function UserOwnedSectorGrid() {
138
171
) ;
139
172
} ;
140
173
141
- const NasaRoverImage = ( { date, rover, onImageMetadataChange, onCreateSector } ) => {
142
- const [ imageUrl , setImageUrl ] = useState ( '' ) ;
143
- const apiKey = 'iT0FQTZKpvadCGPzerqXdO5F4b62arNBOP0dtkXE' ;
144
-
145
- useEffect ( ( ) => {
146
- const apiUrl = `https://api.nasa.gov/mars-photos/api/v1/rovers/${ rover } /photos?sol=${ date } &api_key=${ apiKey } ` ;
147
-
148
- axios . get ( apiUrl )
149
- . then ( ( response ) => {
150
- if ( response . data . photos && response . data . photos . length > 0 ) {
151
- const firstImage = response . data . photos [ 0 ] . img_src ;
152
- setImageUrl ( firstImage ) ;
153
- onImageMetadataChange ( response . data . photos [ 0 ] ) ;
154
- onCreateSector ( firstImage ) ;
155
- } else {
156
- console . error ( 'No images found for the given date & rover:' , date , rover ) ;
157
- }
158
- } )
159
- . catch ( ( error ) => {
160
- console . error ( 'Error fetching image:' , error ) ;
161
- } ) ;
162
- } , [ date , rover , onImageMetadataChange , onCreateSector ] ) ;
163
-
164
- return imageUrl ? < img src = { imageUrl } alt = "Rover image" /> : < p > Loading...</ p > ;
165
- } ;
166
-
167
174
export function AllSectors ( ) {
168
175
const supabase = useSupabaseClient ( ) ;
169
176
const session = useSession ( ) ;
0 commit comments