aboutsummaryrefslogtreecommitdiff
path: root/editor/actions/import.ml
blob: 9539bd3ffcc2f7aae7f514079e4fc37f217ff901 (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
module Js = Js_of_ocaml.Js
module App = Editor_app

let uncheck_import ~previous ~update =
  let _ = previous
  and _ = update in
  true


let check_import ~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 process : 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 -> App.dispatch (module M) v) (Note.E.Option.on_some ev)