Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
feat: App state token is now option(token)
Browse files Browse the repository at this point in the history
  • Loading branch information
CamiloGarciaLaRotta committed Feb 18, 2019
1 parent 4b88f98 commit 6638062
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
22 changes: 15 additions & 7 deletions src/App.re
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
type state = {token: string};
type state = {token: option(string)};

type action =
| Login(string)
| Logout;

let reducer = (action, _state) =>
switch (action) {
| Login(jwt) => ReasonReact.Update({token: jwt})
| Logout => ReasonReact.Update({token: ""})
| Login(jwt) => ReasonReact.Update({token: Js.Option.some(jwt)})
| Logout => ReasonReact.Update({token: None})
};

let component = ReasonReact.reducerComponent("App");

let make = _children => {
...component,
reducer,
initialState: () => {token: ""},
initialState: () => {token: None},
render: self =>
<div className="app">
(
(self.state.token === "") ?
<Login updateToken=(token => self.send(Login(token))) />
:
switch (self.state.token) {
| None =>
<Login updateToken=(
token =>
switch (token) {
| Some(token) => self.send(Login(token))
| None => self.send(Logout)
}
) />
| Some(_token) =>
<JobApp
submitHandler=(_event => self.send(Logout))
signOutHandler=(_event => self.send(Logout))
/>
}
)
</div>,
};
2 changes: 1 addition & 1 deletion src/Login.re
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ let make = (~updateToken, _children) => {
Services.authenticate(
~email=state.email,
~password=state.password,
~success=updateToken,
~callback=updateToken,
~failure=()=>self.send(DisplayError)
)
();
Expand Down
5 changes: 3 additions & 2 deletions src/Services.re
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Decode = {
};
};

let authenticate = (~email, ~password, ~success, ~failure, _self) => {
let authenticate = (~email, ~password, ~callback, ~failure, _self) => {
let authEndpoint = "https://jobhub-authentication-staging.herokuapp.com/login";

let payload = Js.Dict.empty();
Expand All @@ -41,11 +41,12 @@ let authenticate = (~email, ~password, ~success, ~failure, _self) => {
|> then_(json =>
json
|> Decode.authResponse
|> resp => success(resp.token)
|> resp => callback(Js.Option.some(resp.token))
|> resolve
)
|> catch(err => {
Js.log(err);
callback(None);
failure();
resolve();
})
Expand Down

0 comments on commit 6638062

Please sign in to comment.