|
| 1 | +<template> |
| 2 | + <main> |
| 3 | + <h1>Game Importer</h1> |
| 4 | + |
| 5 | + <h2>Select a Platform</h2> |
| 6 | + <p> |
| 7 | + Click a platform to load games from and import as desired. |
| 8 | + If you want to link an account, go to <page-link text="your integration settings" href="/settings/integrations" />. |
| 9 | + </p> |
| 10 | + <div> |
| 11 | + <!-- on hold until chess.com lets me use their oauth :( --> |
| 12 | + <!-- <v-btn v-if="integrations?.chesscom" class="mr-3" text="Chess.com" color="green" /> --> |
| 13 | + <v-btn v-if="integrations?.lichess" text="Lichess" color="grey" :loading="lichessFetching" @click="fetchLichessGames" /> |
| 14 | + </div> |
| 15 | + |
| 16 | + <h2>Games</h2> |
| 17 | + <v-table theme="dark"> |
| 18 | + <thead> |
| 19 | + <tr> |
| 20 | + <th>Players</th> |
| 21 | + <th>Result</th> |
| 22 | + <th>Date</th> |
| 23 | + <th>Options</th> |
| 24 | + </tr> |
| 25 | + </thead> |
| 26 | + <tbody> |
| 27 | + <chess-game-result-row v-for="game in lichessGames" :key="game.id" :date="game.date" |
| 28 | + :white-player="game.whitePlayer" :black-player="game.blackPlayer" |
| 29 | + :clean-result="game.cleanResult" :friendly-result="game.friendlyResult"> |
| 30 | + <td> |
| 31 | + <span v-if="game.imported?.status === false"> |
| 32 | + <v-btn color="green" :loading="loading[game.id]" @click="importGame(game, 'lichess')"> |
| 33 | + Import <i class="fa-solid fa-download" /> |
| 34 | + </v-btn> |
| 35 | + </span> |
| 36 | + |
| 37 | + <PageLink v-if="game.imported?.status === true" :href="`/game/${game.imported.id}`"> |
| 38 | + <v-btn color="blue"> |
| 39 | + View <i class="fa-solid fa-external-link" /> |
| 40 | + </v-btn> |
| 41 | + </PageLink> |
| 42 | + </td> |
| 43 | + </chess-game-result-row> |
| 44 | + </tbody> |
| 45 | + </v-table> |
| 46 | + </main> |
| 47 | +</template> |
| 48 | + |
| 49 | +<script lang="ts"> |
| 50 | +import { defineComponent } from 'vue' |
| 51 | +import type { Database } from '~/types/supabase' |
| 52 | +import type { CleanedGame } from '~/utils/games' |
| 53 | +import type { UserImportGameResponse, UserIntegrationsResponse, UserLichessGames } from '~/types/requests' |
| 54 | +
|
| 55 | +type weirdMergedCleanedGame = CleanedGame & {imported?: {status: boolean, id?: string}} |
| 56 | +
|
| 57 | +export default defineComponent({ |
| 58 | + name: 'import', |
| 59 | +
|
| 60 | + async setup() { |
| 61 | + const client = useSupabaseClient<Database>() |
| 62 | + const user = useSupabaseUser().value |
| 63 | + const id = user?.id |
| 64 | +
|
| 65 | + const inteData = await useFetch<UserIntegrationsResponse>(`/api/users/${id}/integrations`) |
| 66 | +
|
| 67 | + const integrations = inteData.data.value?.integrations |
| 68 | +
|
| 69 | + return { |
| 70 | + client, user, integrations |
| 71 | + } |
| 72 | + }, |
| 73 | +
|
| 74 | + data() { |
| 75 | + return { |
| 76 | + // Your data properties here |
| 77 | + lichessFetching: false, |
| 78 | + lichessGames: [] as weirdMergedCleanedGame[], |
| 79 | + loading: {} as Record<string, boolean> |
| 80 | + } |
| 81 | + }, |
| 82 | +
|
| 83 | + methods: { |
| 84 | + // Your methods here |
| 85 | + async fetchLichessGames() { |
| 86 | + this.lichessFetching = true |
| 87 | + await $fetch<UserLichessGames>('/api/users/me/games/lichess').then((res) => { |
| 88 | + if (res.error) { |
| 89 | + throw showError({ statusCode: 500, statusMessage: res.error }) |
| 90 | + } |
| 91 | +
|
| 92 | + if (res.data) { |
| 93 | + this.lichessGames = res.data |
| 94 | + } |
| 95 | + }) |
| 96 | + this.lichessFetching = false |
| 97 | + }, |
| 98 | +
|
| 99 | + async importGame(game: weirdMergedCleanedGame, platform: string) { |
| 100 | + this.loading[game.id] = true |
| 101 | + await $fetch<UserImportGameResponse>('/api/users/me/games/import', { |
| 102 | + method: 'POST', |
| 103 | + headers: { |
| 104 | + 'Content-Type': 'application/json' |
| 105 | + }, |
| 106 | + body: { |
| 107 | + platformId: game.id, |
| 108 | + platform, |
| 109 | + platformUsername: this.integrations?.lichess?.data.id |
| 110 | + } |
| 111 | + }).then((res) => { |
| 112 | + if (res.error) { |
| 113 | + throw showError({ statusCode: 500, statusMessage: res.error }) |
| 114 | + } |
| 115 | +
|
| 116 | + if (res.success) { |
| 117 | + this.lichessGames.find(g => g.id === game.id)!.imported = { status: true, id: res.id } |
| 118 | + } |
| 119 | + }) |
| 120 | + this.loading[game.id] = false |
| 121 | + } |
| 122 | + } |
| 123 | +}) |
| 124 | +</script> |
0 commit comments