Skip to content

Commit

Permalink
Empty history on error
Browse files Browse the repository at this point in the history
  • Loading branch information
RawToast committed Apr 2, 2018
1 parent 46b60a5 commit 09aa39d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
Empty file modified bin/gcloud-images.sh
100644 → 100755
Empty file.
3 changes: 2 additions & 1 deletion dokusho/src/App.re
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ let mapUrlToRoute = (url: ReasonReact.Router.url) => {
Routes.Home;
}
| ["callback"] => {
LoginButton.Auth.handleAuth(url);
ReasonReact.Router.push("/");
Routes.Home;
}
| _ => {
Routes.Home;
Expand Down
19 changes: 14 additions & 5 deletions dokusho/src/app/Client.re
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module Client = {
open Dom.Storage;

let accessToken = () => localStorage |> getItem("accessToken") |> Rationale.Option.default("no_token");

let backendURI = "http://35.189.70.144:8080";
let jsonHeader = Fetch.HeadersInit.make({"Content-Type": "application/json"});
let authHeader = () => Fetch.HeadersInit.makeWithArray([|( "Content-Type", "application/json" ), ( "accessToken", accessToken() )|]);
Expand All @@ -22,17 +21,27 @@ module Client = {
readingHistory: json |> field("readingHistory", Decoders.parseHistory)
};
};
/* Fetches the given user's reading history */
let userHistory = (_userId:string) => {

let parseResponseT = (jString) =>
Json.parse(jString)
|> Rationale.Option.default(Json.parseOrRaise("{\"userId\": \"userId\",\"readingHistory\": {\"days\": [] } }"))
|> json =>
Json.Decode.{
userId: json |> field("userId", string),
readingHistory: json |> field("readingHistory", Decoders.parseHistory)
};

/* Fetches the given user's reading history, or an empty one */
let userHistory: string => Js.Promise.t(serverResponse) = (_userId:string) => {
Js.Console.log("Get history " ++ accessToken());
Js.Promise.(
Fetch.fetchWithInit(backendURI ++ "/history",
Fetch.RequestInit.make(
~method_=Get,
~headers=authHeader(),
()))
|> then_(Fetch.Response.json)
|> then_(resp => resp |> parseResponse |> resolve)
|> then_(Fetch.Response.text)
|> then_(resp => resp |> parseResponseT |> resolve)
);
};

Expand Down
3 changes: 0 additions & 3 deletions dokusho/src/app/LoginButton.re
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ module Auth {
};
};

open Types;
open Dom.Storage;

let handleAuth = (url: ReasonReact.Router.url) => {
Expand All @@ -48,8 +47,6 @@ module Auth {
localStorage |> setItem("accessToken", accessToken);
localStorage |> setItem("id_token", idToken);
localStorage |> setItem("expiresIn", expiresIn);

Routes.Home;
};

let getIdToken = () => localStorage |> getItem("id_token") |> resolveOption;
Expand Down

0 comments on commit 09aa39d

Please sign in to comment.