Skip to content

Commit 6d4cc07

Browse files
authored
Merge pull request #16 from vedovelli/validation
Bugfixes and improvements
2 parents 6d142dd + 44ab2cc commit 6d4cc07

File tree

12 files changed

+298
-217
lines changed

12 files changed

+298
-217
lines changed

client/config/dev.env.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ var merge = require('webpack-merge')
22
var prodEnv = require('./prod.env')
33

44
module.exports = merge(prodEnv, {
5-
NODE_ENV: '"development"'
5+
NODE_ENV: '"development"',
6+
API_URL: '"http://localhost:8000/api"',
67
})

client/config/prod.env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
22
NODE_ENV: '"production"',
3-
API_URL: '"http://localhost:8000/api"',
3+
API_URL: '"https://spa.vedcasts.com.br/api"',
44
}

client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"bootstrap-sass": "^3.3.7",
1818
"font-awesome": "^4.7.0",
1919
"jquery": "^3.1.1",
20+
"lodash": "^4.17.2",
2021
"sweetalert": "^1.1.3",
2122
"vue": "^2.0.1",
2223
"vue-router": "^2.0.1",

client/src/app/categories/form.vue

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,18 @@
4040
isEditing() {
4141
return this.category.id > 0
4242
},
43+
isValid() {
44+
this.resetMessages()
45+
if (this.category.name === '') {
46+
this.setMessage({ type: 'error', message: ['Please fill category name'] })
47+
return false
48+
}
49+
return true
50+
},
4351
},
4452
4553
methods: {
46-
...mapActions(['setFetching', 'setMessage']),
54+
...mapActions(['setFetching', 'resetMessages', 'setMessage']),
4755
4856
/**
4957
* If there's an ID in the route params
@@ -74,14 +82,19 @@
7482
},
7583
submit() {
7684
/**
77-
* Shows the global spinner
85+
* Pre-conditions are met
7886
*/
79-
this.setFetching({ fetching: true })
87+
if (this.isValid) {
88+
/**
89+
* Shows the global spinner
90+
*/
91+
this.setFetching({ fetching: true })
8092
81-
if (this.isEditing) {
82-
this.update()
83-
} else {
84-
this.save()
93+
if (this.isEditing) {
94+
this.update()
95+
} else {
96+
this.save()
97+
}
8598
}
8699
},
87100
save() {
@@ -111,7 +124,7 @@
111124
})
112125
},
113126
update() {
114-
this.$http.put(`categories/${this.category.id}/update`, { category: this.category }).then(() => {
127+
this.$http.put(`categories/${this.category.id}/update`, this.category).then(() => {
115128
/**
116129
* This event will notify the world about
117130
* the category creation. In this case

client/src/app/routes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ import { routes as login } from './login'
44

55
// https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Operators/Spread_operator
66
// Thus a new array is created, containing all objects that match the routes.
7-
export default [...login, ...dashboard, ...categories]
7+
// ...dashboard must be the last one because of the catchall instruction
8+
export default [...login, ...categories, ...dashboard]

client/src/components/general/alerts.vue

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
<script>
33
import { mapState, mapActions } from 'vuex'
4+
import { isEmpty } from 'lodash'
45
56
export default {
67
computed: {
@@ -11,13 +12,18 @@
1112
hasErrorMessages() {
1213
return this.messages.error.length > 0
1314
},
15+
hasValidationMessages() {
16+
return !isEmpty(this.messages.validation)
17+
},
1418
},
1519
methods: {
1620
...mapActions(['setMessage']),
1721
dismiss(type) {
1822
let obj
1923
if (type === 'error') {
2024
obj = { type, message: [] }
25+
} else if (type === 'validation') {
26+
obj = { type, message: {} }
2127
} else {
2228
obj = { type, message: '' }
2329
}
@@ -48,5 +54,15 @@
4854
</ul>
4955
</div>
5056

57+
<!-- Validation messages -->
58+
<div class="alert alert-danger" v-show="hasValidationMessages">
59+
<button type="button" class="close" aria-label="Close" @click="dismiss('validation')">
60+
<span aria-hidden="true">&times;</span>
61+
</button>
62+
<ul>
63+
<li v-for="error in messages.validation">{{ error[0] }}</li>
64+
</ul>
65+
</div>
66+
5167
</div>
5268
</template>

client/src/plugins/http.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios'
2+
import { isArray } from 'lodash'
23
import store from '../store'
34
import router from '../router'
45
import { apiUrl } from '../config'
@@ -30,7 +31,18 @@ http.interceptors.response.use(
3031
if (error.response.data.reason === 'token') {
3132
router.push({ name: 'login.index' })
3233
}
33-
store.dispatch('setMessage', { type: 'error', message: error.response.data.messages })
34+
/**
35+
* Error messages are sent in arrays
36+
*/
37+
if (isArray(error.response.data)) {
38+
store.dispatch('setMessage', { type: 'error', message: error.response.data.messages })
39+
/**
40+
* Laravel generated validation errors are
41+
* sent in an object
42+
*/
43+
} else {
44+
store.dispatch('setMessage', { type: 'validation', message: error.response.data })
45+
}
3446
store.dispatch('setFetching', { fetching: false })
3547
return Promise.reject(error)
3648
}

client/src/store/actions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ export default {
1616

1717
resetMessages({ commit }) {
1818
commit(TYPES.MAIN_SET_MESSAGE, { type: 'success', message: '' })
19-
commit(TYPES.MAIN_SET_MESSAGE, { type: 'error', message: '' })
19+
commit(TYPES.MAIN_SET_MESSAGE, { type: 'error', message: [] })
2020
commit(TYPES.MAIN_SET_MESSAGE, { type: 'warning', message: '' })
21+
commit(TYPES.MAIN_SET_MESSAGE, { type: 'validation', message: {} })
2122
},
2223

2324
setToken({ commit }, token) {

client/src/store/state.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ export default {
55
user: {},
66
messages: {
77
success: '',
8-
error: '',
8+
error: [],
99
warning: '',
10+
validation: {},
1011
},
1112
fetching: false,
1213
}

0 commit comments

Comments
 (0)