summaryrefslogtreecommitdiff
path: root/bs5/universal/native/shared/NoteEditor.re
blob: 265a5ae06dc93f9f9cabc479973070d052b28cd3 (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
60
61
62
63
64
65
66
67
68
69
70
[@warning "-26-27-32"];

open Melange_json.Primitives;

[@react.client.component]
let make =
    (~noteId: option(int), ~initialTitle: string, ~initialBody: string) => {
  let router: ClientRouter.t = ClientRouter.useRouter();
  let (title, setTitle) = RR.useStateValue(initialTitle);
  let (body, setBody) = RR.useStateValue(initialBody);
  let (isNavigating, startNavigating) = React.useTransition();

  let%browser_only onChangeTitle = e => {
    let newValue = React.Event.Form.target(e)##value;
    setTitle(newValue);
  };

  let%browser_only onChangeBody = e => {
    let newValue = React.Event.Form.target(e)##value;
    setBody(newValue);
  };

  <div className="flex flex-col gap-4">
    <form
      className="flex flex-col gap-2"
      autoComplete="off"
      onSubmit={e => React.Event.Form.preventDefault(e)}>
      <InputText value=title onChange=onChangeTitle />
      <Textarea rows=10 value=body onChange=onChangeBody />
    </form>
    <div className="flex flex-col gap-4">
      <div className="flex flex-row gap-2" role="menubar">
        <button
          className=Theme.button
          disabled=isNavigating
          onClick=[%browser_only
            _ => {
              let action =
                switch (noteId) {
                | Some(id) =>
                  ServerFunctions.Notes.edit.call(~id, ~title, ~content=body)
                | None =>
                  ServerFunctions.Notes.create.call(~title, ~content=body)
                };

              action
              |> Js.Promise.then_((result: Note.t) => {
                   let id = result.id;
                   router.navigate({
                     selectedId: Some(id),
                     isEditing: false,
                     searchText: None,
                   });
                   Js.Promise.resolve();
                 })
              |> ignore;
            }
          ]
          role="menuitem">
          {React.string("Done")}
        </button>
        {switch (noteId) {
         | Some(id) => <DeleteNoteButton noteId=id />
         | None => React.null
         }}
      </div>
      <NotePreview key="note-preview" body />
    </div>
  </div>;
};