gamebrary/src/components/Board/AddList.vue

141 lines
2.8 KiB
Vue
Raw Normal View History

2020-08-11 04:16:43 +00:00
<template lang="html">
<div>
<b-button
v-b-modal="modalId"
2020-08-11 23:39:11 +00:00
class="mr-3"
2020-08-11 04:16:43 +00:00
:title="title"
ref="addList"
>
2020-08-15 06:39:19 +00:00
<b-icon-plus />
2020-08-11 04:16:43 +00:00
</b-button>
<b-modal
:id="modalId"
:title="title"
@shown="reset"
>
<form ref="addListForm" @submit.stop.prevent="submit">
<b-form-input
autofocus
v-model="listName"
:placeholder="$t('list.placeholder')"
required
/>
<b-alert
class="mt-3 mb-0"
2020-08-14 23:58:55 +00:00
:show="isDuplicate && !saving"
2020-08-11 04:16:43 +00:00
variant="warning"
>
{{ $t('list.duplicateWarning') }}
</b-alert>
</form>
<template v-slot:modal-footer="{ cancel }">
<b-button @click="cancel">
Cancel
</b-button>
<b-button
variant="primary"
:disabled="saving || isDuplicate"
@click="submit"
>
<b-spinner small v-if="saving" />
<span v-else>{{ buttonLabel }}</span>
</b-button>
</template>
</b-modal>
</div>
</template>
<script>
import { mapState } from 'vuex';
export default {
data() {
return {
listName: '',
saving: false,
2020-08-18 18:55:39 +00:00
modalId: 'add-list',
2020-08-11 04:16:43 +00:00
};
},
computed: {
2020-08-21 06:11:54 +00:00
...mapState(['platform', 'board']),
2020-08-11 04:16:43 +00:00
title() {
return this.isEmptyBoard
? this.$t('list.addFirstTime')
: this.$t('list.add');
},
buttonLabel() {
return this.isEmptyBoard
? this.$t('list.getStarted')
: this.$t('global.save');
},
existingListNames() {
2020-08-21 06:11:54 +00:00
return this.board.lists
? this.board.lists.map(({ name }) => name.toLowerCase())
2020-08-11 04:16:43 +00:00
: [];
},
isDuplicate() {
return this.existingListNames.includes(this.listName.toLowerCase());
},
isEmptyBoard() {
2020-08-21 06:11:54 +00:00
return this.board.lists.length === 0;
2020-08-11 04:16:43 +00:00
},
disabled() {
return this.isDuplicate || !this.listName;
},
},
methods: {
reset() {
this.listName = '';
},
submit(e) {
e.preventDefault();
if (this.$refs.addListForm.checkValidity()) {
this.addList();
}
},
2020-08-21 06:11:54 +00:00
async addList() {
2020-08-11 04:16:43 +00:00
const list = {
games: [],
name: this.listName,
settings: {},
2020-08-11 04:16:43 +00:00
};
2020-08-14 23:58:55 +00:00
this.saving = true;
2020-08-21 06:11:54 +00:00
await this.$store.dispatch('ADD_LIST', list)
.catch(() => {
this.$bvToast.toast('Error adding list', { title: 'Error', variant: 'danger' });
2020-08-11 04:16:43 +00:00
});
2020-08-14 23:58:55 +00:00
2020-08-21 06:11:54 +00:00
this.$bvToast.toast('List added', { variant: 'success' });
this.saving = false;
this.$bvModal.hide(this.modalId);
this.scroll();
2020-08-11 04:16:43 +00:00
},
scroll() {
this.$nextTick(() => {
2020-08-14 23:58:55 +00:00
const board = document.querySelector('.board');
2020-08-11 04:16:43 +00:00
2020-08-14 23:58:55 +00:00
board.scrollLeft = board.scrollWidth;
2020-08-11 04:16:43 +00:00
});
},
},
};
</script>