gamebrary/src/components/Board/AddList.vue

131 lines
2.7 KiB
Vue
Raw Normal View History

2020-08-11 04:16:43 +00:00
<template lang="html">
2020-08-25 04:24:32 +00:00
<b-button
v-b-modal="modalId"
2020-10-05 18:42:04 +00:00
:title="$t('board.addList.title')"
2020-10-08 17:55:26 +00:00
size="sm"
2020-09-24 04:15:31 +00:00
v-b-tooltip.hover.left
2020-08-25 04:24:32 +00:00
ref="addList"
>
2020-10-08 17:55:26 +00:00
<icon name="plus" white />
2020-08-11 04:16:43 +00:00
<b-modal
:id="modalId"
2020-10-05 18:42:04 +00:00
:title="$t('board.addList.title')"
@show="reset"
2020-08-11 04:16:43 +00:00
>
<form ref="addListForm" @submit.stop.prevent="submit">
<b-form-input
autofocus
v-model="listName"
2020-10-05 18:42:04 +00:00
:placeholder="$t('board.addList.placeholder')"
2020-08-11 04:16:43 +00:00
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"
>
2020-10-05 18:42:04 +00:00
{{ $t('board.list.duplicateWarning') }}
2020-08-11 04:16:43 +00:00
</b-alert>
</form>
<template v-slot:modal-footer="{ cancel }">
<b-button @click="cancel">
2020-10-05 18:42:04 +00:00
{{ $t('global.cancel') }}
2020-08-11 04:16:43 +00:00
</b-button>
<b-button
variant="primary"
:disabled="saving || isDuplicate"
@click="submit"
>
<b-spinner small v-if="saving" />
2020-10-05 18:42:04 +00:00
<span v-else>{{ $t('global.save') }}</span>
2020-08-11 04:16:43 +00:00
</b-button>
</template>
</b-modal>
2020-08-25 04:24:32 +00:00
</b-button>
2020-08-11 04:16:43 +00:00
</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
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-25 04:24:32 +00:00
return this.board.lists && 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;
this.$store.commit('ADD_LIST', list);
await this.$store.dispatch('SAVE_BOARD')
2020-08-21 06:11:54 +00:00
.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
this.$forceUpdate();
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>