Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 71ce50e

Browse files
committed
Split success and failure handling functions
1 parent 436d5e8 commit 71ce50e

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/index.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ async function check(autoCheckElement: AutoCheckElement) {
160160
method: 'POST',
161161
body
162162
})
163-
processResponse(response, input, autoCheckElement.required)
163+
if (response.status === 200) {
164+
processSuccess(response, input, autoCheckElement.required)
165+
} else {
166+
processFailure(response, input, autoCheckElement.required)
167+
}
164168
state.controller = null
165169
input.dispatchEvent(new CustomEvent('auto-check-complete', {bubbles: true}))
166170
} catch (error) {
@@ -171,22 +175,21 @@ async function check(autoCheckElement: AutoCheckElement) {
171175
}
172176
}
173177

174-
function processResponse(response: Response, input: HTMLInputElement, required: boolean) {
175-
if (response.status === 200) {
176-
if (required) {
177-
input.setCustomValidity('')
178-
}
179-
input.dispatchEvent(
180-
new CustomEvent('auto-check-success', {
181-
bubbles: true,
182-
detail: {
183-
response: response.clone()
184-
}
185-
})
186-
)
187-
return
178+
function processSuccess(response: Response, input: HTMLInputElement, required: boolean) {
179+
if (required) {
180+
input.setCustomValidity('')
188181
}
182+
input.dispatchEvent(
183+
new CustomEvent('auto-check-success', {
184+
bubbles: true,
185+
detail: {
186+
response: response.clone()
187+
}
188+
})
189+
)
190+
}
189191

192+
function processFailure(response: Response, input: HTMLInputElement, required: boolean) {
190193
let message = 'Validation failed'
191194
const setValidity = text => (message = text)
192195
input.dispatchEvent(

0 commit comments

Comments
 (0)