Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 45 additions & 22 deletions src/TennisMatchApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import {
rejectInvite,
} from "./services/invites";
import { login, signup, forgotPassword, getPersonalDetails } from "./services/auth";
import { updatePlayerPersonalDetails } from "./services/player";
import {
updatePlayerPersonalDetails,
normalizeRatingFromApi,
} from "./services/player";
import {
Calendar,
MapPin,
Expand Down Expand Up @@ -491,12 +494,15 @@ const resolveAuthSession = (data = {}, fallback = {}) => {
);

const derivedSkill = pickString(
profile?.ntrp_rating,
profile?.usta_rating,
profile?.skill_level,
profile?.skillLevel,
userFromApi?.ntrp_rating,
userFromApi?.usta_rating,
userFromApi?.skill_level,
userFromApi?.skillLevel,
fallbackData?.ntrp_rating,
fallbackData?.skillLevel,
);

Expand Down Expand Up @@ -730,31 +736,48 @@ const TennisMatchApp = () => {
return "";
};

const normalizedProfileDetails =
profileDetails && typeof profileDetails === "object"
? {
...profileDetails,
...(profileDetails.ntrp_rating !== undefined
? { ntrp_rating: normalizeRatingFromApi(profileDetails.ntrp_rating) }
: {}),
...(profileDetails.usta_rating !== undefined
? { usta_rating: normalizeRatingFromApi(profileDetails.usta_rating) }
: {}),
...(profileDetails.uta_rating !== undefined
? { uta_rating: normalizeRatingFromApi(profileDetails.uta_rating) }
: {}),
}
: profileDetails;

setCurrentUser((prev) => {
if (!prev || typeof prev !== "object") return prev;

const mergedProfile = {
...(prev.profile && typeof prev.profile === "object" ? prev.profile : {}),
...profileDetails,
...normalizedProfileDetails,
};

const derivedName = pickFirstValue(
profileDetails.full_name,
profileDetails.fullName,
profileDetails.name,
normalizedProfileDetails.full_name,
normalizedProfileDetails.fullName,
normalizedProfileDetails.name,
);

const derivedSkill = pickFirstValue(
profileDetails.usta_rating,
profileDetails.ustaRating,
profileDetails.skill_level,
profileDetails.skillLevel,
normalizedProfileDetails.ntrp_rating,
normalizedProfileDetails.usta_rating,
normalizedProfileDetails.ustaRating,
normalizedProfileDetails.skill_level,
normalizedProfileDetails.skillLevel,
);

const derivedAvatarUrl = getAvatarUrlFromPlayer({
profile: profileDetails,
player: profileDetails,
user: profileDetails,
profile: normalizedProfileDetails,
player: normalizedProfileDetails,
user: normalizedProfileDetails,
});

const nextUser = {
Expand All @@ -775,16 +798,16 @@ const TennisMatchApp = () => {
}

const profilePicture = pickFirstValue(
profileDetails.profile_picture,
profileDetails.profilePicture,
profileDetails.profile_picture_url,
profileDetails.profilePictureUrl,
profileDetails.photo_url,
profileDetails.photoUrl,
profileDetails.image_url,
profileDetails.imageUrl,
profileDetails.avatar_url,
profileDetails.avatarUrl,
normalizedProfileDetails.profile_picture,
normalizedProfileDetails.profilePicture,
normalizedProfileDetails.profile_picture_url,
normalizedProfileDetails.profilePictureUrl,
normalizedProfileDetails.photo_url,
normalizedProfileDetails.photoUrl,
normalizedProfileDetails.image_url,
normalizedProfileDetails.imageUrl,
normalizedProfileDetails.avatar_url,
normalizedProfileDetails.avatarUrl,
);

if (profilePicture) {
Expand Down
112 changes: 95 additions & 17 deletions src/components/ProfileManager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { formatPhoneNumber, formatPhoneDisplay } from "../services/phone";
import ProfilePhotoUploader from "./ProfilePhotoUploader";
import {
updatePlayerPersonalDetails,
getPlayerMatchProfile,
updatePlayerMatchProfile,
normalizeRatingForApi,
normalizeRatingFromApi,
getMatchProfileId,
} from "../services/player";

const USTA_RATING_OPTIONS = [
Expand All @@ -27,6 +31,7 @@ const USTA_RATING_OPTIONS = [

const emptyDetails = {
id: null,
match_profile_id: null,
full_name: "",
phone: "",
profile_picture: "",
Expand Down Expand Up @@ -63,30 +68,66 @@ const ProfileManager = ({ isOpen, onClose, onProfileUpdate }) => {
if (showLoader) {
setLoading(true);
}
const data = await getPersonalDetails();
const [personalResult, matchProfileResult] = await Promise.allSettled([
getPersonalDetails(),
getPlayerMatchProfile({ player: accessToken }),
]);

if (personalResult.status !== "fulfilled") {
throw personalResult.reason;
}

const data = personalResult.value || {};
if (matchProfileResult.status === "rejected") {
const reason = matchProfileResult.reason;
const status = Number(reason?.status ?? reason?.response?.status);
if (!status || status >= 400) {
if (status !== 404) {
console.warn("Failed to load match profile", reason);
}
}
}
const matchProfile =
matchProfileResult.status === "fulfilled" ? matchProfileResult.value : null;
const matchProfileId = getMatchProfileId(matchProfile);
const hasNtrpRating =
matchProfile && Object.prototype.hasOwnProperty.call(matchProfile, "ntrp_rating");
const hasUstaRating =
matchProfile && Object.prototype.hasOwnProperty.call(matchProfile, "usta_rating");
const matchProfileRating = hasNtrpRating
? matchProfile.ntrp_rating
: hasUstaRating
? matchProfile.usta_rating
: undefined;

const normalizedDetails = {
id: data?.id ?? null,
match_profile_id: matchProfileId,
full_name: data?.full_name || "",
phone: data?.phone ? String(data.phone).replace(/\D/g, "") : "",
profile_picture: data?.profile_picture || "",
date_of_birth: data?.date_of_birth
? data.date_of_birth.split("T")[0]
: "",
usta_rating:
typeof data?.usta_rating === "number" && !Number.isNaN(data.usta_rating)
? String(data.usta_rating)
: data?.usta_rating || "",
uta_rating:
typeof data?.uta_rating === "number" && !Number.isNaN(data.uta_rating)
? String(data.uta_rating)
: data?.uta_rating || "",
usta_rating: normalizeRatingFromApi(
matchProfileRating ?? data?.usta_rating,
),
uta_rating: normalizeRatingFromApi(data?.uta_rating),
about_me: data?.about_me || "",
};
setDetails(normalizedDetails);
setPhoneInput(formatPhoneDisplay(data?.phone) || "");
setImagePreview(normalizedDetails.profile_picture || "");
if (onProfileUpdate) {
onProfileUpdate({ ...data });
onProfileUpdate({
...data,
...(matchProfileId !== undefined && matchProfileId !== null
? { match_profile_id: matchProfileId }
: {}),
...(matchProfileRating !== undefined
? { ntrp_rating: matchProfileRating, usta_rating: matchProfileRating }
: {}),
});
}
} catch (err) {
console.error(err);
Expand Down Expand Up @@ -153,23 +194,60 @@ const ProfileManager = ({ isOpen, onClose, onProfileUpdate }) => {
about_me: aboutMe || null,
};

if (normalizedUstaRating !== undefined) {
payload.usta_rating = normalizedUstaRating;
}

if (normalizedUtaRating !== undefined) {
payload.uta_rating = normalizedUtaRating;
}

const matchProfileNeedsUpdate =
normalizedUstaRating !== undefined ||
(!hasValue(details.usta_rating) &&
details.match_profile_id !== undefined &&
details.match_profile_id !== null);

let matchProfileResponse = null;

if (matchProfileNeedsUpdate) {
matchProfileResponse = await updatePlayerMatchProfile({
player: accessToken,
id: details.match_profile_id,
ntrp_rating:
normalizedUstaRating !== undefined ? normalizedUstaRating : null,
});
}

await updatePlayerPersonalDetails(payload);

const nextMatchProfileId =
getMatchProfileId(matchProfileResponse) ?? details.match_profile_id;
const nextMatchProfileRating =
matchProfileResponse?.ntrp_rating ??
matchProfileResponse?.usta_rating ??
(matchProfileNeedsUpdate
? normalizedUstaRating ?? null
: undefined);
const nextMatchProfileRatingDisplay =
nextMatchProfileRating !== undefined
? normalizeRatingFromApi(nextMatchProfileRating)
: undefined;
const nextUstaRatingDisplay =
normalizedUstaRating !== undefined
? normalizeRatingFromApi(normalizedUstaRating)
: nextMatchProfileRatingDisplay;

if (onProfileUpdate) {
onProfileUpdate({
...details,
...(normalizedUstaRating !== undefined
? { usta_rating: normalizedUstaRating }
match_profile_id: nextMatchProfileId,
...(nextMatchProfileRating !== undefined
? {
ntrp_rating: nextMatchProfileRating,
}
: {}),
...(nextUstaRatingDisplay !== undefined
? { usta_rating: nextUstaRatingDisplay }
: {}),
...(normalizedUtaRating !== undefined
? { uta_rating: normalizedUtaRating }
? { uta_rating: normalizeRatingFromApi(normalizedUtaRating) }
: {}),
});
}
Expand Down
Loading