Skip to content

Commit

Permalink
feat: Better user experience when importing buckets
Browse files Browse the repository at this point in the history
Instead of being redirected to an empty page the error is now shown on
the same page and the buckets list reloaded
  • Loading branch information
johan-bjareholt committed Jul 5, 2020
1 parent 1d3bfd1 commit d59cf32
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/views/Buckets.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ div

b-card-group.deck
b-card(header="Import buckets")
form(method="post", :action="$aw.baseURL + '/api/0/import'", enctype="multipart/form-data")
input(type="file", name="buckets.json")
input(type="submit", value="Import")
b-alert(v-if="import_error" show variant="danger" dismissable)
| {{ import_error }}
b-form-file(v-model="import_file"
placeholder="Choose a file or drop file here..."
drop-placeholder="Drop file here...")
// TODO: This spinner could be placed in a more suitable place
div(v-if="import_file" class="spinner-border" role="status")
span
| A valid file to import is a JSON file from either an export of a single bucket or an export from multiple buckets.
| If there are buckets with the same name the import will fail
Expand Down Expand Up @@ -105,6 +109,8 @@ export default {
name: 'Buckets',
data() {
return {
import_file: null,
import_error: null,
delete_bucket_selected: null,
fields: [
{ key: 'id', label: 'Bucket ID', sortable: true },
Expand All @@ -119,6 +125,26 @@ export default {
return _.orderBy(this.$store.state.buckets.buckets, [b => b.id], ['asc']);
},
},
watch: {
import_file: async function(_new_value, _old_value) {
if (this.import_file != null) {
console.log('Importing file');
try {
await this.importBuckets(this.import_file);
console.log('Import successful');
this.import_error = null;
} catch (err) {
console.log('Import failed');
// TODO: Make aw-server report error message so it can be shown in the web-ui
this.import_error = 'Import failed, see aw-server logs for more info';
}
// We need to reload buckets even if we fail because imports can be partial
// (first bucket succeeds, second fails for example when importing multiple)
await this.$store.dispatch('buckets/loadBuckets');
this.import_file = null;
}
},
},
mounted: async function() {
await this.$store.dispatch('buckets/ensureBuckets');
},
Expand All @@ -131,6 +157,12 @@ export default {
await this.$store.dispatch('buckets/deleteBucket', { bucketId });
this.$root.$emit('bv::hide::modal', 'delete-modal');
},
importBuckets: async function(importFile) {
const formData = new FormData();
formData.append('buckets.json', importFile);
const headers = { 'Content-Type': 'multipart/form-data' };
return this.$aw.req.post('/0/import', formData, { headers });
},
},
};
</script>

0 comments on commit d59cf32

Please sign in to comment.