-
-
Save ipiyer/c4ef611994b4d6550c80 to your computer and use it in GitHub Desktop.
FRP username validation
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
// username is a input element in the dom | |
let userNameVal = Kefir.fromEvents(username, "blur") | |
.map(e => $(e.target) | |
.val()); | |
let kUsernameValidation = username => { | |
try { | |
if (usernameValidation(username)) { | |
return username; | |
} | |
} catch (e) { | |
return Kefir.constantError(e.message); | |
} | |
} | |
let validUsername = userNameVal | |
.flatMapLatest(kUsernameValidation) | |
.onError(function(error) { | |
showErrors(helpText, error); // How do i end the stream here? is there a better way to write this? | |
}); | |
let usernameAvail = validUsername | |
.flatMapLatest(isUserNameTaken) // does xhr to server | |
.map(function(value) { | |
hideErrors(helpText); | |
// username exists; | |
if (value) { | |
showInputUnAvailable(username, "username is already taken.") | |
} else { | |
showInputAvailable(username); | |
} | |
return value; | |
}).onError(function() { | |
window.alert("something went wrong"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment