Skip to content

Commit

Permalink
Reuse create/updated coherence and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbizarre committed Jan 28, 2019
1 parent c5b485c commit 4ddda3c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 39 deletions.
29 changes: 9 additions & 20 deletions js/components/reuse/form.vue
@@ -1,24 +1,25 @@
<template>
<div>
<vform v-ref:form :fields="fields" :model="reuse"></vform>
<vertical-form v-ref:form :fields="fields" :model="reuse"></vertical-form>
</div>
</template>

<script>
import Reuse from 'models/reuse';
import reuse_types from 'models/reuse_types';
import VerticalForm from 'components/form/vertical-form.vue';
export default {
components: {VerticalForm},
props: {
reuse: {
type: Object,
default: function() {
return new Reuse();
}
},
hideNotifications: false
},
data: function() {
data() {
return {
fields: [{
id: 'title',
Expand All @@ -32,7 +33,7 @@ export default {
label: this._('Type'),
widget: 'select-input',
values: reuse_types,
map: function(item) {
map(item) {
return {value: item.id, text: item.label};
}
}, {
Expand All @@ -48,26 +49,14 @@ export default {
}]
};
},
components: {
vform: require('components/form/vertical-form.vue')
},
methods: {
serialize: function() {
serialize() {
return this.$refs.form.serialize();
},
validate: function() {
const isValid = this.$refs.form.validate();
if (isValid & !this.hideNotifications) {
this.$dispatch('notify', {
autoclose: true,
title: this._('Changes saved'),
details: this._('Your reuse has been updated.')
});
}
return isValid;
validate() {
return this.$refs.form.validate();
},
on_error: function(response) {
on_error(response) {
return this.$refs.form.on_error(response);
},
}
Expand Down
8 changes: 4 additions & 4 deletions js/models/reuse.js
Expand Up @@ -23,17 +23,17 @@ export default class Reuse extends Model {
*/
save(on_error) {
if (this.id) {
return this.update(this, this.on_fetched, on_error);
return this.update(this, on_error);
}
this.$api('reuses.create_reuse', {payload: this}, this.on_fetched, on_error);
}

update(data, on_success, on_error) {
update(data, on_error) {
this.$api('reuses.update_reuse', {
reuse: this.id,
payload: data
}, on_success, on_error);
}, this.on_fetched, on_error);
}
};
}

Reuse.__badges_type__ = 'reuse';
24 changes: 16 additions & 8 deletions js/views/reuse-edit.vue
Expand Up @@ -12,12 +12,12 @@ import Reuse from 'models/reuse';
import FormLayout from 'components/form-layout.vue';
export default {
data: function() {
components: {FormLayout, ReuseForm},
data() {
return {
reuse: new Reuse(),
};
},
components: {FormLayout, ReuseForm},
computed: {
title() {
if (this.reuse.id) {
Expand All @@ -29,23 +29,31 @@ export default {
},
methods: {
save() {
let form = this.$refs.form;
const form = this.$refs.form;
if (form.validate()) {
this.reuse.update(form.serialize(), (response) => {
this.reuse.on_fetched(response);
this.$go({name: 'reuse', params: {oid: this.reuse.id}});
}, form.on_error);
this.reuse.update(form.serialize(), form.on_error);
}
},
on_success() {
this.$dispatch('notify', {
autoclose: true,
title: this._('Changes saved'),
details: this._('Your reuse has been updated.')
});
this.$go({name: 'reuse', params: {oid: this.reuse.id}});
},
cancel() {
this.$go({name: 'reuse', params: {oid: this.reuse.id}});
}
},
route: {
data() {
if (this.$route.params.oid !== this.reuse.id) {
this.reuse.fetch(this.$route.params.oid);
this.$scrollTo(this.$el);
this.reuse.fetch(this.$route.params.oid);
this.reuse.$once('updated', () => {
this.reuse.$once('updated', this.on_success);
});
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions js/views/reuse-wizard.vue
Expand Up @@ -7,7 +7,7 @@ import Reuse from 'models/reuse';
import API from 'api';
export default {
data: function() {
data() {
return {
reuse: new Reuse(),
steps: [{
Expand All @@ -24,6 +24,9 @@ export default {
label: this._('New reuse'),
subtitle: this._('Describe your reuse'),
component: require('components/reuse/form.vue'),
init: (component) => {
this.reuse.$once('updated', this.$refs.wizard.go_next);
},
next: (component) => {
if (component.validate()) {
let data = component.serialize();
Expand All @@ -32,22 +35,19 @@ export default {
}
Object.assign(this.reuse, data);
this.reuse.save(component.on_error);
this.reuse.$once('updated', () => {
this.$refs.wizard.go_next();
});
return false;
}
}
}, {
label: this._('Datasets'),
subtitle: this._('Add some related datasets'),
component: require('components/dataset/cards-form.vue'),
init: (component) => {
this.reuse.$once('updated', this.$refs.wizard.go_next);
},
next: (component) => {
this.reuse.datasets = component.datasets;
this.reuse.save();
this.reuse.$once('updated', () => {
this.$refs.wizard.go_next();
});
return false;
}
}, {
Expand Down

0 comments on commit 4ddda3c

Please sign in to comment.