Skip to content

Commit c654df6

Browse files
committed
🧑🏻‍🏫💦 ↝ [SGV2-20 SGV2-7]: Can now create your first rover, and update your mission list automatically
1 parent 554adc5 commit c654df6

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { useSession, useSupabaseClient } from "@supabase/auth-helpers-react";
2+
import { useEffect, useState } from "react";
3+
4+
export default function BuildFirstRover() {
5+
const supabase = useSupabaseClient();
6+
const session = useSession();
7+
const [roverCreated, setRoverCreated] = useState(false);
8+
const [loading, setLoading] = useState(true);
9+
10+
useEffect(() => {
11+
async function checkRoverStatus() {
12+
try {
13+
if (!session?.user?.id) return;
14+
15+
const { data: userItems, error: userItemsError } = await supabase
16+
.from("inventoryUSERS")
17+
.select("*")
18+
.eq("owner", session.user.id)
19+
.eq("notes", "first rover created by user");
20+
21+
if (userItemsError) {
22+
throw userItemsError;
23+
}
24+
25+
if (userItems && userItems.length > 0) {
26+
setRoverCreated(true);
27+
}
28+
29+
// if (userItems && userItems.length > 0) {
30+
// const roverItem = userItems.find(item => item.itemCategory === "Automaton");
31+
// if (roverItem) {
32+
// setRoverCreated(true);
33+
// }
34+
// }
35+
} catch (error) {
36+
console.error("Error checking rover status:", error.message);
37+
} finally {
38+
setLoading(false);
39+
}
40+
}
41+
42+
checkRoverStatus();
43+
}, [supabase, session]);
44+
45+
async function addRoverToSupa() {
46+
try {
47+
if (roverCreated) {
48+
console.log("You've already created your first rover");
49+
return;
50+
}
51+
52+
const { data, error } = await supabase
53+
.from("inventoryUSERS")
54+
.insert([
55+
{
56+
item: 23, // Assuming 23 is the ID of the rover item in inventoryITEMS table
57+
owner: session?.user?.id,
58+
sector: "18",
59+
planetSector: "18",
60+
notes: "first rover created by user"
61+
}
62+
]);
63+
64+
if (error) {
65+
throw error;
66+
}
67+
68+
console.log("Rover added successfully:", data);
69+
setRoverCreated(true);
70+
} catch (error) {
71+
console.error("Error adding rover:", error.message);
72+
}
73+
}
74+
75+
if (loading) {
76+
return <p>Loading...</p>;
77+
}
78+
79+
return (
80+
<div>
81+
{!roverCreated && (
82+
<button onClick={addRoverToSupa}>Add Rover</button>
83+
)}
84+
{roverCreated && (
85+
<p>You've already created your first rover</p>
86+
)}
87+
</div>
88+
);
89+
}

components/Gameplay/mission-list.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useEffect, useState } from "react";
77
export function MissionList() {
88
const supabase = useSupabaseClient();
99
const session = useSession();
10-
const [profileData, setProfileData] = useState<{ location: any } | null>(null); // Set initial state with the correct type
10+
const [profileData, setProfileData] = useState<{ location: any } | null>(null);
1111
const [missions, setMissions] = useState([
1212
{ name: "Pick your home planet", completed: false },
1313
{ name: "Build your first rover", completed: false },
@@ -43,12 +43,15 @@ export function MissionList() {
4343
}, [supabase, session]);
4444

4545
useEffect(() => {
46-
// Update the first mission's completion status based on profile location
46+
// Update missions based on profile location and rover creation
4747
if (profileData) {
4848
const { location } = profileData;
4949
setMissions((prevMissions) => {
5050
const updatedMissions = [...prevMissions];
51+
// Update mission 1 based on location
5152
updatedMissions[0].completed = [1, 2, 3, 4, 5, 6].includes(location);
53+
// Update mission 2 based on rover creation
54+
updatedMissions[1].completed = prevMissions[1].completed || (location && location !== 0); // Assuming location 0 indicates no rover created
5255
return updatedMissions;
5356
});
5457
};

pages/tests/onboarding.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import BuildFirstRover from "../../components/Gameplay/Automatons/BuildRover";
12
import { PickYourPlanet } from "../../components/Gameplay/Chapter 1/onboarding";
23
import { MissionList } from "../../components/Gameplay/mission-list";
34
import Layout from "../../components/_Core/Section/Layout";
@@ -24,6 +25,7 @@ export default function OnboardingTest() {
2425
`}
2526
</style>
2627
<PickYourPlanet />
28+
<BuildFirstRover />
2729
<MissionList />
2830
</>
2931
);

0 commit comments

Comments
 (0)