Commit 9ccea60f authored by Laurent Wouters's avatar Laurent Wouters
Browse files

Correct rendering of collections

parent 5934e2e5574a
......@@ -20,7 +20,7 @@ let rec getTarget = path =>
switch (path) {
| [] => ""
| [first] => "/" ++ first
| [first, ...others] => first ++ "/" ++ getTarget(others)
| [first, ...others] => "/" ++ first ++ getTarget(others)
};
let fetchSchema = path =>
......
......@@ -58,13 +58,13 @@ let renderCollection =
data
|> List.map((entity: Hypermedia.entityValue) => {
let obj = Hypermedia.as_object(entity);
let id = Hypermedia.as_string(Hypermedia.get(obj, "id"));
let link_target = Hypermedia.produce(link_href, obj);
let title = Hypermedia.as_string(Hypermedia.get(obj, "title"));
<li>
<a
href=link_href
href=link_target
onClick=(
self.handle((event, self) => goto(event, self, link_href))
self.handle((event, self) => goto(event, self, link_target))
)>
(ReasonReact.string(title))
</a>
......
......@@ -176,4 +176,34 @@ module Decode = struct
else
ValueObject(entity json)
end;;
end
let produce template entity = begin
let re = [%re "/\{([a-zA-Z_][a-zA-Z0-9_]*)\}/g"] in
let break = ref false in
let last = ref 0 in
let result = ref "" in
while not !break do
match re |> Js.Re.exec template with
| None -> begin
break := true;
let len = String.length template - !last in
let sub = String.sub template !last len in
result := !result ^ sub
end
| Some mr ->
let maybe_id = Js.toOption (Js.Re.captures mr).(1) in
let start = Js.Re.index mr in
let next = Js.Re.lastIndex re in
let len = start - !last in
let sub = String.sub template !last len in
let value = match maybe_id with
| None -> ""
| Some id -> as_string (get entity id)
in begin
result := !result ^ sub ^ value;
last := next
end
done;
!result
end
\ No newline at end of file
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