Skip to content

Commit

Permalink
Move Actions out of Dokusho element
Browse files Browse the repository at this point in the history
  • Loading branch information
RawToast committed Mar 25, 2018
1 parent 4522ba0 commit b1cfbb0
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 67 deletions.
3 changes: 1 addition & 2 deletions dokusho/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"rationale": "^0.1.3",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"reason-scripts": "0.8.0",
"refetch": "glennsl/refetch"
"reason-scripts": "0.8.0"
},
"scripts": {
"start": "react-scripts start",
Expand Down
30 changes: 30 additions & 0 deletions dokusho/src/app/Actions.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
open Client;
open Types;

module Actions = {
let addNewEntry = (pageType, count) => ReasonReact.SideEffects(
( self =>
Js.Promise.(
Client.newEntry(Types.testUser, pageType, count)
|> then_((serverResponse: serverResponse) => {
self.send(UpdateHistory(serverResponse.readingHistory.days));
resolve(serverResponse);
}))
|> ignore
)
);

let loadUserData = (userId) => ReasonReact.SideEffects(
(self =>
Js.Promise.(
Client.userHistory(userId)
|> then_((serverResponse: serverResponse) => {
if(List.length(serverResponse.readingHistory.days) != 0) {
self.send(UpdateHistory(serverResponse.readingHistory.days))
};
resolve(serverResponse);
}))
|> ignore
)
);
};
19 changes: 10 additions & 9 deletions dokusho/src/app/Client.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

open Types;

type serverResponse = {
userId: string,
readingHistory: readingHistory
};

module Client = {
type serverResponse = {
userId: string,
readingHistory: readingHistory
};
let parseResponse = (json: Js.Json.t) : serverResponse =>

let backendURI = "http://35.189.70.144:8080";
let jsonHeader = Fetch.HeadersInit.make({"Content-Type": "application/json"});
let parseResponse = (json: Js.Json.t) => {
Json.Decode.{
userId: json |> field("userId", string),
readingHistory: json |> field("readingHistory", Decoders.parseHistory)
};
let backendURI = "http://35.189.70.144:8080";
/* let backendURI = "http://localhost:8080"; */
let jsonHeader = Fetch.HeadersInit.make({"Content-Type": "application/json"});

};
/* Fetches the given user's reading history */
let userHistory = (userId:string) =>
Js.Promise.(
Expand Down
1 change: 0 additions & 1 deletion dokusho/src/app/Day.re
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@ module Day {
|> List.map(i => float_of_int(i.value) *. PageType.pageScore(i.kind))
|> List.fold_left((a, b) => a +. b, 0.);


let entries = (d) => d.entries
}
41 changes: 5 additions & 36 deletions dokusho/src/app/Dokusho.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ open Entry;
open Types;
open Day;
open PageTypeSelection;
open Client;
open Actions;

module Dokusho {
let component = ReasonReact.reducerComponent("Dokusho");
Expand All @@ -20,44 +20,14 @@ module Dokusho {
| ChangeSelection(pageType) =>
ReasonReact.Update({readingData: readingData, selectedEntry: pageType});
| AddEntry(pageType, count) =>
ReasonReact.SideEffects(
( self =>
Js.Promise.(
Client.newEntry(Types.testUser, pageType, count)
|> then_((serverResponse: Client.serverResponse) => {
self.send(UpdateHistory(serverResponse.readingHistory.days));
resolve(serverResponse);
}))
|> ignore
)
);
Actions.addNewEntry(pageType, count);
| UpdateHistory(days) =>
ReasonReact.Update({readingData: {days: days}, selectedEntry: selectedEntry});
| LoadUserData(userId) =>
ReasonReact.SideEffects(
(self =>
Js.Promise.(
Client.userHistory(userId)
|> then_((serverResponse: Client.serverResponse) => {
self.send(UpdateHistory(serverResponse.readingHistory.days));
resolve(serverResponse);
}))
|> ignore
)
);
Actions.loadUserData(userId);
},
didMount: (_self) => {
ReasonReact.SideEffects(
(self =>
Js.Promise.(
Client.userHistory(Types.testUser)
|> then_((serverResponse: Client.serverResponse) => {
self.send(UpdateHistory(serverResponse.readingHistory.days));
resolve(serverResponse);
}))
|> ignore
)
);
Actions.loadUserData(testUser);
},
render: (self) => {
let pageCount = Day.pageCount(List.hd(self.state.readingData.days));
Expand All @@ -82,6 +52,5 @@ module Dokusho {
</div>
</div>
}
};

};
}
6 changes: 3 additions & 3 deletions dokusho/src/app/Types.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* TODO: Remove this for real authentication */
let testUser = "fully";

type pageType =
| Manga
| News
Expand Down Expand Up @@ -36,7 +39,6 @@ type action =

module Decoders = {
let parsePageType = (asString:string) => {
Js.Console.log("Got type " ++ asString);
switch (asString) {
| "Manga" => Manga
| "News" => News
Expand Down Expand Up @@ -118,5 +120,3 @@ module Encoders = {
])
);
};

let testUser = "fully";
18 changes: 18 additions & 0 deletions dokusho/src/test/Decoder_test.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
open Jest;
open Types;

describe("Decoders", () => {
open Expect;

describe("Decoders.parsePageType", () => {

test("Can parse all Page Types", () => {
expect("Book" |> Decoders.parsePageType) |> toBe(Book) |> ignore;
expect("Lyric" |> Decoders.parsePageType) |> toBe(Lyric) |> ignore;
expect("Manga" |> Decoders.parsePageType) |> toBe(Manga) |> ignore;
expect("Net" |> Decoders.parsePageType) |> toBe(Net) |> ignore;
expect("News" |> Decoders.parsePageType) |> toBe(News) |> ignore;
expect("Else" |> Decoders.parsePageType) |> toBe(Book);
});
});
});
2 changes: 1 addition & 1 deletion dokusho/src/test/Input_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open Input;
describe("Input", () => {
open Expect;

describe("Input.str", () => {
describe("Input.render", () => {
test("Is an alias for React", () =>
expect(Input.str("Hello")) |> toBe(ReasonReact.stringToElement("Hello")));
});
Expand Down
15 changes: 0 additions & 15 deletions dokusho/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
version "1.1.3"
resolved "https://registry.yarnpkg.com/@glennsl/bs-json/-/bs-json-1.1.3.tgz#492e0336777087356968ae4d57515365d6a18b38"

"@glennsl/rebase@^0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@glennsl/rebase/-/rebase-0.2.0.tgz#dc1d99296dd7f7c47c90b5a4518dd598a71bedfa"

abab@^1.0.3, abab@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/abab/-/abab-1.0.4.tgz#5faad9c2c07f60dd76770f71cf025b62a63cfd4e"
Expand Down Expand Up @@ -1225,10 +1221,6 @@ bs-fetch@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/bs-fetch/-/bs-fetch-0.2.1.tgz#165bed867009f2e91efb959ca1c0ff940d8cd19a"

bs-fetch@reasonml-community/bs-fetch#next:
version "0.2.0"
resolved "https://codeload.github.com/reasonml-community/bs-fetch/tar.gz/e4d3fb4751ace0f5d64713f9c947fe62790c67c8"

bs-jest@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/bs-jest/-/bs-jest-0.2.0.tgz#62576e00c35f069d7681b9bb9bfd744dc7197eb7"
Expand Down Expand Up @@ -6050,13 +6042,6 @@ reduce-function-call@^1.0.1:
dependencies:
balanced-match "^0.4.2"

refetch@glennsl/refetch:
version "0.1.0"
resolved "https://codeload.github.com/glennsl/refetch/tar.gz/c33aadd461bf32a520c282d3444f2602affe4401"
dependencies:
"@glennsl/rebase" "^0.2.0"
bs-fetch reasonml-community/bs-fetch#next

regenerate@^1.2.1:
version "1.3.3"
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.3.3.tgz#0c336d3980553d755c39b586ae3b20aa49c82b7f"
Expand Down

0 comments on commit b1cfbb0

Please sign in to comment.