open Brr module Js = Js_of_ocaml.Js type events = | StoreEvent | LoadEvent of Jstr.t option | ClosePopup of Forms.Events.event option | Generic of Actions.Event.t let key_of_title : Jstr.t -> Jstr.t = fun title -> title (** [update] is the event loop. The function take a new event, and apply it to the current state. *) let update : (events, State.t) Application.t = fun event state -> match event with | Generic (E (t, (module EventHandler))) -> EventHandler.apply t state | ClosePopup result -> (* First close the last popin. *) let state = match state.window with | [] -> { state with window = [] } | el::tl -> El.remove el ; { state with window = tl } in (* Call the handler associated with the event *) begin match result with | None -> state | Some (Event (t, (module Handler))) -> Handler.on_close t state end | StoreEvent -> let title_element = Document.find_el_by_id G.document (Jstr.v "title") in let content = Option.map (fun el -> El.prop (El.Prop.value) el) title_element in let new_date = (new%js Js.date_now)##getTime in let content_obj = object%js val content = Js.some @@ Jv.Id.to_jv (state.view##.state##toJSON ()) val title = Js.Opt.option content val date = Js.some new_date end in let save = State.Storage.save content_obj state.page_id ~check:(fun previous_state -> Js.Opt.case previous_state##.date (fun () -> true) (fun date -> (* I do not figure how the previous date could be older than the last backup. It could be either : - equal (if we are the only one to update it) - more recent (if the content has been updated elsewhere) but older shoud be a bug. *) let is_ok = date <= state.last_backup in if (not is_ok) then ( let open Console in log [ Jstr.v "Last backup date is " ; new%js Js.date_fromTimeValue state.last_backup ; Jstr.v " but date is " ; new%js Js.date_fromTimeValue date] ); is_ok)) in begin match save with | Ok true -> { state with last_backup = new_date } | Ok false -> Console.(log [Jstr.v "Didn't save"]); state | Error other -> (* TODO In case of error, notify the user *) Console.(log [Jstr.v "Couldn't save" ; other]); state end | LoadEvent page_id -> let json = State.Storage.load page_id in State.load_page page_id state json