- dear god lazy loading why but it works good night

This commit is contained in:
Alexis
2020-06-22 23:37:24 +02:00
parent 96d13ec6e9
commit 7819cf004d
5 changed files with 90 additions and 30 deletions

View File

@@ -5,19 +5,22 @@ const resource = "/spells"
// CRUD methods for spells // CRUD methods for spells
export default { export default {
getSpells() { getAll() {
return api.get(`${resource}`) return api.get(`${resource}`)
}, },
getSpell(id) { getPage(page) {
return api.get(`${resource}/page/${page}`)
},
getOne(id) {
return api.get(`${resource}/${id}`) return api.get(`${resource}/${id}`)
}, },
addSpell(data) { addOne(data) {
return api.post(`${resource}`, data) return api.post(`${resource}`, data)
}, },
updateSpell(id, data) { updateOne(id, data) {
return api.put(`${resource}/${id}`, data) return api.put(`${resource}/${id}`, data)
}, },
deleteSpell(id) { deleteOne(id) {
return api.delete(`${resource}/${id}`) return api.delete(`${resource}/${id}`)
} }
} }

View File

@@ -143,7 +143,7 @@ export default {
ingredients: ingredientsData, ingredients: ingredientsData,
} }
Spells.addSpell(data) Spells.addOne(data)
.then(v => { .then(v => {
this.$emit('addSpell', v.data) this.$emit('addSpell', v.data)
this.$refs["add_spell_modal"].hide() this.$refs["add_spell_modal"].hide()

View File

@@ -182,7 +182,7 @@ export default {
ingredients: ingredientsData, ingredients: ingredientsData,
} }
Spells.updateSpell(this.spell.id, data) Spells.updateOne(this.spell.id, data)
.then(v => { .then(v => {
this.$emit('updateSpell', v.data) this.$emit('updateSpell', v.data)
}) })

View File

@@ -93,7 +93,7 @@ export default {
this.$emit('editSpell', spell) this.$emit('editSpell', spell)
}, },
deleteSpell(spell) { deleteSpell(spell) {
Spells.deleteSpell(this.spell.id) Spells.deleteOne(this.spell.id)
.then(() => { .then(() => {
this.$emit('deleteSpell', spell) this.$emit('deleteSpell', spell)
}) })

View File

@@ -4,11 +4,35 @@
<i class="mad">add</i> <i class="mad">add</i>
<span>Ajouter un sort</span> <span>Ajouter un sort</span>
</button> </button>
<div v-masonry transition-duration=".5s" item-selector=".spell-card" class="row spells-list"> <div
<spell-card v-masonry-tile class="spell-card" v-for="(spell) in spells" :key="spell.id" v-bind:spell="spell" @editSpell="editSpell" @deleteSpell="deleteSpell"/> v-if="computedSpells.length > 0"
class="spell-list-wrapper">
<div
class="row spells-list"
v-masonry
transition-duration=".5s"
item-selector=".spell-card">
<spell-card
class="spell-card"
v-masonry-tile
v-for="(spell) in computedSpells"
:key="spell.id"
:spell="spell"
@editSpell="editSpell"
@deleteSpell="deleteSpell" />
</div>
<edit-spell-card
v-if="Object.keys(current_edit_spell).length > 0"
:spell="current_edit_spell"
@editSpell="editSpell"
@updateSpell="updateSpell"/>
<add-spell-card
v-if="adding_spell"
@cancelAdd="cancelAdd"
@addSpell="addSpell"/>
</div> </div>
<edit-spell-card v-if="Object.keys(current_edit_spell).length > 0" v-bind:spell="current_edit_spell" @editSpell="editSpell" @updateSpell="updateSpell"/>
<add-spell-card v-if="adding_spell" @cancelAdd="cancelAdd" @addSpell="addSpell"/>
</div> </div>
</template> </template>
@@ -21,7 +45,7 @@ import AddSpellCard from "./add-spell-card"
// API // API
import { RepositoryFactory } from "~/api/repositories" import { RepositoryFactory } from "~/api/repositories"
const Spells = RepositoryFactory.get('spells') const Spells = RepositoryFactory.get('spells')
const Schools = RepositoryFactory.get('schools') // const Schools = RepositoryFactory.get('schools')
export default { export default {
name: 'spellslist', name: 'spellslist',
@@ -31,34 +55,67 @@ export default {
'add-spell-card': AddSpellCard, 'add-spell-card': AddSpellCard,
}, },
props: { props: {
school_id: String school_id: String,
}, },
data() { data() {
return { return {
loading: false,
spells: [], spells: [],
loading: false,
current_edit_spell: {}, current_edit_spell: {},
currentIndex: 1,
adding_spell: false, adding_spell: false,
} }
}, },
created() { computed: {
this.computeSpells() computedSpells() {
}, return this.spells
methods: {
async fetchSpells() {
if (!this.school_id) {
const { data } = await Spells.getSpells()
return data
} else {
const { data } = await Schools.getSpellsFromSchool(this.school_id)
return data.spells
} }
}, },
async computeSpells() { beforeMount() {
this.spells = this.computedSpells
this.getInitialSpells()
},
mounted() {
this.scroll()
},
methods: {
getInitialSpells() {
Spells.getPage(this.currentIndex)
.then(v => {
this.loading = true this.loading = true
const displaySpells = await this.fetchSpells() let spells = this.computedSpells
spells.push(v.data)
this.spells = spells[0]
})
.then(() => {
this.currentIndex++
this.loading = false this.loading = false
this.spells = displaySpells })
.catch(err => {
console.log(err)
})
},
scroll() {
window.onscroll = () => {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
Spells.getPage(this.currentIndex)
.then(v => {
this.loading = true
let spells = this.computedSpells
for (let i = 0; i < v.data.length; i++) {
const element = v.data[i];
spells.push(element)
}
})
.then(() => {
this.currentIndex++
this.loading = false
})
.catch(err => {
console.log(err)
})
}
}
}, },
editSpell(e) { editSpell(e) {
this.current_edit_spell = e; this.current_edit_spell = e;