Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/total games #55

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
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
12 changes: 9 additions & 3 deletions components/profile/ProfileInformation.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<script setup lang="ts">

const session = useSupabaseSession()
const { user, loading, error, fetchUser } = useUser()
const { user, loading, error, fetchUser, fetchTotalGames } = useUser()
const { fetchGames } = useGame()

const totalGames = useState<string | null>('userTotalGames', () => null)

onMounted(async () => {
if (session.value) {
await fetchUser()
await fetchUser();
await fetchGames();
await fetchTotalGames();
}
})

Expand Down Expand Up @@ -33,7 +38,8 @@ const formatDate = (dateString: string) => {

<div class="grid grid-cols-2 gap-2">
<div class="bg-gray-200 h-8 rounded-3xl bg-yellow-500 px-2 flex items-center">
Games played
<Icon name="mdi:controller" class="text-2xl mr-2 text-red-600" />
{{ totalGames }} Games
</div>
<div class="bg-gray-200 h-8 rounded-3xl bg-yellow-500 px-2 flex items-center">
<Icon name="mdi:fire" class="text-2xl mr-2 text-red-600" />
Expand Down
27 changes: 2 additions & 25 deletions composables/useSpotify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export default function useSpotify(playlistId: string) {
const data = await res.json();

for (const item of data.items) {
const itemCount = await getPlaylistItems(item.id);
const itemCount = item.tracks.total
console.log(itemCount)
if (itemCount >= 8) {
const exists = userPlaylists.value.some(playlist => playlist.id === item.id);
if (!exists) {
Expand All @@ -117,29 +118,6 @@ export default function useSpotify(playlistId: string) {
}
}

async function getPlaylistItems(playlistId: string): Promise<number> {
try {
const res = await fetch(`https://api.spotify.com/v1/playlists/${ playlistId }/tracks`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token.value}`,
},
});

if (!res.ok) {
console.error(`Error: ${res.status} - ${res.statusText}`);
return 0;
}

const data = await res.json();

return data.items.length;
} catch (error) {
console.error('Failed to get playlists:', error);
return 0;
}
}

async function getTrackCover(trackId: string): Promise<string> {
try {
const res = await fetch(`https://api.spotify.com/v1/tracks/${trackId}`, {
Expand Down Expand Up @@ -169,7 +147,6 @@ export default function useSpotify(playlistId: string) {
followPlaylist,
unfollowPlaylist,
getUserPlaylists,
getPlaylistItems,
getTrackCover
};
}
20 changes: 19 additions & 1 deletion composables/useUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type {GetUserResponse} from "@/types/api/users";

export const useUser = () => {
const user = useState<GetUserResponse | null>('user', () => null)
const totalGames = useState<number>('userTotalGames', () => 0)
const games = useState<string | null>('games', () => null)
const loading = useState<boolean>('userLoading', () => false)
const error = useState<string | null>('userError', () => null)

Expand All @@ -21,11 +23,27 @@ export const useUser = () => {
}
}

const fetchTotalGames = async () => {
try {
totalGames.value = 0;
games.value.past.forEach(item => {
if (item.players[0].id === user.value?.id || item.players[1].id === user.value?.id) {
totalGames.value++;
}
});
} catch (err) {
error.value = 'An error occurred';
console.error('Error fetching user', err)
}
}

return {
user,
totalGames,
loading,
error,
fetchUser
fetchUser,
fetchTotalGames
}
}

Expand Down
Loading
Loading