Frontend prototype implementation

This commit is contained in:
Florian Sylvain
2023-03-08 13:25:14 +01:00
parent 9340200086
commit 8bce6af70c
18 changed files with 2438 additions and 3 deletions
+9
View File
@@ -0,0 +1,9 @@
<script setup lang="ts">
import { RouterView } from "vue-router"
</script>
<template>
<RouterView />
</template>
<style scoped></style>
+3
View File
@@ -0,0 +1,3 @@
* {
box-sizing: border-box;
}
+5
View File
@@ -0,0 +1,5 @@
<script setup lang="ts"></script>
<template></template>
<style scoped></style>
+14
View File
@@ -0,0 +1,14 @@
import { createApp } from "vue"
import { createPinia } from "pinia"
import App from "./App.vue"
import router from "./router"
import "./assets/base.css"
const app = createApp(App)
app.use(createPinia())
app.use(router)
app.mount("#app")
+15
View File
@@ -0,0 +1,15 @@
import { createRouter, createWebHistory } from "vue-router"
import HomeView from "../views/HomeView.vue"
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: "/",
name: "home",
component: HomeView,
},
],
})
export default router
+3
View File
@@ -0,0 +1,3 @@
import { defineStore } from "pinia"
export const useStore = defineStore("store", () => {})
+81
View File
@@ -0,0 +1,81 @@
<script setup lang="ts">
import { onMounted, Ref, ref } from "vue"
interface AuthorData {
author: {
id: number
fullname: string
created_at: string
updated_at: string
}
}
interface Painting {
id: number
name: string
image_url: string
link: string
musuemId: number
PaintingAuthor: AuthorData[]
}
const paintings: Ref<Painting[]> = ref([])
const winningPainting: Ref<Painting> = ref({} as Painting)
const gameMessage = ref("")
function fetchRandomPainting(): Promise<{ paintings: Painting[] }> {
return new Promise((resolve, reject) => {
fetch(`${__APP_ENV__.API_ADDRESS}/paintings/random/4`)
.then((response) => response.json())
.then((data) => resolve(data))
.catch((error) => reject(error))
})
}
async function initPaintings(): Promise<void> {
const fetchedData = await fetchRandomPainting()
paintings.value = fetchedData.paintings
}
function setWinningPainting(): void {
const randomIndex = Math.floor(Math.random() * paintings.value.length)
winningPainting.value = paintings.value[randomIndex]
}
function authorClickHandler(auhtorId: number): void {
if (auhtorId === winningPainting.value.PaintingAuthor[0].author.id) {
gameMessage.value = "Correct!"
} else {
gameMessage.value = "Wrong!"
}
}
async function initGame(): Promise<void> {
await initPaintings()
setWinningPainting()
gameMessage.value = "What is the author of this painting?"
}
onMounted(async () => {
await initGame()
})
</script>
<template>
<img :src="winningPainting?.image_url" />
<button
v-for="painting of paintings"
@click="() => authorClickHandler(painting.PaintingAuthor[0].author.id)"
type="button"
>
{{ painting.PaintingAuthor[0].author.fullname }}
</button>
<p>{{ gameMessage }}</p>
<button type="button" @click="initGame">Next</button>
</template>
<style scoped>
img {
width: 200px;
}
</style>