Skip to content

Commit

Permalink
Some work towards friend infra (#30)
Browse files Browse the repository at this point in the history
* Some work towards friend infra

* withCredentials: true for relationships api

* pass profileUserId thru to card

* done
  • Loading branch information
cmyui authored Jul 23, 2024
1 parent 18f4b9d commit 8f768ee
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 19 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ REACT_APP_LEADERBOARD_API_BASE_URL=https://akatsuki.gg/api
REACT_APP_USER_API_BASE_URL=https://akatsuki.gg/api
REACT_APP_SCORES_API_BASE_URL=https://akatsuki.gg/api
REACT_APP_PROFILE_HISTORY_API_BASE_URL=https://akatsuki.gg/api
REACT_APP_USER_RELATIONSHIPS_API_BASE_URL=https://akatsuki.gg/api
REACT_APP_BANCHO_API_BASE_URL=https://c.akatsuki.gg/api
REACT_APP_ADMIN_PANEL_HOME_URL=https://old.akatsuki.gg
REACT_APP_GITHUB_ORG_URL=https://github.com/osuAkatsuki
Expand Down
44 changes: 44 additions & 0 deletions src/adapters/akatsuki-api/userRelationships.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import axios from "axios"

interface UserFriendsWithRequest {
id: number
}

export const enum RelationshipType {
NotFriend = "not_friend",
Friend = "friend",
Mutual = "mutual",
}

export interface UserFriendsWithResponse {
code: number
friend: boolean
mutual: boolean
}

const userRelationshipsApiInstance = axios.create({
baseURL: process.env.REACT_APP_USER_RELATIONSHIPS_API_BASE_URL,
withCredentials: true,
})

export const fetchUserFriendsWith = async (
request: UserFriendsWithRequest
): Promise<UserFriendsWithResponse> => {
try {
const response = await userRelationshipsApiInstance.get(
"/v1/friends/with",
{
params: {
id: request.id,
},
}
)
return {
code: response.status,
friend: response.data.friend,
mutual: response.data.mutual,
}
} catch (e: any) {
throw new Error("Failed to fetch user friends data from server")
}
}
2 changes: 1 addition & 1 deletion src/components/images/icons/AddUserIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const AddUserIcon = () => {
>
<path
d="M15.8583 3.528C15.8583 3.29408 15.9512 3.06974 16.1166 2.90433C16.2821 2.73893 16.5064 2.646 16.7403 2.646H18.5043V0.88201C18.5043 0.648089 18.5972 0.42375 18.7626 0.258343C18.928 0.0929369 19.1524 1.25328e-05 19.3863 1.25328e-05C19.6202 1.25328e-05 19.8446 0.0929369 20.01 0.258343C20.1754 0.42375 20.2683 0.648089 20.2683 0.88201V2.646H22.0323C22.2662 2.646 22.4906 2.73893 22.656 2.90433C22.8214 3.06974 22.9143 3.29408 22.9143 3.528C22.9143 3.76192 22.8214 3.98626 22.656 4.15167C22.4906 4.31707 22.2662 4.41 22.0323 4.41H20.2683V6.17399C20.2683 6.40791 20.1754 6.63225 20.01 6.79766C19.8446 6.96306 19.6202 7.05599 19.3863 7.05599C19.1524 7.05599 18.928 6.96306 18.7626 6.79766C18.5972 6.63225 18.5043 6.40791 18.5043 6.17399V4.41H16.7403C16.5064 4.41 16.2821 4.31707 16.1166 4.15167C15.9512 3.98626 15.8583 3.76192 15.8583 3.528ZM22.7555 9.55645C23.1585 11.9517 22.7898 14.413 21.7027 16.585C20.6156 18.7571 18.8663 20.5274 16.7075 21.6405C14.5486 22.7535 12.0919 23.1517 9.69199 22.7775C7.2921 22.4033 5.07322 21.2761 3.35573 19.5586C1.63824 17.8411 0.511017 15.6222 0.136805 13.2223C-0.237406 10.8224 0.160761 8.36569 1.27382 6.20684C2.38688 4.04799 4.15723 2.29873 6.32926 1.21163C8.50129 0.124523 10.9626 -0.244174 13.3579 0.158772C13.5868 0.199261 13.7905 0.328515 13.9247 0.518418C14.0588 0.708321 14.1125 0.943512 14.0742 1.17283C14.0358 1.40215 13.9084 1.60704 13.7198 1.74294C13.5311 1.87884 13.2965 1.93475 13.0668 1.89851C11.6756 1.66448 10.2501 1.73642 8.88953 2.10932C7.52895 2.48221 6.26597 3.14712 5.18849 4.05775C4.11101 4.96838 3.24491 6.10287 2.65045 7.38227C2.056 8.66166 1.74748 10.0552 1.74637 11.466C1.74428 13.841 2.61717 16.1335 4.19832 17.9057C5.182 16.4802 6.56518 15.3774 8.17392 14.736C7.30977 14.0553 6.67919 13.1223 6.36987 12.0667C6.06056 11.0111 6.08789 9.88532 6.44806 8.84594C6.80823 7.80657 7.48333 6.90526 8.37949 6.26736C9.27565 5.62946 10.3483 5.28668 11.4483 5.28668C12.5483 5.28668 13.621 5.62946 14.5172 6.26736C15.4133 6.90526 16.0884 7.80657 16.4486 8.84594C16.8088 9.88532 16.8361 11.0111 16.5268 12.0667C16.2175 13.1223 15.5869 14.0553 14.7227 14.736C16.3315 15.3774 17.7147 16.4802 18.6983 17.9057C20.2795 16.1335 21.1524 13.841 21.1503 11.466C21.1503 10.9237 21.1054 10.3823 21.0158 9.84751C20.9955 9.73278 20.9981 9.61516 21.0236 9.50145C21.049 9.38775 21.0967 9.28021 21.1639 9.18504C21.2311 9.08988 21.3166 9.00897 21.4152 8.94701C21.5139 8.88504 21.6239 8.84323 21.7388 8.82401C21.8537 8.80478 21.9713 8.80852 22.0847 8.83499C22.1982 8.86147 22.3053 8.91017 22.3998 8.97827C22.4944 9.04637 22.5745 9.13253 22.6355 9.23177C22.6966 9.33101 22.7374 9.44136 22.7555 9.55645ZM11.4483 14.112C12.1461 14.112 12.8282 13.9051 13.4084 13.5174C13.9886 13.1297 14.4407 12.5787 14.7078 11.9341C14.9748 11.2894 15.0447 10.5801 14.9085 9.8957C14.7724 9.21134 14.4364 8.58271 13.943 8.08931C13.4496 7.59591 12.821 7.25991 12.1366 7.12378C11.4522 6.98765 10.7429 7.05752 10.0982 7.32454C9.45358 7.59157 8.90258 8.04376 8.51492 8.62393C8.12726 9.20411 7.92035 9.88621 7.92035 10.584C7.92035 11.5197 8.29204 12.417 8.95367 13.0786C9.61529 13.7403 10.5127 14.112 11.4483 14.112ZM11.4483 21.1679C13.6019 21.1701 15.6944 20.4522 17.393 19.1283C16.7549 18.1304 15.876 17.3092 14.8371 16.7404C13.7982 16.1715 12.6328 15.8734 11.4483 15.8734C10.2639 15.8734 9.09851 16.1715 8.05961 16.7404C7.02071 17.3092 6.14173 18.1304 5.50367 19.1283C7.20228 20.4522 9.29476 21.1701 11.4483 21.1679Z"
fill="white"
fill="currentcolor"
/>
</svg>
)
Expand Down
75 changes: 58 additions & 17 deletions src/components/profile/ProfileRelationshipCard.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,71 @@
import { Typography } from "@mui/material"
import { Button, Typography } from "@mui/material"
import Stack from "@mui/material/Stack"
import Box from "@mui/material/Box"
import { AddUserIcon } from "../images/icons/AddUserIcon"
import { RelationshipType } from "../../adapters/akatsuki-api/userRelationships"
import { useIdentityContext } from "../../context/identity"

const getRelationshipColor = (relationship: RelationshipType) => {
switch (relationship) {
case RelationshipType.Friend:
return "#00FF00"
case RelationshipType.Mutual:
return "#CF6179"
case RelationshipType.NotFriend:
return "rgba(18, 15, 29, 1)"
}
}

export const ProfileRelationshipCard = ({
profileUserId,
relationship,
setRelationship,
followers,
}: {
profileUserId: number
relationship: RelationshipType
setRelationship: (relationship: RelationshipType) => void
followers: number
}) => {
const { identity } = useIdentityContext()

const onClick = () => {
if (profileUserId === identity?.userId) {
return
}

if (relationship === RelationshipType.NotFriend) {
// Add them as a friend over the API
// Conditionally set mutual vs. friend
} else {
// Remove them as a friend over the API
setRelationship(RelationshipType.NotFriend)
}
}

return (
<Stack
direction="row"
spacing={1.5}
bgcolor="rgba(18, 15, 29, 1)"
borderRadius={11}
py={0.5}
px={1.5}
alignItems="center"
>
{/* TODO: color icon based on relationship status */}
{/* https://www.figma.com/design/moJEAJT6UYGnwQYIuKzanf?node-id=76-1050#878804687 */}
<Box borderRadius={11} overflow="hidden">
<Button
onClick={() => onClick()}
sx={{ textDecoration: "none", color: "white", padding: 0 }}
>
<Stack
direction="row"
spacing={1.5}
bgcolor={getRelationshipColor(relationship)} //"rgba(18, 15, 29, 1)"
py={0.5}
px={1.5}
alignItems="center"
>
{/* TODO: color icon based on relationship status */}
{/* https://www.figma.com/design/moJEAJT6UYGnwQYIuKzanf?node-id=76-1050#878804687 */}

<Box width={23} height={23}>
<AddUserIcon />
</Box>
<Typography variant="h6">{followers}</Typography>
</Stack>
<Box width={23} height={23}>
<AddUserIcon />
</Box>
<Typography variant="h6">{followers}</Typography>
</Stack>
</Button>
</Box>
)
}
33 changes: 32 additions & 1 deletion src/pages/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import { ProfileClanCard } from "../components/profile/ProfileClanCard"
import { ProfileUserpageCard } from "../components/profile/ProfileUserpageCard"
import { ProfileHistoryCard } from "../components/profile/ProfileHistoryCard"
import { ProfileScoresCard } from "../components/profile/ProfileScoresCard"
import {
fetchUserFriendsWith,
RelationshipType,
} from "../adapters/akatsuki-api/userRelationships"

export const ProfilePage = () => {
const queryParams = useParams()
Expand All @@ -40,6 +44,8 @@ export const ProfilePage = () => {
const [userProfile, setUserProfile] = useState<UserFullResponse | null>(null)
const [profileHistoryType, setProfileHistoryType] =
useState<ProfileHistoryType>(ProfileHistoryType.GlobalRank)
const [relationship, setRelationship] = useState(RelationshipType.NotFriend)

// const [isOnline, setIsOnline] = useState(false)

const [gameMode, setGameMode] = useState(GameMode.Standard)
Expand All @@ -60,6 +66,26 @@ export const ProfilePage = () => {
})()
}, [profileUserId])

useEffect(() => {
;(async () => {
if (!profileUserId) return

try {
const response = await fetchUserFriendsWith({ id: profileUserId })
if (response.friend) {
setRelationship(RelationshipType.Friend)
} else if (response.mutual) {
setRelationship(RelationshipType.Mutual)
} else {
setRelationship(RelationshipType.NotFriend)
}
} catch (e: any) {
setError("Failed to fetch user relationship data from server")
return
}
})()
}, [profileUserId])

// useEffect(() => {
// ;(async () => {
// const response = await userIsOnline({ userId: profileUserId })
Expand Down Expand Up @@ -115,7 +141,12 @@ export const ProfilePage = () => {
spacing={{ xs: 1, sm: 0 }}
>
<ProfileActivityDatesCard userProfile={userProfile} />
<ProfileRelationshipCard followers={userProfile.followers} />
<ProfileRelationshipCard
profileUserId={profileUserId}
relationship={relationship}
setRelationship={setRelationship}
followers={userProfile.followers}
/>
</Stack>
</Stack>
</Container>
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare namespace NodeJS {
readonly REACT_APP_SCORES_API_BASE_URL: string
readonly REACT_APP_PROFILE_HISTORY_API_BASE_URL: string
readonly REACT_APP_BANCHO_API_BASE_URL: string
readonly REACT_APP_USER_RELATIONSHIPS_API_BASE_URL: string
readonly REACT_APP_ADMIN_PANEL_HOME_URL: string
readonly REACT_APP_GITHUB_ORG_URL: string
readonly REACT_APP_TWITTER_URL: string
Expand Down

0 comments on commit 8f768ee

Please sign in to comment.