standardize mobile api transport for gyms and core services
This commit is contained in:
1 parent
c36cad9c54
commit
80110acbf7
8 files changed
+322
-386
No files matched your search
@@ -18,8 +18,8 @@ import { ListItem } from "../../components/ListItem";
|
||||
import { MinimalButton } from "../../components/MinimalButton";
|
||||
import { Badge } from "../../components/Badge";
|
||||
import { IconContainer } from "../../components/IconContainer";
|
||||
import { API_BASE_URL, API_ENDPOINTS } from "../../config/api";
|
||||
import { fitnessProfileApi, FitnessProfile } from "../../api/fitnessProfile";
|
||||
import { gymsApi, type Gym } from "../../api/gyms";
|
||||
import log from "../../utils/logger";
|
||||
|
||||
export default function ProfileScreen() {
|
||||
@@ -29,9 +29,7 @@ export default function ProfileScreen() {
|
||||
const { colors, typography, theme: activeTheme, setTheme } = useTheme();
|
||||
const { getToken } = useAuth();
|
||||
|
||||
const [gyms, setGyms] = useState<
|
||||
Array<{ id: string; name: string; location?: string }>
|
||||
>([]);
|
||||
const [gyms, setGyms] = useState<Gym[]>([]);
|
||||
const [gymsLoading, setGymsLoading] = useState(false);
|
||||
const [selectedGymId, setSelectedGymId] = useState<string | null>(null);
|
||||
const [currentGymId, setCurrentGymId] = useState<string | null>(null);
|
||||
@@ -76,51 +74,14 @@ export default function ProfileScreen() {
|
||||
try {
|
||||
setGymsLoading(true);
|
||||
const token = await getToken();
|
||||
const url = `${API_BASE_URL}${API_ENDPOINTS.GYMS}`;
|
||||
log.debug("Loading gyms", { url });
|
||||
const res = await fetch(url, {
|
||||
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
||||
});
|
||||
const contentType = res.headers.get("content-type") || "";
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => "");
|
||||
log.error(
|
||||
"Failed to fetch gyms - non-OK response",
|
||||
new Error(text.slice(0, 200)),
|
||||
{ status: res.status },
|
||||
);
|
||||
setGyms([]);
|
||||
return;
|
||||
}
|
||||
if (!contentType.includes("application/json")) {
|
||||
const text = await res.text().catch(() => "");
|
||||
log.error(
|
||||
"Failed to fetch gyms - expected JSON",
|
||||
new Error(text.slice(0, 200)),
|
||||
{ contentType },
|
||||
);
|
||||
setGyms([]);
|
||||
return;
|
||||
}
|
||||
let data: any = null;
|
||||
try {
|
||||
data = await res.json();
|
||||
} catch (e) {
|
||||
const text = await res.text().catch(() => "");
|
||||
log.error("Failed to parse gyms JSON", e, {
|
||||
bodyPreview: text?.slice(0, 200),
|
||||
});
|
||||
setGyms([]);
|
||||
return;
|
||||
}
|
||||
const list = Array.isArray(data) ? data : [];
|
||||
const list = await gymsApi.getGyms(token);
|
||||
setGyms(list);
|
||||
const gid =
|
||||
currentGymId ??
|
||||
((user?.publicMetadata as any)?.gymId as string | undefined) ??
|
||||
null;
|
||||
if (gid) {
|
||||
const g = list.find((x: any) => x.id === gid);
|
||||
const g = list.find((x) => x.id === gid);
|
||||
setCurrentGymId(gid);
|
||||
setCurrentGymName(g?.name ?? null);
|
||||
if (selectedGymId === null) setSelectedGymId(gid);
|
||||
@@ -136,42 +97,7 @@ export default function ProfileScreen() {
|
||||
const handleApplyGym = async () => {
|
||||
try {
|
||||
const token = await getToken();
|
||||
const url = `${API_BASE_URL}${API_ENDPOINTS.USERS.GYM}`;
|
||||
log.debug("Updating gym selection", {
|
||||
url,
|
||||
gymId: selectedGymId,
|
||||
token: token ? "present" : "missing",
|
||||
});
|
||||
const res = await fetch(url, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
},
|
||||
body: JSON.stringify({ gymId: selectedGymId }),
|
||||
});
|
||||
const contentType = res.headers.get("content-type") || "";
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => "");
|
||||
log.error(
|
||||
"Failed to update gym selection - non-OK response",
|
||||
new Error(text.slice(0, 200)),
|
||||
{ status: res.status },
|
||||
);
|
||||
Alert.alert("Error", "Failed to update gym selection");
|
||||
return;
|
||||
}
|
||||
if (contentType.includes("application/json")) {
|
||||
try {
|
||||
const data = await res.json();
|
||||
log.debug("Gym selection updated", { data });
|
||||
} catch (e) {
|
||||
const text = await res.text().catch(() => "");
|
||||
log.error("Failed to parse update response JSON", e, {
|
||||
bodyPreview: text?.slice(0, 200),
|
||||
});
|
||||
}
|
||||
}
|
||||
await gymsApi.updateUserGym(selectedGymId, token);
|
||||
setCurrentGymId(selectedGymId);
|
||||
setCurrentGymName(
|
||||
selectedGymId
|
||||
|
||||
Reference in new issue
Block a user