cozynet/src/app/axios/useFetchApi.tsx
2024-03-05 12:43:09 +07:00

945 lines
24 KiB
TypeScript

/* eslint-disable react-hooks/exhaustive-deps */
import { Dispatch, SetStateAction, useEffect, useState } from "react"
import axios from "axios"
import { ApiFetch } from "@/lib/api"
type AvatarItemList = {
id: number
user: number
itemId: number
type: number
itemKind: number
stock: number
isValid: number
}
interface UseAvatarItemListParams {
setAvatarItemList: Dispatch<SetStateAction<AvatarItemList[]>>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
}
axios.defaults.withCredentials = true;
export const UseAvatarItemList = ({
setAvatarItemList,
setLoading,
setError,
}: UseAvatarItemListParams) => {
useEffect(() => {
const source = axios.CancelToken.source()
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/items`;
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setAvatarItemList(response.data)
setLoading(false)
})
.catch((err) => {
if (axios.isCancel(err)) {
} else {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [setAvatarItemList, setLoading, setError])
}
type bestTop = {
maxCombo: number
isAllJustice: number
isFullCombo: number
userPlayDate: string
score: number
isNewRecord: number
judgeHeaven: number
judgeGuilty: number
judgeJustice: number
judgeAttack: number
judgeCritical: number
isClear: number
skillId: number
chartId: number
title: string
level: number
genre: string
jacketPath: string
artist: string
score_change: string
rating_change: string
rating: number
}
interface UseBestTopScoresParams {
setBestTop: Dispatch<SetStateAction<bestTop[]>>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
}
export const useBestTop = ({
setBestTop,
setLoading,
setError,
}: UseBestTopScoresParams) => {
useEffect(() => {
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/chuni-best-and-top`
const source = axios.CancelToken.source()
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setBestTop(response.data)
setLoading(false)
})
.catch((err) => {
if (!axios.isCancel(err)) {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [setBestTop, setLoading, setError])
}
type playerRecentRatingTable = {
score: number
chartId: number
title: string
level: number
genre: string
jacketPath: string
artist: string
rating: number
}
interface UsePlayerRecentRatingTableParams {
setPlayerRecentRatingTable: Dispatch<
SetStateAction<playerRecentRatingTable[]>
>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
}
export const usePlayerRecentRatingTable = ({
setPlayerRecentRatingTable,
setLoading,
setError,
}: UsePlayerRecentRatingTableParams) => {
useEffect(() => {
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/chuni-recent`
const source = axios.CancelToken.source()
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayerRecentRatingTable(response.data)
setLoading(false)
})
.catch((err) => {
if (!axios.isCancel(err)) {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [setPlayerRecentRatingTable, setLoading, setError])
}
type playerAvatarSelection = {
avatarBack: string
avatarItem: string
avatarWear: string
avatarFront: string
avatarSkin: string
avatarHead: string
avatar_skinfoot_l: string
avatar_skinfoot_r: string
avatarFace: string
}
interface UseCharacterDataParams {
setPlayerInfo: Dispatch<SetStateAction<playerAvatarSelection[]>>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
}
export const UsePlayerInfo = ({
setPlayerInfo,
setLoading,
setError,
}: UseCharacterDataParams) => {
useEffect(() => {
const source = axios.CancelToken.source()
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/profile-data`
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayerInfo(response.data)
setLoading(false)
})
.catch((err) => {
if (axios.isCancel(err)) {
// Handle cancellation if needed
} else {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [setPlayerInfo, setLoading, setError])
}
type playerVoiceIdSelection = {
voiceId: string
}
interface UsePlayerVoiceIdParams {
setPlayerVoiceId: Dispatch<SetStateAction<playerVoiceIdSelection[]>>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
}
export const UsePlayerVoiceId = ({
setPlayerVoiceId,
setLoading,
setError,
}: UsePlayerVoiceIdParams) => {
useEffect(() => {
const source = axios.CancelToken.source()
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/profile-data`
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayerVoiceId(response.data)
setLoading(false)
})
.catch((err) => {
if (axios.isCancel(err)) {
// Handle cancellation if needed
} else {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [setPlayerVoiceId, setLoading, setError])
}
type playerMapIconSelection = {
mapIconId: string
}
interface UsePlayerMapIconParams {
setPlayerMapIcon: Dispatch<SetStateAction<playerMapIconSelection[]>>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
}
export const UsePlayerMapIcon = ({
setPlayerMapIcon,
setLoading,
setError,
}: UsePlayerMapIconParams) => {
useEffect(() => {
const source = axios.CancelToken.source()
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/profile-data`
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayerMapIcon(response.data)
setLoading(false)
})
.catch((err) => {
if (axios.isCancel(err)) {
// Handle cancellation if needed
} else {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [setPlayerMapIcon, setLoading, setError])
}
type playerNamePlateIconSelection = {
nameplateId: string
}
interface UsePlayerNameplateParams {
setPlayerNameplate: Dispatch<SetStateAction<playerNamePlateIconSelection[]>>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
}
export const UsePlayerNamePlates = ({
setPlayerNameplate,
setLoading,
setError,
}: UsePlayerNameplateParams) => {
useEffect(() => {
const source = axios.CancelToken.source()
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/profile-data`
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayerNameplate(response.data)
setLoading(false)
})
.catch((err) => {
if (axios.isCancel(err)) {
// Handle cancellation if needed
} else {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [setPlayerNameplate, setLoading, setError])
}
type PlayerScoreLogType = any
interface UseFetchPlayerScoreLogParams {
setPlayerScoreLog: Dispatch<SetStateAction<PlayerScoreLogType>>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
}
export const useFetchPlayerScoreLog = ({
setPlayerScoreLog,
setLoading,
setError,
}: UseFetchPlayerScoreLogParams) => {
useEffect(() => {
let isMounted = true
const source = axios.CancelToken.source()
async function fetchProfileData() {
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/chuni-score-playlog`
try {
const user = await ApiFetch<{ body: { version: number; } }>("/SDHD/user");
// Check if the version in the response is the expected version
if (user.body.body.version !== ARTEMIS_CHUNITHM_VERSION) {
setError("Please update to the latest version")
setLoading(false)
return
}
const response = await axios.get(apiUrl, { cancelToken: source.token })
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
if (isMounted) {
setPlayerScoreLog(response.data)
setLoading(false)
}
} catch (error) {
if (axios.isCancel(error)) {
// Request canceled
} else if (error instanceof Error) {
setError(error.message)
setLoading(false)
} else {
setError("An unknown error occurred")
setLoading(false)
}
}
}
fetchProfileData()
return () => {
isMounted = false
source.cancel("Operation canceled by the user.")
}
}, [setError, setLoading, setPlayerScoreLog])
}
type playerStaticMusic = any
interface UsePlayerStaticMusicParam {
setPlayerStaticMusic: Dispatch<SetStateAction<playerStaticMusic>>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
}
export const useFetchStaticMusic = ({
setPlayerStaticMusic,
setLoading,
setError,
}: UsePlayerStaticMusicParam) => {
useEffect(() => {
let isMounted = true
const source = axios.CancelToken.source()
async function fetchProfileData() {
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/chuni-static-music`
try {
const user = await ApiFetch<{ body: { version: number; } }>("/SDHD/user");
// Check if the version in the response is the expected version
if (user.body.body.version !== ARTEMIS_CHUNITHM_VERSION) {
setError("Please update to the latest version")
setLoading(false)
return
}
const response = await axios.get(apiUrl, { cancelToken: source.token })
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
if (isMounted) {
setPlayerStaticMusic(response.data)
setLoading(false)
}
} catch (error) {
if (axios.isCancel(error)) {
// Request canceled
} else if (error instanceof Error) {
setError(error.message)
setLoading(false)
} else {
setError("An unknown error occurred")
setLoading(false)
}
}
}
fetchProfileData()
return () => {
isMounted = false
source.cancel("Operation canceled by the user.")
}
}, [setError, setLoading, setPlayerStaticMusic])
}
type PlayerTeam = {
id: number
teamName: string
teamPoint: number
}
interface usePlayerTeamsParams {
setPlayerTeams: Dispatch<SetStateAction<PlayerTeam[]>>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
}
export const UsePlayerTeams = ({
setPlayerTeams,
setLoading,
setError,
}: usePlayerTeamsParams) => {
useEffect(() => {
const source = axios.CancelToken.source()
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/chuni-player-team`
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayerTeams(response.data)
setLoading(false)
})
.catch((err) => {
if (axios.isCancel(err)) {
} else {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [setPlayerTeams, setLoading, setError])
}
type PlayerRivalList = any
interface UsePlayerRivalListParams {
setPlayerRivalList: React.Dispatch<React.SetStateAction<PlayerRivalList[]>>
setLoading: React.Dispatch<React.SetStateAction<boolean>>
setError: React.Dispatch<React.SetStateAction<string | null>>
refreshRivalList: boolean // Add the new dependency
}
export const usePlayerRivalList = ({
setPlayerRivalList,
setLoading,
setError,
refreshRivalList, // Include it in the parameters
}: UsePlayerRivalListParams) => {
useEffect(() => {
const fetchData = async () => {
const source = axios.CancelToken.source()
try {
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/listRivals`
const response = await axios.get(apiUrl, { cancelToken: source.token })
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayerRivalList(response.data)
setLoading(false)
} catch (error) {
if (!axios.isCancel(error)) {
setError("error")
setLoading(false)
}
}
return () => {
source.cancel("Operation canceled by the user.")
}
}
fetchData()
}, [
setPlayerRivalList,
setLoading,
setError,
refreshRivalList,
]) // Include refreshRivalList as a dependency
}
type PlayerFavoriteSong = {
songId: number
// Add other properties as needed
}
export type PlayerFavoriteSongsList = PlayerFavoriteSong[]
interface UsePlayerFavoriteSongListResult {
playerFavoriteSongs: PlayerFavoriteSongsList
loading: boolean
error: string | null
refetchPlayerFavoriteSongs: () => void // Add refetch function
}
export const usePlayerFavoriteSongsList =
(): UsePlayerFavoriteSongListResult => {
const [playerFavoriteSongs, setPlayerFavoriteSongs] =
useState<PlayerFavoriteSongsList>([])
const [loading, setLoading] = useState<boolean>(true)
const [error, setError] = useState<string | null>(null)
const fetchData = async () => {
const source = axios.CancelToken.source()
try {
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/list-favorite-songs`
const response = await axios.get(apiUrl, { cancelToken: source.token })
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayerFavoriteSongs(response.data)
setLoading(false)
} catch (error) {
if (!axios.isCancel(error)) {
setError("An error occurred")
setLoading(false)
}
} finally {
source.cancel("Operation canceled by the user.")
}
}
useEffect(() => {
fetchData()
}, [])
const refetchPlayerFavoriteSongs = () => {
setLoading(true)
setError(null)
fetchData()
}
return { playerFavoriteSongs, loading, error, refetchPlayerFavoriteSongs }
}
type PlayerAimeCard = {
accessCode: number
}
interface usePlayerAimeCardParams {
setPlayAimeCard: Dispatch<SetStateAction<PlayerAimeCard[]>>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
}
export const UsePlayerAimeCard = ({
setPlayAimeCard,
setLoading,
setError,
}: usePlayerAimeCardParams) => {
useEffect(() => {
const source = axios.CancelToken.source()
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/access-code`
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayAimeCard(response.data)
setLoading(false)
})
.catch((err) => {
if (axios.isCancel(err)) {
} else {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [])
}
type PlayerProfileTrophies = {
name: string
trophyId: number
itemId: string
}
interface usePlayerProfileTrophiesParam {
setPlayerProfileTrophies: Dispatch<SetStateAction<PlayerProfileTrophies[]>>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
}
export const UsePlayerProfileTrophies = ({
setPlayerProfileTrophies,
setLoading,
setError,
}: usePlayerProfileTrophiesParam) => {
useEffect(() => {
const source = axios.CancelToken.source()
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/profile-data`
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayerProfileTrophies(response.data)
setLoading(false)
})
.catch((err) => {
if (axios.isCancel(err)) {
} else {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [setPlayerProfileTrophies, setLoading, setError])
}
type PlayerTrophies = {
name: string
trophyId: string
itemId: string
rareType: number
isClear: number
isAllJustice: number
}
interface usePlayerTrophiesParam {
setPlayerTrophies: Dispatch<SetStateAction<PlayerTrophies[]>>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
}
export const UsePlayerTrophies = ({
setPlayerTrophies,
setLoading,
setError,
}: usePlayerTrophiesParam) => {
useEffect(() => {
const source = axios.CancelToken.source()
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/get-trophies`
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayerTrophies(response.data)
setLoading(false)
})
.catch((err) => {
if (axios.isCancel(err)) {
} else {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [setPlayerTrophies, setLoading, setError])
}
type PlayerAvatarItems = {
avatarBack: string
avatarItem: string
avatarWear: string
avatarFront: string
avatarSkin: string
avatarHead: string
avatar_skinfoot_l: string
avatar_skinfoot_r: string
avatarFace: string
}
interface usePlayerAvatarItemsParam {
setPlayerAvatarItems: Dispatch<SetStateAction<PlayerAvatarItems[]>>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
}
export const UsePlayerAvatarItems = ({
setPlayerAvatarItems,
setLoading,
setError,
}: usePlayerAvatarItemsParam) => {
useEffect(() => {
const source = axios.CancelToken.source()
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/chuni-avatar-items`
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayerAvatarItems(response.data)
setLoading(false)
})
.catch((err) => {
if (axios.isCancel(err)) {
} else {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [setPlayerAvatarItems, setLoading, setError])
}
type PlayerDuelCompletions = {
duelId: string
str: string
}
interface UsePlayerDuelCompletionsParams {
setPlayerDuelCompletions: Dispatch<SetStateAction<PlayerDuelCompletions[]>>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
onSuccess?: (data: PlayerDuelCompletions[]) => void
}
export const UsePlayerDuelCompletions = ({
setPlayerDuelCompletions,
setLoading,
setError,
onSuccess,
}: UsePlayerDuelCompletionsParams) => {
useEffect(() => {
const source = axios.CancelToken.source()
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/chuni-duel-items`
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayerDuelCompletions(response.data)
setLoading(false)
})
.catch((err) => {
if (axios.isCancel(err)) {
} else {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [setPlayerDuelCompletions, setLoading, setError])
}
type playerAvailableNameplates = {
nameplateId: string
str: string
imagePath: string
}
interface UsePlayerAvailableNameplatesParams {
setPlayerAvailableNameplates: Dispatch<
SetStateAction<playerAvailableNameplates[]>
>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
onSuccess?: (data: PlayerDuelCompletions[]) => void
}
export const UsePlayerAvailableNameplates = ({
setPlayerAvailableNameplates,
setLoading,
setError,
onSuccess,
}: UsePlayerAvailableNameplatesParams) => {
useEffect(() => {
const source = axios.CancelToken.source()
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/chuni-nameplate-items`
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayerAvailableNameplates(response.data)
setLoading(false)
})
.catch((err) => {
if (axios.isCancel(err)) {
} else {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [setPlayerAvailableNameplates, setLoading, setError])
}
type playerAvailableMapIcons = {
itemId: string
str: string
imagePath: string
}
interface UsePlayerAvailableMapIconsParams {
setPlayerAvailableMapIcons: Dispatch<
SetStateAction<playerAvailableMapIcons[]>
>
setLoading: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
onSuccess?: (data: PlayerDuelCompletions[]) => void
}
export const UsePlayerAvailableMapIcons = ({
setPlayerAvailableMapIcons,
setLoading,
setError,
onSuccess,
}: UsePlayerAvailableMapIconsParams) => {
useEffect(() => {
const source = axios.CancelToken.source()
const apiUrl = `${process.env.NEXT_PUBLIC_ARTEMIS_API_URL}/SDHD/chuni-map-items`
axios
.get(apiUrl, { cancelToken: source.token })
.then((response) => {
if (response.status !== 200) {
throw new Error("Network response was not ok")
}
setPlayerAvailableMapIcons(response.data)
setLoading(false)
})
.catch((err) => {
if (axios.isCancel(err)) {
} else {
setError(err.message)
setLoading(false)
}
})
return () => {
source.cancel("Operation canceled by the user.")
}
}, [setPlayerAvailableMapIcons, setLoading, setError])
}