Created
November 15, 2017 03:14
-
-
Save tiensonqin/110349a7490112ed25d450c5a79e1973 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
open Antd | |
open Form | |
open ApolloClient | |
type action = | |
| EditEmail of string | |
| EditPassword of string | |
| Submit | |
type state = { | |
email: string; | |
password: string;} | |
let component = ReasonReact.reducerComponent "Login" | |
let make' onSubmit props _children = | |
{ | |
component with | |
initialState = (fun () -> { email = ""; password = "" }); | |
reducer = | |
(fun action -> | |
fun state -> | |
match action with | |
| ((EditEmail (value))[@explicit_arity ]) -> | |
((ReasonReact.Update ({ state with email = value }))[@explicit_arity | |
]) | |
| ((EditPassword (value))[@explicit_arity ]) -> | |
((ReasonReact.Update ({ state with password = value })) | |
[@explicit_arity ]) | |
| Submit -> | |
((ReasonReact.UpdateWithSideEffects | |
({ email = ""; password = "" }, | |
((fun _self -> onSubmit ((## props signin), state))))) | |
[@explicit_arity ])); | |
render = | |
(fun self -> | |
let userIcon = | |
((Icon.createElement ~type_:"user" | |
~style:([%bs.obj { fontSize = 13 }]) ~children:[] ())[@JSX ]) in | |
let passwordIcon = | |
((Icon.createElement ~type_:"lock" | |
~style:([%bs.obj { fontSize = 13 }]) ~children:[] ())[@JSX ]) in | |
((Form.createElement ~onSubmit:(self.reduce (fun _e -> Submit)) | |
~children:[((Item.createElement | |
~children:[((Input.createElement ~id:"email" | |
~prefix:userIcon | |
~placeholder:"Email" | |
~onChange:(self.reduce | |
(fun e -> | |
((EditEmail | |
((Util.ev e))) | |
[@explicit_arity ]))) | |
~value:((self.state).email) | |
~children:[] ())[@JSX ])] ()) | |
[@JSX ]); | |
((Item.createElement | |
~children:[((Input.createElement ~id:"password" | |
~prefix:passwordIcon | |
~type_:"password" | |
~placeholder:"Password" | |
~value:((self.state).password) | |
~onChange:(self.reduce | |
(fun e -> | |
((EditPassword | |
((Util.ev e))) | |
[@explicit_arity ]))) | |
~children:[] ())[@JSX ])] ()) | |
[@JSX ]); | |
((Button.createElement ~type_:"primary" | |
~htmlType:"submit" ~className:"login-form-button" | |
~children:[Util.s2e "Log in"] ())[@JSX ]); | |
((Item.createElement ~children:[] ())[@JSX ])] ()) | |
[@JSX ])) | |
} | |
let signinMutation = | |
((gql | |
{| | |
mutation Signin($email: String!, $password: String!) { | |
EmailSignin(email: $email, password: $password) { | |
accessToken | |
refreshToken | |
} | |
} | |
|}) | |
[@bs ]) | |
let make ~onSubmit _children = | |
let wrapper = Form.create () in | |
let reactClass' = | |
ReasonReact.wrapReasonForJs ~component | |
(fun props -> make' onSubmit props [||]) in | |
let reactClass = ((wrapper reactClass')[@bs ]) in | |
let options: gqlOptions = [{ name = "signin"; gql = signinMutation }] in | |
wrapGqlsReactClass ~reactClass ~children:[||] options |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment