Added data mutator to form processing. This allows conversion of data between server and a component (TreeTable in our case).

This commit is contained in:
Tomas Bures 2017-06-06 00:24:39 +02:00
parent 5e4c86f626
commit 61893d77f6
2 changed files with 37 additions and 15 deletions

View file

@ -282,7 +282,7 @@ function withForm(target) {
});
};
inst.populateFormValuesFromURL = function(url) {
inst.getFormValuesFromURL = async function(url, mutator) {
setTimeout(() => {
this.setState(previousState => {
if (previousState.formState.get('state') === FormState.Loading) {
@ -293,16 +293,35 @@ function withForm(target) {
});
}, 500);
axios.get(url).then(response => {
const data = response.data;
const response = await axios.get(url)
data.originalHash = data.hash;
delete data.hash;
const data = response.data;
this.populateFormValues(data);
});
data.originalHash = data.hash;
delete data.hash;
if (mutator) {
mutator(data);
}
this.populateFormValues(data);
};
inst.validateAndPutFormValuesToURL = async function(url, mutator) {
if (this.isFormWithoutErrors()) {
const data = this.getFormValues();
if (mutator) {
mutator(data);
}
await axios.put(`/namespaces/rest/namespaces/${this.nsId}`, data);
} else {
this.showFormValidation();
}
};
inst.populateFormValues = function(data) {
this.setState(previousState => ({
formState: previousState.formState.withMutations(state => {