Skip to content

Commit 590c000

Browse files
committed
Implement note creation auto adding
* Use set state hook function to automatically show newly created notes instead of making a new API request
1 parent 2a1df89 commit 590c000

4 files changed

Lines changed: 10 additions & 4 deletions

File tree

components/CreateJottingButton/createJottingButton.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default function CreateJottingButton({
1111
jotType,
1212
requestFunc = "createJotting",
1313
requestArg1 = jotType,
14+
setJots
1415
}) {
1516
const [newJotInputCls, setNewJotInputCls] = useState("invisible");
1617
const [createJotBtnCls, setCreateJotBtnCls] = useState(
@@ -54,16 +55,21 @@ export default function CreateJottingButton({
5455
if (e.key == "Enter") {
5556
const jotName = e.target.value;
5657
try {
57-
await Requests[requestFunc](
58+
const newJotResponseData = await Requests[requestFunc](
5859
requestArg1,
5960
jotName
6061
);
62+
setJots((prevJots, props) => {
63+
console.info("New Jots State", [...prevJots, newJotResponseData]);
64+
return [...prevJots, newJotResponseData];
65+
});
6166
} catch (e) {
6267
console.error(e);
6368
window.alert("A system error occurred");
6469
} finally {
6570
clearInput();
6671
showBtn();
72+
6773
}
6874
} else if (e.key == "Escape") {
6975
clearInput();

components/JottingList/noteList.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import FetchError from "../FetchError/fetchError";
22
import NotePreview from "../Jotting/notePreview";
33
import jottingList from "./jottingList.module.css";
44

5-
export default function NoteList({ notes }) {
5+
export default function NoteList({ notes }) {
66
let noteList = <FetchError itemName="notes" />;
77

88
if (notes instanceof Array) {

components/JottingsControls/notesControl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default function NotesControl({notesState, active}) {
1919
<article className={`${active ? "active" : ""} ${jottingsControl.ownNoteList}`}>
2020
<h1>Notes</h1>
2121
{notes ? <NoteList notes={notes} /> : <ProgressSpinner />}
22-
<CreateNoteButton jots={notes} />
22+
<CreateNoteButton setJots={setNotes} />
2323
</article>
2424
);
2525
}

libs/Datastore/requests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export async function updateTaskStatus(id, completed) {
161161
*/
162162
export async function createJotting(jotType, jotName) {
163163
const queryString = `${jotType.toLowerCase()}`;
164-
const response = BorumJotRequest.initialize(queryString)
164+
const response = await BorumJotRequest.initialize(queryString)
165165
.authorize()
166166
.post(`name=${jotName}`)
167167
.makeRequest();

0 commit comments

Comments
 (0)