aboutsummaryrefslogtreecommitdiff
path: root/editor/actions/import.ml
blob: 13ed9382576d267cac9fe716da3771743c57d41e (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module Js = Js_of_ocaml.Js

let uncheck_import =
  fun ~previous ~update ->
  let _ = previous
  and _ = update in
  true

let check_import =
  fun ~previous ~update ->
  Js.Opt.case previous##.date
    (fun () -> true)
    (fun previous_date ->
       Js.Opt.case update##.date
         (fun () -> true)
         (fun update_date ->
            update_date >= previous_date ))

module M = struct

  type t = Forms.Selector.t

  let update
    : t -> State.t -> State.t
    = fun t state ->
      match t.Forms.Selector.file with
      | None -> state
      | Some file ->
        let content = file.Elements.Input.content in
        let check =
          if t.Forms.Selector.preserve_newest then
            check_import
          else
            uncheck_import
        in
        match
          State.Storage.of_json
            ~check
            content with
        | Error _ -> state
        | Ok _obj ->
          State.load_page state.State.page_id state
end

let create () =
  let title = Jstr.v "Importer des notes" in
  let form = Forms.Selector.create () in
  let ev = Elements.Popup.create
      ~title
      ~form
      ~valid_on:(Note.S.map (fun form -> form.Forms.Selector.file != None) (fst form))
      ()
  in

  Note.E.map
    (fun v -> State.E
        ( v
        , (module M : State.Event with type t = M.t )))
    (Note.E.Option.on_some ev)