gym selection via profile screen
This commit is contained in:
parent
339d798a88
commit
02879cefd1
Binary file not shown.
@ -63,7 +63,7 @@ export async function PATCH(req: Request) {
|
|||||||
gymId,
|
gymId,
|
||||||
});
|
});
|
||||||
await db.run(
|
await db.run(
|
||||||
sql`UPDATE users SET gym_id = ${gymId ?? null}, updated_at = ${new Date()} WHERE id = ${userId}`,
|
sql`UPDATE users SET gym_id = ${gymId ?? null}, updated_at = ${Date.now()} WHERE id = ${userId}`,
|
||||||
);
|
);
|
||||||
console.log("PATCH /api/users/gym update completed");
|
console.log("PATCH /api/users/gym update completed");
|
||||||
|
|
||||||
|
|||||||
@ -15,6 +15,8 @@ import { LinearGradient } from "expo-linear-gradient";
|
|||||||
import { theme } from "../../styles/theme";
|
import { theme } from "../../styles/theme";
|
||||||
import { AnimatedButton } from "../../components/AnimatedButton";
|
import { AnimatedButton } from "../../components/AnimatedButton";
|
||||||
import { GradientBackground } from "../../components/GradientBackground";
|
import { GradientBackground } from "../../components/GradientBackground";
|
||||||
|
import { useState } from "react";
|
||||||
|
import { API_BASE_URL, API_ENDPOINTS } from "../../config/api";
|
||||||
|
|
||||||
export default function ProfileScreen() {
|
export default function ProfileScreen() {
|
||||||
const { user } = useUser();
|
const { user } = useUser();
|
||||||
@ -48,13 +50,48 @@ export default function ProfileScreen() {
|
|||||||
try {
|
try {
|
||||||
setGymsLoading(true);
|
setGymsLoading(true);
|
||||||
const token = await getToken();
|
const token = await getToken();
|
||||||
const res = await fetch("/api/gyms", {
|
const url = `${API_BASE_URL}${API_ENDPOINTS.GYMS}`;
|
||||||
|
console.log("Profile.loadGyms fetching:", url);
|
||||||
|
const res = await fetch(url, {
|
||||||
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
||||||
});
|
});
|
||||||
const data = await res.json();
|
const contentType = res.headers.get("content-type") || "";
|
||||||
|
if (!res.ok) {
|
||||||
|
const text = await res.text().catch(() => "");
|
||||||
|
console.error("Failed to fetch gyms: non-OK response", {
|
||||||
|
status: res.status,
|
||||||
|
body: text?.slice(0, 200),
|
||||||
|
});
|
||||||
|
setGyms([]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!contentType.includes("application/json")) {
|
||||||
|
const text = await res.text().catch(() => "");
|
||||||
|
console.error("Failed to fetch gyms: expected JSON", {
|
||||||
|
contentType,
|
||||||
|
body: text?.slice(0, 200),
|
||||||
|
});
|
||||||
|
setGyms([]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let data: any = null;
|
||||||
|
try {
|
||||||
|
data = await res.json();
|
||||||
|
} catch (e) {
|
||||||
|
const text = await res.text().catch(() => "");
|
||||||
|
console.error(
|
||||||
|
"Failed to parse gyms JSON:",
|
||||||
|
e,
|
||||||
|
"body:",
|
||||||
|
text?.slice(0, 200),
|
||||||
|
);
|
||||||
|
setGyms([]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
setGyms(Array.isArray(data) ? data : []);
|
setGyms(Array.isArray(data) ? data : []);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Failed to fetch gyms:", err);
|
console.error("Failed to fetch gyms:", err);
|
||||||
|
setGyms([]);
|
||||||
} finally {
|
} finally {
|
||||||
setGymsLoading(false);
|
setGymsLoading(false);
|
||||||
}
|
}
|
||||||
@ -63,7 +100,14 @@ export default function ProfileScreen() {
|
|||||||
const handleApplyGym = async () => {
|
const handleApplyGym = async () => {
|
||||||
try {
|
try {
|
||||||
const token = await getToken();
|
const token = await getToken();
|
||||||
await fetch("/api/users/gym", {
|
const url = `${API_BASE_URL}${API_ENDPOINTS.USERS}/gym`;
|
||||||
|
console.log(
|
||||||
|
"Profile.handleApplyGym PATCH:",
|
||||||
|
url,
|
||||||
|
"gymId:",
|
||||||
|
selectedGymId,
|
||||||
|
);
|
||||||
|
const res = await fetch(url, {
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@ -71,6 +115,30 @@ export default function ProfileScreen() {
|
|||||||
},
|
},
|
||||||
body: JSON.stringify({ gymId: selectedGymId }),
|
body: JSON.stringify({ gymId: selectedGymId }),
|
||||||
});
|
});
|
||||||
|
const contentType = res.headers.get("content-type") || "";
|
||||||
|
if (!res.ok) {
|
||||||
|
const text = await res.text().catch(() => "");
|
||||||
|
console.error("Failed to update gym selection: non-OK response", {
|
||||||
|
status: res.status,
|
||||||
|
body: text?.slice(0, 200),
|
||||||
|
});
|
||||||
|
Alert.alert("Error", "Failed to update gym selection");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (contentType.includes("application/json")) {
|
||||||
|
try {
|
||||||
|
const data = await res.json();
|
||||||
|
console.log("Gym selection updated:", data);
|
||||||
|
} catch (e) {
|
||||||
|
const text = await res.text().catch(() => "");
|
||||||
|
console.error(
|
||||||
|
"Failed to parse update response JSON:",
|
||||||
|
e,
|
||||||
|
"body:",
|
||||||
|
text?.slice(0, 200),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Alert.alert(
|
Alert.alert(
|
||||||
"Success",
|
"Success",
|
||||||
selectedGymId ? "Gym selected successfully" : "Proceeding without gym",
|
selectedGymId ? "Gym selected successfully" : "Proceeding without gym",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user