aboutsummaryrefslogtreecommitdiff
path: root/editor/actions/add_page.ml
blob: fcad87f4cbdacc8a1a7a7ba95fd339266893f39b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Js = Js_of_ocaml.Js

module M = struct

  type t = Forms.Add_page.t

  let key_of_title
    : Jstr.t -> Jstr.t
    = fun title ->
      title

  let update
    : t -> State.t -> State.t
    = fun {title} state ->
      let page_id = key_of_title title in
      State.new_page ~title (Some page_id) state
end

(** Create a new element *)
let create
  : unit -> State.event Note.event
  = fun () ->
    let title = Jstr.v "Nouvelle page" in
    let form = Forms.Add_page.create () in

    let valid_on = Note.S.map
        (fun Forms.Add_page.{title} -> not @@ Jstr.equal Jstr.empty title)
        (fst form)
    in
    let ev = Elements.Popup.create
        ~title
        ~form
        ~valid_on
        ()
    in
    Note.E.map
      (fun v -> State.E
          (v
          , (module M : State.Event with type t = M.t )))
      (* Option.on_some trigger the event only when the pop up is validated.
         Closing the popup doesn't do anything.
      *)
      (Note.E.Option.on_some ev)