diff --git a/src/admin/App.vue b/src/admin/App.vue index b8476df..efc1637 100644 --- a/src/admin/App.vue +++ b/src/admin/App.vue @@ -5,10 +5,13 @@ main.content-container keep-alive router-view - + toast() \ No newline at end of file diff --git a/src/admin/store/index.js b/src/admin/store/index.js index f9e0f76..c5450a1 100644 --- a/src/admin/store/index.js +++ b/src/admin/store/index.js @@ -4,12 +4,14 @@ import auth from './modules/auth' import works from './modules/works' import reviews from './modules/reviews' import categories from './modules/categories' +import toast from './modules/toast' Vue.use(Vuex) export default new Vuex.Store({ modules: { auth, works, + toast, reviews, categories } diff --git a/src/admin/store/modules/categories.js b/src/admin/store/modules/categories.js index c8c14d2..7fb4659 100644 --- a/src/admin/store/modules/categories.js +++ b/src/admin/store/modules/categories.js @@ -1,4 +1,5 @@ import axios from '../../customAxios' +import generateError from '../../utils/generateError' export default { namespaced: true, @@ -58,7 +59,7 @@ export default { const { data } = await axios.get(`/categories/${userId}`) commit('setCategories', data) } catch (error) { - console.log(error) + generateError(error) } }, @@ -66,9 +67,12 @@ export default { try { const { data } = await axios.post('/categories', { title }) commit('addCategory', data) + commit('toast/showToast', + { type: 'success', message: 'Категория успешно добавлена' }, + { root: true } + ) } catch (error) { - throw new Error(error) - console.log(error) + generateError(error) } }, @@ -76,8 +80,12 @@ export default { try { const { data } = await axios.post(`/categories/${category.id}`, { title: category.category }) commit('editCategory', data.category) + commit('toast/showToast', + { type: 'success', message: 'Категория успешно обновлена' }, + { root: true } + ) } catch (error) { - console.log(error) + generateError(error) } }, @@ -85,8 +93,12 @@ export default { try { await axios.delete(`/categories/${categoryId}`) commit('removeCategory', categoryId) + commit('toast/showToast', + { type: 'success', message: 'Категория успешно удалена' }, + { root: true } + ) } catch (error) { - console.log(error) + generateError(error) } }, @@ -94,26 +106,38 @@ export default { try { const { data } = await axios.post('/skills', skill); commit('addSkill', data); + commit('toast/showToast', + { type: 'success', message: 'Скилл успешно добавлен' }, + { root: true } + ) } catch (error) { - console.log(error) + generateError(error) } }, - async deleteSkill ({ commit }, skill) { + async updateSkill ({ commit }, skill) { try { - await axios.delete(`/skills/${ skill.id }`); - commit('removeSkill', skill); + const { data } = await axios.post(`/skills/${ skill.id }`, skill); + commit('editSkill', data.skill); + commit('toast/showToast', + { type: 'success', message: 'Скилл успешно обновлен' }, + { root: true } + ) } catch (error) { - console.log(error) + generateError(error) } }, - async updateSkill ({ commit }, skill) { + async deleteSkill ({ commit }, skill) { try { - const { data } = await axios.post(`/skills/${ skill.id }`, skill); - commit('editSkill', data.skill); + await axios.delete(`/skills/${ skill.id }`); + commit('removeSkill', skill); + commit('toast/showToast', + { type: 'success', message: 'Скилл успешно удален' }, + { root: true } + ) } catch (error) { - console.log(error) + generateError(error) } } } diff --git a/src/admin/store/modules/reviews.js b/src/admin/store/modules/reviews.js index 16ac762..972aaf1 100644 --- a/src/admin/store/modules/reviews.js +++ b/src/admin/store/modules/reviews.js @@ -1,5 +1,6 @@ import axios from '../../customAxios' import formData from '../../utils/formData' +import generateError from '../../utils/generateError' export default { namespaced: true, @@ -43,21 +44,24 @@ export default { const { data } = await axios.get(`/reviews/${userId}`) commit('setReviews', data) } catch (error) { - console.log(error) + generateError(error) } }, async saveReview ({ commit }, review) { try { const { data } = await axios.post( - '/reviews', + '/reivews', formData(review), { headers: { 'Content-Type': 'multipart/form-data' } } ) commit('addReview', data) + commit('toast/showToast', + { type: 'success', message: 'Отзыв успешно добавлен' }, + { root: true } + ) } catch (error) { - throw new Error(error) - console.log(error) + generateError(error) } }, @@ -69,8 +73,12 @@ export default { { headers: { 'Content-Type': 'multipart/form-data' } } ) commit('editReview', data.review) + commit('toast/showToast', + { type: 'success', message: 'Отзыв успешно обновлен' }, + { root: true } + ) } catch (error) { - console.log(error) + generateError(error) } }, @@ -78,8 +86,12 @@ export default { try { await axios.delete(`/reviews/${reviewId}`) commit('removeReview', reviewId) + commit('toast/showToast', + { type: 'success', message: 'Отзыв успешно удален' }, + { root: true } + ) } catch (error) { - console.log(error) + generateError(error) } } } diff --git a/src/admin/store/modules/toast.js b/src/admin/store/modules/toast.js new file mode 100644 index 0000000..8a0a8e3 --- /dev/null +++ b/src/admin/store/modules/toast.js @@ -0,0 +1,24 @@ +export default { + namespaced: true, + state: { + showed: false, + message: 'Сообщение успешно отправленно', + type: 'success' + }, + mutations: { + setVisibility (state, value) { + state.showed = value + }, + setMessage (state, message) { + state.message = message + }, + setType (state, type) { + state.type = type + }, + showToast (state, { type, message }) { + state.showed = true + state.message = message + state.type = type + } + } +} \ No newline at end of file diff --git a/src/admin/store/modules/works.js b/src/admin/store/modules/works.js index 6c923f3..e6b1258 100644 --- a/src/admin/store/modules/works.js +++ b/src/admin/store/modules/works.js @@ -43,7 +43,7 @@ export default { const { data } = await axios.get(`/works/${userId}`) commit('setWorks', data) } catch (error) { - console.log(error) + generateError(error) } }, @@ -55,9 +55,12 @@ export default { { headers: { 'Content-Type': 'multipart/form-data' } } ) commit('addWork', data) + commit('toast/showToast', + { type: 'success', message: 'Работа успешно добавлена' }, + { root: true } + ) } catch (error) { - throw new Error(error) - console.log(error) + generateError(error) } }, @@ -69,8 +72,12 @@ export default { { headers: { 'Content-Type': 'multipart/form-data' } } ) commit('editWork', data.work) + commit('toast/showToast', + { type: 'success', message: 'Работа успешно обновлена' }, + { root: true } + ) } catch (error) { - console.log(error) + generateError(error) } }, @@ -78,8 +85,12 @@ export default { try { await axios.delete(`/works/${workId}`) commit('removeWork', workId) + commit('toast/showToast', + { type: 'success', message: 'Работа успешно удалена' }, + { root: true } + ) } catch (error) { - console.log(error) + generateError(error) } } } diff --git a/src/admin/utils/generateError.js b/src/admin/utils/generateError.js new file mode 100644 index 0000000..1df9440 --- /dev/null +++ b/src/admin/utils/generateError.js @@ -0,0 +1,23 @@ +export default (error) => { + const errorResponseObject = error.response.data; + + if (errorResponseObject.message) { + switch (errorResponseObject.message) { + case "The given data was invalid.": + throw new Error("Ошибка валидации данных на сервере"); + default: + throw new Error(errorResponseObject.message); + } + } + + if (errorResponseObject.error) { + switch (errorResponseObject.error) { + case "token_not_provided": + throw new Error("Токен авторизации не предоставлен"); + case "token_expired": + throw new Error("Токен авторизации просрочен"); + default: + throw new Error(errorResponseObject.error); + } + } +}; \ No newline at end of file