- [API] Segregated API files, Implemented spells-list, component that calls the API

This commit is contained in:
Alexis
2020-04-06 16:39:55 +02:00
parent 37f4913d3e
commit 042c205ce4
11 changed files with 169 additions and 29 deletions

7
client/api/api.js Normal file
View File

@@ -0,0 +1,7 @@
import axios from "axios"
const url = "http://localhost:2814/api"
export default axios.create({
baseURL: url
})

View File

@@ -0,0 +1,11 @@
import spellsRepository from './spellsRepository'
// List of possible repositories
const repositories = {
spells: spellsRepository
}
// Usage : RepositoryFactoryInstance.get('reponame');
export const RepositoryFactory = {
get: name => repositories[name]
}

View File

@@ -0,0 +1,14 @@
import api from './api'
// URL for spells
const resource = "/spells"
// CRUD methods for spells
export default {
getSpells() {
return api.get(`${resource}`)
},
getSpell(id) {
return api.get(`${resource}?id=${id}`)
},
}

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 24.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 32 34" style="enable-background:new 0 0 32 34;" xml:space="preserve">
<style type="text/css">
.st0{fill:#000;}
.st1{fill:#599EF4;}
</style>
<g id="layer_cog">
<path class="st0" d="M31.6,16L29,15.5h0c-0.1,0-0.3-0.1-0.4-0.1c-0.2,0-0.3-0.2-0.4-0.3c-0.3-1.3-0.8-2.5-1.5-3.6
c-0.1-0.2-0.1-0.4,0-0.5c0.5-0.8,1.1-1.6,1.6-2.5c0.2-0.2,0.2-0.6-0.1-0.8c-0.7-0.7-1.4-1.4-2-2c-0.2-0.2-0.5-0.2-0.7,0l-0.9,0.6
l-1.8,1.1c-1.2,0.8-0.6,3.2,0,3.8c0.8,0.8,1.5,1.8,2,2.8c0.5,1,0.7,2.1,0.8,3.2c0,0.2,0,0.4,0,0.5s0,0.1,0,0.1c0,0.5,0,1.1-0.1,1.7
c-0.2,1.2-0.6,2.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.3-0.5-0.5-1.1-0.6-1.7c0-0.5,0-0.9,0-1.4c-0.1,0.1-0.2,0.3-0.3,0.5c-0.5,0.9-0.6,1.9-0.3,2.9c0.2,0.7,0.5,1.5,0.7,2.2
c0.2,0.7,0.4,1.4,0.4,2.1C18.4,21.7,17.7,22.8,16.5,23.4L16.5,23.4z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 24.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 32 34" style="enable-background:new 0 0 32 34;" xml:space="preserve">
<style type="text/css">
.st0{fill:#FFFFFF;}
.st1{fill:#599EF4;}
</style>
<g id="layer_cog">
<path class="st0" d="M31.6,16L29,15.5h0c-0.1,0-0.3-0.1-0.4-0.1c-0.2,0-0.3-0.2-0.4-0.3c-0.3-1.3-0.8-2.5-1.5-3.6
c-0.1-0.2-0.1-0.4,0-0.5c0.5-0.8,1.1-1.6,1.6-2.5c0.2-0.2,0.2-0.6-0.1-0.8c-0.7-0.7-1.4-1.4-2-2c-0.2-0.2-0.5-0.2-0.7,0l-0.9,0.6
l-1.8,1.1c-1.2,0.8-0.6,3.2,0,3.8c0.8,0.8,1.5,1.8,2,2.8c0.5,1,0.7,2.1,0.8,3.2c0,0.2,0,0.4,0,0.5s0,0.1,0,0.1c0,0.5,0,1.1-0.1,1.7
c-0.2,1.2-0.6,2.4-1.2,3.4c-0.6,1.1-1.5,2-2.5,2.8c-1.7,1.3-3.8,2-5.9,2l0,0c-2.1,0-4.2-0.7-5.9-2c-1-0.8-1.8-1.7-2.5-2.8
c-0.6-1-1.1-2.2-1.2-3.4c-0.1-0.5-0.1-1.1-0.1-1.7c0,0,0-0.1,0-0.1s0-0.3,0.1-0.5c0.2-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After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.7 KiB

View File

@@ -63,11 +63,11 @@ export default {
name: 'spell-card',
props: {
name: String,
description: Array,
level: String,
description: String,
level: Number,
schools: Array,
ingredients: String,
charge: String,
ingredients: Array,
charge: Number,
cost: String,
variables: Array,
},

View File

@@ -0,0 +1,47 @@
<template>
<div class="spells-list">
<spell-card v-for="(spell, index) in spells" :key="index" v-bind="spell"/>
</div>
</template>
<script>
// Components
import spellcard from "./spell-card"
// API
import { RepositoryFactory } from "../../../api/repositories"
const spellsRepository = RepositoryFactory.get('spells')
export default {
name: 'spellslist',
components: {
'spell-card': spellcard,
},
data() {
return {
loading: false,
spells: []
}
},
created() {
this.computeSpells()
},
methods: {
async fetchSpells() {
const { data } = await spellsRepository.getSpells()
return data
},
async computeSpells() {
this.loading = true
const displaySpells = await this.fetchSpells();
this.loading = false
this.spells = displaySpells.splice(0,10)
}
}
}
</script>
<style>
</style>

View File

@@ -1,34 +1,19 @@
<template>
<div class="spells-page">
<v-for v-for="(spell, index) in spells" :key="index">
<spell-card v-bind="spell"/>
</v-for>
<spell-list/>
</div>
</template>
<script>
import axios from 'axios';
import spellcard from "../components/spells/spell-card";
// Components
import spellslist from "../components/spells/spells-list"
export default {
name: 'spells',
components: {
'spell-card': spellcard,
},
data() {
return {
spells: []
}
},
mounted() {
axios({ method: "GET", "url": "http://localhost:2814/api/spells"})
.then(result => {
this.spells = result.data.spells;
console.log(result)
}, error => {
console.log(error)
})
},
'spell-list': spellslist,
}
}
</script>

View File

@@ -2,8 +2,9 @@
// MODULES
const express = require('express')
const cors = require('cors')
const cors = require('cors') // module to format the json response
// Creates instances of database connections
const connection = require('./database/connection')
const db = connection.db
@@ -13,11 +14,14 @@ const port = 2814
// Import routes
const routes = require('./routes')
// Builds app
// Builds app w/ express
let app = express()
app.use(cors())
const server = app.listen( port, () => { console.log(`App listening on port ${port}`)} )
// Serves
const server = app.listen( port, () => {console.log(`App listening on port ${port}`)})
// Routing
app.use('/api/spells', routes.spells)
// On process kill with SIGINT

View File

@@ -92,7 +92,7 @@ router
getSpells(req.query)
.then(v => {
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({spells : v}))
res.end(JSON.stringify(v))
})
.catch(err => {
console.log(err)