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

182 lines
7.5 KiB
Vue
Raw Normal View History

<template>
<div class="section" v-if="twofaccountExists">
<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">{{ $t('twofaccounts.forms.edit_account') }}</h1>
<form @submit.prevent="updateAccount">
<div class="field">
<label class="label">{{ $t('twofaccounts.service') }}</label>
<div class="control">
<input class="input" type="text" :placeholder="$t('twofaccounts.forms.service.placeholder')" v-model="twofaccount.service" autofocus />
2020-01-02 00:09:19 +01:00
</div>
<p class="help is-danger" v-if="validationErrors.service">{{ validationErrors.service.toString() }}</p>
2020-01-02 00:09:19 +01:00
</div>
<div class="field">
<label class="label">{{ $t('twofaccounts.account') }}</label>
<div class="control">
<input class="input" type="text" :placeholder="$t('twofaccounts.forms.account.placeholder')" v-model="twofaccount.account" />
</div>
<p class="help is-danger" v-if="validationErrors.account">{{ validationErrors.account.toString() }}</p>
</div>
<div class="field">
<label class="label">{{ $t('twofaccounts.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">{{ $t('twofaccounts.forms.choose_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>
<p class="help is-danger help-for-file" v-if="validationErrors.icon">{{ validationErrors.icon.toString() }}</p>
<div class="field is-grouped">
<div class="control">
<button type="submit" class="button is-link">{{ $t('twofaccounts.forms.save') }}</button>
</div>
<div class="control">
<button class="button is-text" @click.prevent="cancelCreation">{{ $t('commons.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
},
twofaccountExists: false,
tempIcon: '',
validationErrors: {}
}
},
created: function() {
this.getAccount();
},
methods: {
getAccount: function () {
axios.get('/api/twofaccounts/' + this.$route.params.twofaccountId).then(response => {
this.twofaccount = response.data
this.twofaccountExists = true
2020-01-08 23:22:51 +01:00
// set account icon as temp icon
this.tempIcon = this.twofaccount.icon
})
.catch(error => {
if( error.response.status === 404 ) {
this.$router.push({ name: '404' });
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
}
});
},
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
axios.put('/api/twofaccounts/' + this.$route.params.twofaccountId, this.twofaccount)
.then(response => {
this.$router.push({name: 'accounts', params: { InitialEditMode: true }});
})
.catch(error => {
if (error.response.status === 404) {
this.$router.push({ name: '404' });
2020-01-10 08:35:15 +01:00
}
else if( error.response.data.validation ) {
this.validationErrors = error.response.data.validation
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
}
});
},
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) {
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-05 23:21:28 +01:00
let imgdata = new FormData();
imgdata.append('icon', this.$refs.iconInput.files[0]);
2020-01-05 23:21:28 +01:00
let config = {
header : {
'Content-Type' : 'multipart/form-data',
}
}
axios.post('/api/icon/upload', imgdata, config)
.then(response => {
2020-01-05 23:21:28 +01:00
console.log('icon path > ', response);
2020-01-08 23:22:51 +01:00
this.tempIcon = response.data;
this.validationErrors['icon'] = '';
})
.catch(error => {
if( error.response.data.validation ) {
this.validationErrors = error.response.data.validation
}
else {
this.$router.push({ name: 'genericError', params: { err: error.response.data.message } });
}
});
2020-01-08 16:14:25 +01:00
},
deleteIcon(event) {
if( this.tempIcon && this.tempIcon !== this.twofaccount.icon ) {
axios.delete('/api/icon/delete/' + this.tempIcon)
.then(response => {
2020-01-08 23:22:51 +01:00
this.tempIcon = ''
}
)
}
this.tempIcon = ''
},
2020-01-08 16:14:25 +01:00
},
}
</script>