Skip to content

Commit

Permalink
Merge branch 'feature/delete-account'
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumebriday committed Jun 23, 2018
2 parents 4623b6a + dd8fca7 commit dbfa7c5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
46 changes: 43 additions & 3 deletions src/js/components/Users/Profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<div class="flex-1">
<div class="rounded overflow-hidden bg-white px-6 py-6 shadow-md">
<p class="font-bold text-xl mb-6 pb-6 border-b">Update your profile</p>
<h2 class="font-bold text-xl mb-6 pb-6 border-b">Update your profile</h2>

<form @submit.prevent="update" class="w-full max-w-md" @keydown="form.errors.clear($event.target.name)">
<div class="md:flex md:items-center mb-4">
Expand Down Expand Up @@ -40,6 +40,20 @@
</div>
</div>
</form>

<h2 class="font-bold text-red text-xl mb-6 pb-6 border-b">Delete your account</h2>

<p>Once you delete your account, there is no going back. Please be certain.</p>

<loading-button
:isLoading="isDeleteLoading"
:disabled="isDeleteDisabled"
@click.native.prevent="deleteAccount"
:class="{'opacity-50 cursor-not-allowed' : isDeleteDisabled}"
icon="trash"
class="btn-red mt-4 text-sm">
Delete your account
</loading-button>
</div>
</div>
</div>
Expand All @@ -50,6 +64,7 @@
import Navbar from '@components/Navbar'
import Sidebar from '@components/Users/Sidebar'
import Form from '@utils/Form'
import axios from 'axios'
export default {
components: {
Expand All @@ -63,19 +78,24 @@ export default {
email: '',
name: ''
}),
isLoading: false
isLoading: false,
isDeleteLoading: false
}
},
computed: {
isDisabled () {
if (this.isLoading || this.form.incompleted()) {
if (this.isLoading || this.form.incompleted() || !this.user) {
return true
}
return this.form.email === this.user.email && this.form.name === this.user.name
},
isDeleteDisabled () {
return this.isDeleteLoading || !this.user
},
user () {
return this.$store.state.auth.user
}
Expand Down Expand Up @@ -111,10 +131,30 @@ export default {
},
setForm () {
if (!this.user) {
return false
}
this.form = new Form({
email: this.user.email,
name: this.user.name
})
},
deleteAccount () {
if (this.isDeleteDisabled || !window.confirm('Are you sure ? Your tasks and your account will be deleted forever.')) {
return false
}
this.isDeleteLoading = true
axios.delete('/users/' + this.user.id)
.then(() => {
this.$store.dispatch('logout')
})
.catch(() => {
this.isDeleteLoading = false
})
}
},
Expand Down
6 changes: 5 additions & 1 deletion src/js/store/modules/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const mutations = {

[types.LOGOUT] (state) {
state.logged = false
state.user = null

window.localStorage.removeItem('token')
window.localStorage.removeItem('userId')
Expand Down Expand Up @@ -56,14 +57,17 @@ const actions = {
})
},

login ({ commit }, data) {
login ({ commit, dispatch }, data) {
commit(types.LOGIN, data)

dispatch('fetchUser')

router.push({name: 'TaskList', params: { status: 'active' }})
},

logout ({ commit }) {
commit(types.LOGOUT)
commit('clearTasks')

router.push({name: 'Login'})
}
Expand Down
4 changes: 4 additions & 0 deletions src/js/store/modules/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const mutations = {
removeTask (state, task) {
let taskId = task.id
state.all.splice(state.all.findIndex(task => task.id === taskId), 1)
},

clearTasks (state) {
state.all = []
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/styles/components/_buttons.sass
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
&:hover
@apply .bg-indigo-dark

.btn-red
@apply .bg-red .text-white .font-bold .py-2 .px-4 .rounded

&:hover
@apply .bg-red-dark

.pill-active
@apply .text-indigo .border-b-2 .border-indigo
Expand Down

0 comments on commit dbfa7c5

Please sign in to comment.