Skip to content

Commit

Permalink
Add Toasts with throwing messages on actions and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Dual-Ice committed May 24, 2020
1 parent 48cd948 commit f08a0b1
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 51 deletions.
5 changes: 4 additions & 1 deletion src/admin/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
main.content-container
keep-alive
router-view

toast()
</template>
<script>
export default {
components: {
toast: () => import('./components/partial/Toast')
}
}
</script>
<style lang="postcss">
Expand Down
51 changes: 35 additions & 16 deletions src/admin/components/About/SkillGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
placeholder="Название новой группы"
:errorText="validationMessage('tmpGroup', 'category')"
)
.add__form-btns.add__form-btns--colored
.add__form-btns.add__form-btns--colored(:class="{ 'blocked': isBlocked }")
CardBtn(
icon="confirm"
type="submit"
Expand Down Expand Up @@ -61,13 +61,16 @@
placeholder="100 %"
:errorText="validationMessage('newSkill', 'percent')"
)
AddBtn(type="submit")
AddBtn(
:class="{ 'blocked': isBlocked }"
type="submit"
)
</template>
<script>
import Skill from './Skill'
import { mapActions } from 'vuex';
import AddBtn from '../partial/AddBtn'
import CardBtn from '../partial/CardBtn'
import { mapActions, mapMutations } from 'vuex'
import CustomInput from '../partial/CustomInput'
import { required, minLength, numeric, maxValue } from 'vuelidate/lib/validators'
export default {
Expand Down Expand Up @@ -95,7 +98,8 @@ export default {
title: '',
percent: '',
category: 0
}
},
isBlocked: false
}
},
Expand Down Expand Up @@ -152,6 +156,8 @@ export default {
]
),
...mapMutations('toast', ['showToast']),
switchEdit () {
this.editMode = !this.editMode
this.$emit('hide');
Expand Down Expand Up @@ -188,27 +194,40 @@ export default {
async saveGroup () {
this.$v.tmpGroup.$touch()
if (!this.$v.tmpGroup.$error) {
if (this.tmpGroup.category !== this.skillGroup.category) {
this.tmpGroup.id
? await this.updateCategory(this.tmpGroup)
: await this.saveCategory(this.tmpGroup.category)
try {
this.isBlocked = true
if (this.tmpGroup.category !== this.skillGroup.category) {
this.tmpGroup.id
? await this.updateCategory(this.tmpGroup)
: await this.saveCategory(this.tmpGroup.category)
}
this.switchEdit()
} catch ({message}) {
this.showToast( { type: 'error', message });
} finally {
this.isBlocked = false
}
this.switchEdit()
}
},
async addSkill () {
this.$v.newSkill.$touch()
if (!this.$v.newSkill.$error) {
this.newSkill.category = this.tmpGroup.id;
await this.saveSkill(this.newSkill);
this.newSkill = {
title: '',
percent: '',
category: 0
try {
this.isBlocked = true
await this.saveSkill(this.newSkill);
this.newSkill = {
title: '',
percent: '',
category: 0
}
this.$v.newSkill.$reset()
} catch ({message}) {
this.showToast( { type: 'error', message });
} finally {
this.isBlocked = false
}
this.$v.newSkill.$reset()
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/admin/components/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<script>
import Icon from './partial/Icon'
import axios from '../customAxios'
import { mapMutations } from 'vuex'
import CustomInput from './partial/CustomInput'
import { required, minLength } from 'vuelidate/lib/validators'
Expand Down Expand Up @@ -60,16 +61,19 @@ export default {
},
methods: {
...mapMutations('toast', ['showToast']),
async login() {
this.$v.$touch()
if (!this.$v.$error) {
try {
const response = await axios.post('/login', this.user)
const token = response.data.token
localStorage.setItem('user-token', token)
await this.$router.replace('/')
this.$router.replace('/')
} catch (error) {
console.log(error)
const message = error.response.data.error || error.response.data.message
this.showToast( { type: 'error', message });
}
}
},
Expand Down
12 changes: 9 additions & 3 deletions src/admin/components/Reviews/ReviewEdit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
).form__btn.form__btn--big {{btnTitle}}
</template>
<script>
import { mapActions } from 'vuex'
import Icon from '../partial/Icon'
import { mapActions, mapMutations } from 'vuex'
import CustomInput from '../partial/CustomInput'
import InputTooltip from '../partial/InputTooltip'
import { required, minLength } from 'vuelidate/lib/validators'
Expand Down Expand Up @@ -142,6 +142,7 @@ export default {
methods: {
...mapActions('reviews', ['saveReview', 'updateReview']),
...mapMutations('toast', ['showToast']),
appendFileAndRenderPhoto (e) {
this.tmpReview.photo = e.target.files[0]
Expand All @@ -153,7 +154,12 @@ export default {
this.image = reader.result
}
} catch (error) {
console.log(error)
this.showToast(
{
type: 'error',
message: 'Ошибка при чтении файла'
}
)
}
},
Expand All @@ -177,7 +183,7 @@ export default {
this.hide()
} catch (error) {
console.log(error)
this.showToast( { type: 'error', message });
} finally {
this.isBlocked = false
}
Expand Down
14 changes: 10 additions & 4 deletions src/admin/components/Works/WorkEdit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
).form__btn.form__btn--big {{btnTitle}}
</template>
<script>
import { mapActions } from 'vuex'
import Icon from '../partial/Icon'
import { mapActions, mapMutations } from 'vuex'
import CustomInput from '../partial/CustomInput'
import InputTooltip from '../partial/InputTooltip'
import { required, minLength, url } from 'vuelidate/lib/validators'
Expand Down Expand Up @@ -178,6 +178,7 @@ export default {
methods: {
...mapActions('works', ['saveWork', 'updateWork']),
...mapMutations('toast', ['showToast']),
showInputFile () {
document.querySelector("#upload-pic").click()
Expand All @@ -198,7 +199,12 @@ export default {
this.image = reader.result
}
} catch (error) {
console.log(error)
this.showToast(
{
type: 'error',
message: 'Ошибка при чтении файла'
}
)
}
},
Expand All @@ -217,8 +223,8 @@ export default {
}
this.hide()
} catch (error) {
console.log(error)
} catch ({message}) {
this.showToast( { type: 'error', message });
} finally {
this.isBlocked = false
}
Expand Down
95 changes: 95 additions & 0 deletions src/admin/components/partial/Toast.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<template lang="pug">
.toast-container(:class="{ showed: showed }")
.toast(:class="'toast--' + type")
.toast__text {{ message }}
button(@click="setVisibility(false)").toast__close
</template>
<script>
import { mapState, mapMutations } from 'vuex';
export default {
computed: {
...mapState(
'toast',
[
'type',
'showed',
'message'
]
)
},
watch: {
showed (value) {
if (value) {
let timeout
clearTimeout(timeout)
timeout = setTimeout(() => this.setVisibility(false), 3000)
}
}
},
methods: {
...mapMutations('toast', ['setVisibility'])
}
}
</script>
<style lang="postcss">
@import '../../../styles/mixins.pcss';
.toast {
&-container {
width: 100%;
display: flex;
justify-content: center;
position: fixed;
bottom: -100%;
left: 50%;
transform: translateX(-50%);
visibility: hidden;
transition: 0.3s;
&.showed {
bottom: 0;
visibility: visible;
}
}
color: #fff;
display: flex;
align-items: center;
justify-content: space-between;
position: relative;
width: 100%;
max-width: 400px;
background: #4bb133;
padding: 28px 20px 28px 30px;
@include phones {
max-width: 100%;
}
&__close {
}
&--warning {
background: #b18333;
}
&--error {
background: #b13333;
}
}
.toast__text {
font-size: 18px;
font-weight: 600;
}
.toast__close {
width: 20px;
height: 20px;
background: svg-load('cross.svg', fill=#fff) center center no-repeat;
cursor: pointer;
margin-left: 20px;
}
</style>
2 changes: 2 additions & 0 deletions src/admin/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit f08a0b1

Please sign in to comment.