Commit 053073f8 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

Refactored rendering

parent fbddb6104ffe
......@@ -19,7 +19,7 @@ let component = ReasonReact.reducerComponent("Page");
let rec getTarget = path =>
switch (path) {
| [] => ""
| [first] => first
| [first] => "/" ++ first
| [first, ...others] => first ++ "/" ++ getTarget(others)
};
......@@ -41,17 +41,21 @@ let fetchSchema = path =>
|> catch(_err => resolve(None))
);
let doFetchSchema = (self: ReasonReact.self('state, 'retainedProps, 'action), path: list(string)) =>
Js.Promise.(
fetchSchema(path)
|> then_(result =>
switch (result) {
| Some(schema) => resolve(self.send(LoadedSchema(schema)))
| None => resolve(self.send(LoadingFailed))
}
)
|> ignore
);
let doFetchSchema =
(
self: ReasonReact.self('state, 'retainedProps, 'action),
path: list(string),
) =>
Js.Promise.(
fetchSchema(path)
|> then_(result =>
switch (result) {
| Some(schema) => resolve(self.send(LoadedSchema(schema)))
| None => resolve(self.send(LoadingFailed))
}
)
|> ignore
);
let fetchData = path =>
Js.Promise.(
......@@ -75,34 +79,46 @@ let fetchData = path =>
let doFetchData = (self: ReasonReact.self(_, _, _), path: list(string)) =>
Js.Promise.(
fetchData(path)
|> then_(result =>
switch (result) {
| Some(data) => resolve(self.send(LoadedData(data)))
| None => resolve(self.send(LoadingFailed))
}
)
|> ignore
);
|> then_(result =>
switch (result) {
| Some(data) => resolve(self.send(LoadedData(data)))
| None => resolve(self.send(LoadingFailed))
}
)
|> ignore
);
let onSchemaLoaded = (self: ReasonReact.self(_, _, _), path: list(string), schema: Hypermedia.schemaType) =>
switch schema {
let onSchemaLoaded =
(
self: ReasonReact.self(_, _, _),
path: list(string),
schema: Hypermedia.schemaType,
) =>
switch (schema) {
| Hypermedia.ArrayType(_) => doFetchData(self, path)
| Hypermedia.ObjectType(_) => doFetchData(self, path)
| _ => ()
}
};
let goto = (_event, _self, link) => ReasonReact.Router.push(link);
let renderLoadedInitialSchema = (self: ReasonReact.self(_, _, _), schema: Hypermedia.schemaType) =>
switch schema {
| Hypermedia.ArrayType(_) => <div> (ReasonReact.string("Loading")) </div>
| Hypermedia.ObjectType(_) => <div> (ReasonReact.string("Loading")) </div>
| Hypermedia.RootType(rootSchema) => Root.renderRoot(self, goto, rootSchema)
| Hypermedia.PrimitiveType(ptype) => <div> (ReasonReact.string(ptype)) </div>
}
let renderLoadedInitialSchema =
(self: ReasonReact.self(_, _, _), schema: Hypermedia.schemaType) =>
switch (schema) {
| Hypermedia.ArrayType(_) => <div> (ReasonReact.string("Loading")) </div>
| Hypermedia.ObjectType(_) => <div> (ReasonReact.string("Loading")) </div>
| Hypermedia.RootType(rootSchema) =>
Root.renderRoot(self, goto, rootSchema)
| Hypermedia.PrimitiveType(ptype) =>
<div> (ReasonReact.string(ptype)) </div>
};
let renderLoadedWithData =
(self: ReasonReact.self(_, _, _), schema: Hypermedia.schemaType, data: Hypermedia.entityValue) =>
(
self: ReasonReact.self(_, _, _),
schema: Hypermedia.schemaType,
data: Hypermedia.entityValue,
) =>
<div />;
let make = _children => {
......@@ -124,7 +140,7 @@ let make = _children => {
| LoadedSchema(schema) =>
switch (state) {
| Loading(path) =>
ReasonReact.Update(LoadedInitialSchema(path, schema));
ReasonReact.Update(LoadedInitialSchema(path, schema))
| _ => ReasonReact.Update(Failed)
}
| LoadedData(value) =>
......@@ -137,14 +153,17 @@ let make = _children => {
},
didUpdate: oldNewSelf =>
switch (oldNewSelf.oldSelf.state, oldNewSelf.newSelf.state) {
| (Loading(_), LoadedInitialSchema(path, schema)) => onSchemaLoaded(oldNewSelf.newSelf, path, schema)
| (Loading(_), LoadedInitialSchema(path, schema)) =>
onSchemaLoaded(oldNewSelf.newSelf, path, schema)
| _ => ()
},
render: self =>
switch (self.state) {
| Loading(_) => <div> (ReasonReact.string("Loading")) </div>
| Failed => <div> (ReasonReact.string("Failed")) </div>
| LoadedInitialSchema(_, schema) => renderLoadedInitialSchema(self, schema)
| LoadedWithData(_, schema, data) => renderLoadedWithData(self, schema, data)
| LoadedInitialSchema(_, schema) =>
renderLoadedInitialSchema(self, schema)
| LoadedWithData(_, schema, data) =>
renderLoadedWithData(self, schema, data)
},
};
\ No newline at end of file
......@@ -136,7 +136,7 @@ module Decode = struct
;;
let rec entity json = {
properties = (Json.Decode.field "properties" entityProperties json);
properties = entityProperties json;
}
and entityProperties json = begin
let source = (Obj.magic (json : Js.Json.t) : Js.Json.t Js.Dict.t) in
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment