forked from reactjs/server-components-demo
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNoteEditor.client.js
More file actions
172 lines (162 loc) · 4.77 KB
/
NoteEditor.client.js
File metadata and controls
172 lines (162 loc) · 4.77 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import {useState, useTransition} from 'react';
import {createFromReadableStream} from 'react-server-dom-webpack/client';
import NotePreview from './NotePreview';
import {useRefresh} from './Cache.client';
import {useLocation} from './LocationContext.client';
export default function NoteEditor({noteId, initialTitle, initialBody}) {
const refresh = useRefresh();
const [title, setTitle] = useState(initialTitle);
const [body, setBody] = useState(initialBody);
const [location, setLocation] = useLocation();
const [isNavigating, startNavigating] = useTransition();
const [isSaving, saveNote] = useMutation({
endpoint: noteId !== null ? `/notes/${noteId}` : `/notes`,
method: noteId !== null ? 'PUT' : 'POST',
});
const [isDeleting, deleteNote] = useMutation({
endpoint: `/notes/${noteId}`,
method: 'DELETE',
});
async function handleSave() {
const payload = {title, body};
const requestedLocation = {
selectedId: noteId,
isEditing: false,
searchText: location.searchText,
};
const response = await saveNote(payload, requestedLocation);
navigate(response);
}
async function handleDelete() {
const payload = {};
const requestedLocation = {
selectedId: null,
isEditing: false,
searchText: location.searchText,
};
const response = await deleteNote(payload, requestedLocation);
navigate(response);
}
function navigate(response) {
const cacheKey = response.headers.get('X-Location');
const nextLocation = JSON.parse(cacheKey);
const seededResponse = createFromReadableStream(response.body);
startNavigating(() => {
refresh(cacheKey, seededResponse);
setLocation(nextLocation);
});
}
const isDraft = noteId === null;
return (
<div className="note-editor">
<form
className="note-editor-form"
autoComplete="off"
onSubmit={(e) => e.preventDefault()}>
<label className="offscreen" htmlFor="note-title-input">
Enter a title for your note
</label>
<input
id="note-title-input"
type="text"
value={title}
onChange={(e) => {
setTitle(e.target.value);
}}
/>
<label className="offscreen" htmlFor="note-body-input">
Enter the body for your note
</label>
<textarea
id="note-body-input"
value={body}
onChange={(e) => {
setBody(e.target.value);
}}
/>
</form>
<div className="note-editor-preview">
<div className="note-editor-menu" role="menubar">
<button
className="note-editor-done"
disabled={isSaving || isNavigating}
onClick={() => handleSave()}
role="menuitem">
<img
src="checkmark.svg"
width="14px"
height="10px"
alt=""
role="presentation"
/>
Done
</button>
{!isDraft && (
<button
className="note-editor-delete"
disabled={isDeleting || isNavigating}
onClick={() => handleDelete()}
role="menuitem">
<img
src="cross.svg"
width="10px"
height="10px"
alt=""
role="presentation"
/>
Delete
</button>
)}
</div>
<div className="label label--preview" role="status">
Preview
</div>
<h1 className="note-title">{title}</h1>
<NotePreview title={title} body={body} />
</div>
</div>
);
}
function useMutation({endpoint, method}) {
const [isSaving, setIsSaving] = useState(false);
const [didError, setDidError] = useState(false);
const [error, setError] = useState(null);
if (didError) {
// Let the nearest error boundary handle errors while saving.
throw error;
}
async function performMutation(payload, requestedLocation) {
setIsSaving(true);
try {
const response = await fetch(
`${endpoint}?location=${encodeURIComponent(
JSON.stringify(requestedLocation)
)}`,
{
method,
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json',
},
}
);
if (!response.ok) {
throw new Error(await response.text());
}
return response;
} catch (e) {
setDidError(true);
setError(e);
} finally {
setIsSaving(false);
}
}
return [isSaving, performMutation];
}