laravel-precognition
Advanced tools
Comparing version 0.5.2 to 0.5.3
@@ -27,7 +27,14 @@ import debounce from 'lodash.debounce'; | ||
let validating = false; | ||
/** | ||
* Set the validating inputs. | ||
* | ||
* Returns an array of listeners that should be invoked once all state | ||
* changes have taken place. | ||
*/ | ||
const setValidating = (value) => { | ||
if (value !== validating) { | ||
validating = value; | ||
listeners.validatingChanged.forEach(callback => callback()); | ||
return listeners.validatingChanged; | ||
} | ||
return []; | ||
}; | ||
@@ -38,2 +45,8 @@ /** | ||
let validated = []; | ||
/** | ||
* Set the validated inputs. | ||
* | ||
* Returns an array of listeners that should be invoked once all state | ||
* changes have taken place. | ||
*/ | ||
const setValidated = (value) => { | ||
@@ -43,4 +56,5 @@ const uniqueNames = [...new Set(value)]; | ||
validated = uniqueNames; | ||
listeners.validatedChanged.forEach(callback => callback()); | ||
return listeners.validatedChanged; | ||
} | ||
return []; | ||
}; | ||
@@ -55,2 +69,8 @@ /** | ||
let touched = []; | ||
/** | ||
* Set the touched inputs. | ||
* | ||
* Returns an array of listeners that should be invoked once all state | ||
* changes have taken place. | ||
*/ | ||
const setTouched = (value) => { | ||
@@ -60,4 +80,5 @@ const uniqueNames = [...new Set(value)]; | ||
touched = uniqueNames; | ||
listeners.touchedChanged.forEach(callback => callback()); | ||
return listeners.touchedChanged; | ||
} | ||
return []; | ||
}; | ||
@@ -68,2 +89,8 @@ /** | ||
let errors = {}; | ||
/** | ||
* Set the input errors. | ||
* | ||
* Returns an array of listeners that should be invoked once all state | ||
* changes have taken place. | ||
*/ | ||
const setErrors = (value) => { | ||
@@ -73,9 +100,16 @@ const prepared = toValidationErrors(value); | ||
errors = prepared; | ||
listeners.errorsChanged.forEach(callback => callback()); | ||
return listeners.errorsChanged; | ||
} | ||
return []; | ||
}; | ||
/** | ||
* Forget the given input's errors. | ||
* | ||
* Returns an array of listeners that should be invoked once all state | ||
* changes have taken place. | ||
*/ | ||
const forgetError = (name) => { | ||
const newErrors = { ...errors }; | ||
delete newErrors[resolveName(name)]; | ||
setErrors(newErrors); | ||
return setErrors(newErrors); | ||
}; | ||
@@ -138,4 +172,6 @@ /** | ||
onValidationError: (response, axiosError) => { | ||
setValidated([...validated, ...validate]); | ||
setErrors(merge(omit({ ...errors }, validate), response.data.errors)); | ||
[ | ||
...setValidated([...validated, ...validate]), | ||
...setErrors(merge(omit({ ...errors }, validate), response.data.errors)), | ||
].forEach(listener => listener()); | ||
return config.onValidationError | ||
@@ -146,6 +182,9 @@ ? config.onValidationError(response, axiosError) | ||
onSuccess: () => { | ||
setValidated([...validated, ...validate]); | ||
setValidated([...validated, ...validate]).forEach(listener => listener()); | ||
}, | ||
onPrecognitionSuccess: (response) => { | ||
setErrors(omit({ ...errors }, validate)); | ||
[ | ||
...setValidated([...validated, ...validate]), | ||
...setErrors(omit({ ...errors }, validate)), | ||
].forEach(listener => listener()); | ||
return config.onPrecognitionSuccess | ||
@@ -171,7 +210,7 @@ ? config.onPrecognitionSuccess(response) | ||
onStart: () => { | ||
setValidating(true); | ||
setValidating(true).forEach(listener => listener()); | ||
(config.onStart ?? (() => null))(); | ||
}, | ||
onFinish: () => { | ||
setValidating(false); | ||
setValidating(false).forEach(listener => listener()); | ||
oldTouched = validatingTouched; | ||
@@ -198,3 +237,3 @@ oldData = validatingData; | ||
if (get(oldData, name) !== value) { | ||
setTouched([name, ...touched]); | ||
setTouched([name, ...touched]).forEach(listener => listener()); | ||
} | ||
@@ -225,3 +264,3 @@ if (touched.length === 0) { | ||
: [resolveName(input)]; | ||
setTouched([...touched, ...inputs]); | ||
setTouched([...touched, ...inputs]).forEach(listener => listener()); | ||
return form; | ||
@@ -234,7 +273,7 @@ }, | ||
setErrors(value) { | ||
setErrors(value); | ||
setErrors(value).forEach(listener => listener()); | ||
return form; | ||
}, | ||
forgetError(name) { | ||
forgetError(name); | ||
forgetError(name).forEach(listener => listener()); | ||
return form; | ||
@@ -244,3 +283,3 @@ }, | ||
if (names.length === 0) { | ||
setTouched([]); | ||
setTouched([]).forEach(listener => listener()); | ||
} | ||
@@ -255,3 +294,3 @@ else { | ||
}); | ||
setTouched(newTouched); | ||
setTouched(newTouched).forEach(listener => listener()); | ||
} | ||
@@ -258,0 +297,0 @@ return form; |
{ | ||
"name": "laravel-precognition", | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"description": "Laravel Precognition.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
27670
672