1
1
import { CardTitle , CardDescription , CardHeader , CardContent , CardFooter , Card } from "../ui/card" ;
2
2
import Link from "next/link" ;
3
3
import { Button } from "../ui/button" ;
4
+ import { useSession , useSupabaseClient } from "@supabase/auth-helpers-react" ;
5
+ import { useEffect , useState } from "react" ;
4
6
5
7
export function MissionList ( ) {
6
- const missions = [
7
- { name : "Pick your home planet" , completed : true } ,
8
+ const supabase = useSupabaseClient ( ) ;
9
+ const session = useSession ( ) ;
10
+ const [ profileData , setProfileData ] = useState < { location : any } | null > ( null ) ; // Set initial state with the correct type
11
+ const [ missions , setMissions ] = useState ( [
12
+ { name : "Pick your home planet" , completed : false } ,
8
13
{ name : "Build your first rover" , completed : false } ,
9
14
{ name : "Collect your first resources" , completed : false } ,
10
15
{ name : "Build your first structure" , completed : false } ,
11
16
{ name : "Make your first classification" , completed : false } ,
12
- ] ;
17
+ ] ) ;
18
+
19
+ useEffect ( ( ) => {
20
+ async function fetchProfileData ( ) {
21
+ try {
22
+ const { data, error } = await supabase
23
+ . from ( "profiles" )
24
+ . select ( "location" )
25
+ . eq ( "id" , session ?. user ?. id )
26
+ . single ( ) ;
27
+
28
+ if ( error ) {
29
+ throw error ;
30
+ } ;
31
+
32
+ if ( data ) {
33
+ setProfileData ( data ) ;
34
+ } ;
35
+ } catch ( error ) {
36
+ console . error ( "Error fetching profile data:" , error . message ) ;
37
+ } ;
38
+ } ;
39
+
40
+ if ( session ) {
41
+ fetchProfileData ( ) ;
42
+ } ;
43
+ } , [ supabase , session ] ) ;
44
+
45
+ useEffect ( ( ) => {
46
+ // Update the first mission's completion status based on profile location
47
+ if ( profileData ) {
48
+ const { location } = profileData ;
49
+ setMissions ( ( prevMissions ) => {
50
+ const updatedMissions = [ ...prevMissions ] ;
51
+ updatedMissions [ 0 ] . completed = [ 1 , 2 , 3 , 4 , 5 , 6 ] . includes ( location ) ;
52
+ return updatedMissions ;
53
+ } ) ;
54
+ } ;
55
+ } , [ profileData ] ) ;
13
56
14
57
return (
15
58
< >
@@ -21,13 +64,11 @@ export function MissionList() {
21
64
{ missions . map ( ( mission , index ) => (
22
65
< div key = { index } className = "flex items-center justify-between" >
23
66
< div className = "flex items-center space-x-3" >
24
- < Link href = "#" >
25
- < LinkIcon
26
- className = { `w-5 h-5 text-gray-500 ${
27
- mission . completed ? "line-through" : ""
28
- } hover:text-gray-900 dark:hover:text-gray-50`}
29
- />
30
- </ Link >
67
+ < LinkIcon
68
+ className = { `w-5 h-5 text-gray-500 ${
69
+ mission . completed ? "line-through" : ""
70
+ } hover:text-gray-900 dark:hover:text-gray-50`}
71
+ />
31
72
< p
32
73
className = { `${
33
74
mission . completed ? "line-through text-gray-500 dark:text-gray-400" : ""
@@ -38,8 +79,8 @@ export function MissionList() {
38
79
</ div >
39
80
</ div >
40
81
) ) }
41
- </ CardContent >
42
- </ >
82
+ </ CardContent >
83
+ </ >
43
84
) ;
44
85
}
45
86
0 commit comments