🚧 You can now save items to VueX store, neat!

This commit is contained in:
Alicia Sykes 2021-10-16 21:03:26 +01:00
parent 677f0eea4e
commit f1fc013457
3 changed files with 37 additions and 7 deletions

View File

@ -7,22 +7,22 @@
classes="dashy-modal edit-item" classes="dashy-modal edit-item"
@closed="modalClosed" @closed="modalClosed"
> >
<h3>Edit Item</h3> <h3>Edit Item</h3>
<form> <div class="row" v-for="(row, index) in formData" :key="row.name">
<div class="row" v-for="row in formData" :key="row.name">
<Input <Input
:label="row.name" v-model="formData[index].value"
:value="row.value"
:description="row.description" :description="row.description"
:label="row.name"
layout="horizontal" layout="horizontal"
/> />
</div> </div>
</form> <Button :click="saveItem">Save</Button>
</modal> </modal>
</template> </template>
<script> <script>
import Input from '@/components/FormElements/Input'; import Input from '@/components/FormElements/Input';
import Button from '@/components/FormElements/Button';
import StoreKeys from '@/utils/StoreMutations'; import StoreKeys from '@/utils/StoreMutations';
import DashySchema from '@/utils/ConfigSchema'; import DashySchema from '@/utils/ConfigSchema';
import { modalNames } from '@/utils/defaults'; import { modalNames } from '@/utils/defaults';
@ -43,6 +43,7 @@ export default {
computed: {}, computed: {},
components: { components: {
Input, Input,
Button,
}, },
mounted() { mounted() {
this.item = this.getItemFromState(this.itemId); this.item = this.getItemFromState(this.itemId);
@ -66,6 +67,13 @@ export default {
getItemFromState(id) { getItemFromState(id) {
return this.$store.getters.getItemById(id); return this.$store.getters.getItemById(id);
}, },
saveItem() {
const newItem = {};
this.formData.forEach((row) => {
newItem[row.name] = row.value;
});
this.$store.commit(StoreKeys.UPDATE_ITEM, { newItem, itemId: this.itemId });
},
modalClosed() { modalClosed() {
this.$store.commit(StoreKeys.SET_MODAL_OPEN, false); this.$store.commit(StoreKeys.SET_MODAL_OPEN, false);
this.$emit('closeEditMenu'); this.$emit('closeEditMenu');
@ -89,6 +97,10 @@ export default {
&:not(:last-child) { &:not(:last-child) {
border-bottom: 1px dotted var(--config-settings-color); border-bottom: 1px dotted var(--config-settings-color);
} }
.input-container input.input-field {
font-size: 1rem;
padding: 0.35rem 0.5rem;
}
} }
} }

View File

@ -8,7 +8,12 @@ import filterUserSections from '@/utils/CheckSectionVisibility';
Vue.use(Vuex); Vue.use(Vuex);
const { UPDATE_CONFIG, SET_MODAL_OPEN, SET_LANGUAGE } = Keys; const {
UPDATE_CONFIG,
SET_MODAL_OPEN,
SET_LANGUAGE,
UPDATE_ITEM,
} = Keys;
const store = new Vuex.Store({ const store = new Vuex.Store({
state: { state: {
@ -54,6 +59,18 @@ const store = new Vuex.Store({
[SET_MODAL_OPEN](state, modalOpen) { [SET_MODAL_OPEN](state, modalOpen) {
state.modalOpen = modalOpen; state.modalOpen = modalOpen;
}, },
[UPDATE_ITEM](state, payload) {
const { itemId, newItem } = payload;
const newConfig = state.config;
newConfig.sections.forEach((section, secIndex) => {
section.items.forEach((item, itemIndex) => {
if (item.id === itemId) {
newConfig.sections[secIndex].items[itemIndex] = newItem;
}
});
});
state.config = newConfig;
},
}, },
actions: { actions: {
/* Called when app first loaded. Reads config and sets state */ /* Called when app first loaded. Reads config and sets state */

View File

@ -3,6 +3,7 @@ const KEY_NAMES = [
'UPDATE_CONFIG', 'UPDATE_CONFIG',
'SET_MODAL_OPEN', 'SET_MODAL_OPEN',
'SET_LANGUAGE', 'SET_LANGUAGE',
'UPDATE_ITEM',
]; ];
// Convert array of key names into an object, and export // Convert array of key names into an object, and export