Skip to content

Commit 18cc6a7

Browse files
committed
Add update event
1 parent 9ac9662 commit 18cc6a7

File tree

3 files changed

+70
-23
lines changed

3 files changed

+70
-23
lines changed

api/db.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"negara": [
33
{
4-
"id": 0,
5-
"name": "Indonesia"
4+
"name": "Indonesia",
5+
"id": 0
66
},
77
{
88
"id": 1,
@@ -19,6 +19,10 @@
1919
{
2020
"id": 4,
2121
"name": "Arab"
22+
},
23+
{
24+
"name": "Australia",
25+
"id": 5
2226
}
2327
]
2428
}

pages/index.vue

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,27 @@
44
<h2 class="subtitle">Name of Countries</h2>
55
<div class="columns">
66
<div class="column">
7-
<form @submit.prevent="add(name)">
7+
88
<div class="field">
99
<label class="label">Name</label>
1010
<div class="control">
1111
<input type="text" class="input" v-model="name" required>
1212
</div>
1313
</div>
14-
<div class="control">
14+
<div class="control" :class="{hidden: !isHidden}" @click="add(name)">
1515
<button class="button is-link">Submit</button>
1616
</div>
17-
</form>
17+
<div class="control" :class="{hidden: isHidden}" @click="update(name)">
18+
<button class="button is-link">Update</button>
19+
</div>
20+
1821
</div>
1922
<div class="column">
2023
<ul class="panel">
2124
<li class="panel-heading">Countries List</li>
2225
<li class="panel-block" v-for="country in countries" :key="country.id">
2326
{{ country.name }} / &nbsp;
24-
<button class="button is-info">Edit</button> &nbsp;
27+
<button class="button is-info" @click="show(country.id)">Edit</button> &nbsp;
2528
<button class="button is-danger" @click="remove(country.id, country.name)">Delete</button>
2629
</li>
2730
</ul>
@@ -34,19 +37,47 @@
3437
import { mapState, mapActions } from 'vuex'
3538
3639
export default {
37-
computed: mapState([
38-
'countries'
39-
]),
40-
methods: {
41-
add(name) {
42-
this.$store.dispatch('store', name)
43-
this.name = ''
44-
},
45-
remove(id, name) {
46-
if (confirm('Do you want delete ' + name + ' ?')) {
47-
this.$store.dispatch('remove', id)
48-
}
49-
}
50-
}
40+
data: function() {
41+
return {
42+
name: '',
43+
isHidden: true,
44+
activeItem: null
45+
}
46+
},
47+
computed: mapState([
48+
'countries'
49+
]),
50+
methods: {
51+
add(name) {
52+
this.$store.dispatch('store', name)
53+
this.name = ''
54+
},
55+
show(id) {
56+
this.$store.dispatch('show', id).then((res) => {
57+
this.name = res.name
58+
this.activeItem = res
59+
this.isHidden = false
60+
})
61+
},
62+
update() {
63+
this.$store.dispatch('update', {
64+
id: this.activeItem.id,
65+
name: this.name
66+
})
67+
this.name = ''
68+
this.isHidden = true
69+
},
70+
remove(id, name) {
71+
if (confirm('Do you want delete ' + name + ' ?')) {
72+
this.$store.dispatch('remove', id)
73+
}
74+
}
75+
}
5176
}
52-
</script>
77+
</script>
78+
79+
<style type="text/css">
80+
.hidden {
81+
display: none;
82+
}
83+
</style>

store/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ export const mutations = {
1212
state.countries.push(item)
1313
},
1414
removeData(state, id) {
15-
const foundItem = state.countries.findIndex(p => p.id === id)
16-
state.countries.splice(foundItem, 1)
15+
const foundIndex = state.countries.findIndex(p => p.id === id)
16+
state.countries.splice(foundIndex, 1)
17+
},
18+
updateData(state, data) {
19+
const foundItem = state.countries.find(p => p.id === data.id)
20+
foundItem.name = data.name
1721
}
1822
}
1923

@@ -29,5 +33,13 @@ export const actions = {
2933
async remove({commit}, id) {
3034
const res = await axios.delete('negara/' + id)
3135
commit('removeData', id)
36+
},
37+
async show({commit}, id) {
38+
const res = await axios.get('negara/' + id)
39+
return res.data
40+
},
41+
async update({commit}, data) {
42+
const res = await axios.put('negara/' + data.id, {name: data.name})
43+
commit('updateData', data)
3244
}
3345
}

0 commit comments

Comments
 (0)