Created
December 4, 2018 10:55
-
-
Save veekthoven/82fbec6c12025c381c7b595f8e97a08f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Errors from './Errors.js' | |
class Form { | |
/** | |
* Create a new Form instance. | |
* | |
* @param {object} data | |
*/ | |
constructor(data) { | |
this.originalData = data; | |
this.submitting = false; | |
for (let field in data) { | |
this[field] = data[field]; | |
} | |
this.errors = new Errors(); | |
} | |
/** | |
* Fetch all relevant data for the form. | |
*/ | |
data() { | |
let data = {}; | |
for (let property in this.originalData) { | |
data[property] = this[property]; | |
} | |
return data; | |
} | |
/** | |
* Determine if the form has any errors. | |
*/ | |
hasError(field = null) { | |
if (field) { | |
return this.errors.has(field); | |
} | |
return this.errors.any(); | |
} | |
/** | |
* Retrieve the error message for a field. | |
* | |
* @param {string} field | |
*/ | |
getError(field) { | |
return this.errors.get(field); | |
} | |
/** | |
* Clear one or all error fields. | |
* | |
* @param {string|null} field | |
*/ | |
clearErrors() { | |
this.errors.clear(); | |
} | |
/** | |
* Reset the form fields. | |
*/ | |
reset() { | |
for (let field in this.originalData) { | |
this[field] = ''; | |
} | |
this.submitting = false; | |
this.errors.clear(); | |
} | |
/** | |
* Send a POST request to the given URL. | |
* . | |
* @param {string} url | |
*/ | |
post(url) { | |
return this.submit('post', url); | |
} | |
/** | |
* Send a PUT request to the given URL. | |
* . | |
* @param {string} url | |
*/ | |
put(url) { | |
return this.submit('put', url); | |
} | |
/** | |
* Send a PATCH request to the given URL. | |
* . | |
* @param {string} url | |
*/ | |
patch(url) { | |
return this.submit('patch', url); | |
} | |
/** | |
* Send a DELETE request to the given URL. | |
* . | |
* @param {string} url | |
*/ | |
delete(url) { | |
return this.submit('delete', url); | |
} | |
/** | |
* Submit the form. | |
* | |
* @param {string} requestType | |
* @param {string} url | |
*/ | |
submit(requestType, url) { | |
this.submitting = true; | |
return new Promise((resolve, reject) => { | |
axios[requestType](url, this.data()) | |
.then(response => { | |
this.onSuccess(response.data); | |
resolve(response.data); | |
}) | |
.catch(error => { | |
this.onFail(error.response.data); | |
reject(error.response.data); | |
}); | |
}); | |
} | |
/** | |
* Handle a successful form submission. | |
* | |
* @param {object} data | |
*/ | |
onSuccess(data) { | |
this.reset(); | |
} | |
/** | |
* Handle a failed form submission. | |
* | |
* @param {object} errors | |
*/ | |
onFail(errors) { | |
this.submitting = false; | |
this.errors.record(errors.errors); | |
} | |
} | |
export default Form |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment