2FAuth/resources/js/views/Edit.vue

172 lines
6.4 KiB
Vue
Raw Normal View History

<template>
<div class="section">
<div class="columns is-mobile is-centered">
<div class="column is-two-thirds-tablet is-half-desktop is-one-third-widescreen is-one-quarter-fullhd">
<h1 class="title">Edit account</h1>
<form @submit.prevent="updateAccount">
<div class="field">
<label class="label">Service</label>
<div class="control">
2020-01-08 16:14:25 +01:00
<input class="input" type="text" placeholder="example.com" v-model="twofaccount.service" required autofocus />
2020-01-02 00:09:19 +01:00
</div>
</div>
<div class="field">
<label class="label">Account</label>
<div class="control">
2020-01-08 16:14:25 +01:00
<input class="input" type="text" placeholder="John DOE" v-model="twofaccount.account" />
</div>
</div>
<div class="field">
<label class="label">Icon</label>
<div class="file is-dark">
<label class="file-label">
<input class="file-input" type="file" accept="image/*" v-on:change="uploadIcon" ref="iconInput">
<span class="file-cta">
<span class="file-icon">
<font-awesome-icon :icon="['fas', 'image']" />
</span>
<span class="file-label">Choose an image</span>
</span>
</label>
2020-01-08 23:22:51 +01:00
<span class="tag is-black is-large" v-if="tempIcon">
<img class="icon-preview" :src="'../storage/icons/' + tempIcon" >
<button class="delete is-small" @click.prevent="deleteIcon"></button>
2020-01-08 16:14:25 +01:00
</span>
</div>
</div>
<div class="field is-grouped">
<div class="control">
2020-01-08 16:14:25 +01:00
<button type="submit" class="button is-link">Save</button>
</div>
<div class="control">
2020-01-08 23:22:51 +01:00
<button class="button is-text" @click.prevent="cancelCreation">Cancel</button>
</div>
</div>
</form>
</div>
</div>
2020-01-02 00:09:19 +01:00
</div>
</template>
<script>
export default {
data() {
return {
2020-01-08 16:14:25 +01:00
twofaccount: {
'service' : '',
'account' : '',
'uri' : '',
'icon' : ''
2020-01-08 23:22:51 +01:00
},
tempIcon: ''
}
},
created: function(){
this.getAccount();
},
methods: {
getAccount: function () {
let token = localStorage.getItem('jwt')
axios.defaults.headers.common['Content-Type'] = 'application/json'
axios.defaults.headers.common['Authorization'] = 'Bearer ' + token
axios.get('/api/twofaccounts/' + this.$route.params.twofaccountId).then(response => {
this.twofaccount = response.data
2020-01-08 23:22:51 +01:00
// set account icon as temp icon
this.tempIcon = this.twofaccount.icon
})
},
updateAccount: function() {
2020-01-08 23:22:51 +01:00
// Set new icon and delete old one
if( this.tempIcon !== this.twofaccount.icon ) {
let oldIcon = ''
oldIcon = this.twofaccount.icon
this.twofaccount.icon = this.tempIcon
this.tempIcon = oldIcon
this.deleteIcon()
}
// store the account
let token = localStorage.getItem('jwt')
axios.defaults.headers.common['Content-Type'] = 'application/json'
axios.defaults.headers.common['Authorization'] = 'Bearer ' + token
axios.put('/api/twofaccounts/' + this.$route.params.twofaccountId, this.twofaccount).then(response => {
this.$router.push({name: 'accounts', params: { InitialEditMode: true }});
})
},
2020-01-05 23:21:28 +01:00
2020-01-08 23:22:51 +01:00
cancelCreation: function() {
// clean new temp icon
if( this.tempIcon ) {
this.deleteIcon()
}
this.$router.push({name: 'accounts', params: { InitialEditMode: true }});
},
2020-01-05 23:21:28 +01:00
uploadIcon(event) {
let token = localStorage.getItem('jwt')
axios.defaults.headers.common['Content-Type'] = 'application/json'
axios.defaults.headers.common['Authorization'] = 'Bearer ' + token
2020-01-08 23:22:51 +01:00
let files = this.$refs.iconInput.files
2020-01-05 23:21:28 +01:00
2020-01-08 23:22:51 +01:00
if (!files.length) {
return false;
}
2020-01-08 16:14:25 +01:00
2020-01-08 23:22:51 +01:00
// clean possible tempIcon but keep original one
if( this.tempIcon && this.tempIcon !== this.twofaccount.icon ) {
2020-01-08 16:14:25 +01:00
this.deleteIcon()
2020-01-08 23:22:51 +01:00
}
2020-01-05 23:21:28 +01:00
let imgdata = new FormData();
imgdata.append('icon', files[0]);
let config = {
header : {
'Content-Type' : 'multipart/form-data',
}
}
axios.post('/api/icon/upload', imgdata, config).then(response => {
console.log('icon path > ', response);
2020-01-08 23:22:51 +01:00
this.tempIcon = response.data;
2020-01-05 23:21:28 +01:00
}
)
2020-01-08 16:14:25 +01:00
},
deleteIcon(event) {
2020-01-08 23:22:51 +01:00
if( this.tempIcon !== this.twofaccount.icon ) {
let token = localStorage.getItem('jwt')
2020-01-08 16:14:25 +01:00
2020-01-08 23:22:51 +01:00
axios.defaults.headers.common['Content-Type'] = 'application/json'
axios.defaults.headers.common['Authorization'] = 'Bearer ' + token
2020-01-08 16:14:25 +01:00
2020-01-08 23:22:51 +01:00
axios.delete('/api/icon/delete/' + this.tempIcon).then(response => {
this.tempIcon = ''
}
)
}
this.tempIcon = ''
},
2020-01-08 16:14:25 +01:00
},
}
</script>