Created
July 10, 2018 16:12
-
-
Save ng-marcus/67729c2464d06c6ec2ef99fb96a6bfa9 to your computer and use it in GitHub Desktop.
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
type pages = | |
| GroupList | |
| DocumentList(string) | |
| OAUthCallback; | |
type actions = | |
| SetToken(string) | |
| ChangeFolder(string) | |
| ChangeRoute(pages) | |
| ParseToken(string) | |
| CaptureInitUrl(ReasonReact.Router.url); | |
type state = { | |
token: option(string), | |
path: string, | |
initUrl: option(ReasonReact.Router.url), | |
nowShowing: pages, | |
}; | |
let findCurrentView = (url: ReasonReact.Router.url) : pages => { | |
Js.log("find current view"); | |
switch (url.path) { | |
| ["callback"] => OAUthCallback | |
| ["sites", id] => DocumentList(id) | |
| [] => GroupList | |
| _ => GroupList | |
}; | |
}; | |
let component = ReasonReact.reducerComponent("App"); | |
let handleClick = (_event, _self) => Js.log("clicked!"); | |
let make = _children => { | |
...component, | |
initialState: () => { | |
token: None, | |
path: "Shared Documents", | |
initUrl: None, | |
nowShowing: GroupList, | |
}, | |
didMount: self => { | |
let url = ReasonReact.Router.dangerouslyGetInitialUrl(); | |
self.send(CaptureInitUrl(url)); | |
let handleGetCurrentUrl = route => { | |
Js.log("handleGetCurrentUrl"); | |
Js.log(route); | |
self.send(ChangeRoute(route)); | |
}; | |
/* decide which route here */ | |
findCurrentView(url) |> handleGetCurrentUrl; | |
}, | |
reducer: (action, state) => | |
switch (action) { | |
| ParseToken(search_string) => | |
Js.log("parse callback" ++ search_string); | |
ReasonReact.NoUpdate; | |
| SetToken(token) => ReasonReact.Update({...state, token: Some(token)}) | |
| ChangeFolder(name) => | |
ReasonReact.Update({...state, path: state.path ++ "/" ++ name}) | |
| ChangeRoute(route) => | |
Js.log("change to route"); | |
ReasonReact.Update({...state, nowShowing: route}); | |
| CaptureInitUrl(url) => | |
ReasonReact.Update({...state, initUrl: Some(url)}) | |
}, | |
subscriptions: self => [ | |
Sub( | |
() => | |
ReasonReact.Router.watchUrl(url => { | |
Js.log("watcher"); | |
Js.log(url.path); | |
switch (url.path) { | |
| ["callback"] => self.send(ParseToken(url.search)) | |
| ["sites", id] => self.send(ChangeRoute(DocumentList(id))) | |
| [] => self.send(ChangeRoute(GroupList)) | |
| _ => self.send(ChangeRoute(GroupList)) | |
}; | |
}), | |
ReasonReact.Router.unwatchUrl, | |
), | |
], | |
render: self => { | |
let tokenAdded = token => self.send(SetToken(token)); | |
let changeFolder = name => self.send(ChangeFolder(name)); | |
Js.log("now showing is "); | |
Js.log(self.state.nowShowing); | |
switch (self.state.token) { | |
| None => <div> <TokenLoad setToken=tokenAdded /> </div> | |
| Some(token) => | |
let main_panel = | |
switch (self.state.nowShowing) { | |
| GroupList => <Groups token /> | |
| DocumentList(path) => | |
<div> | |
<Files token path=self.state.path changeFolder /> | |
<CreateFile token path=self.state.path /> | |
</div> | |
| OAUthCallback => <div> (ReasonReact.string("Callback")) </div> | |
| _ => <Groups token /> | |
}; | |
<div> | |
<div> (ReasonReact.string("Token Set to " ++ token)) </div> | |
main_panel | |
</div>; | |
}; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment