Skip to content

Commit 2f8b4b9

Browse files
committed
Polish and update for 0.3.0
1 parent 452b798 commit 2f8b4b9

8 files changed

Lines changed: 75 additions & 29 deletions

File tree

components/Jotting/jotting.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import jotting from "./jotting.module.css";
2+
import JottingOptionsBar from "./JottingOptionsBar";
3+
import JottingTitle from "./jottingTitle";
4+
import { useRef } from "react";
5+
import { useOutsideAlerter } from "../../libs/view";
6+
import { useRouter } from "next/router";
7+
8+
export default function Jotting(props) {
9+
const ref = useRef(null);
10+
const router = useRouter();
11+
12+
useOutsideAlerter(ref, router);
13+
14+
return (
15+
<div ref={ref} className={jotting.fullJotting}>
16+
<JottingOptionsBar {...props} />
17+
<JottingTitle
18+
id={props.id}
19+
originalTitle={props.title}
20+
jotType={props.jotType}
21+
/>
22+
{props.children}
23+
</div>
24+
);
25+
}

components/Jotting/jottingDetails.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useEffect, useState } from "react";
77
/**
88
* The component for the body or details of a jotting
99
* @param { {jotType: string, jottingInfo: any } } props The component props object
10-
* @param { {id: number} } props.jottingInfo The object representation of the jotting
10+
* @param { {id: number, body?: string} } props.jottingInfo The object representation of the jotting
1111
* @param {string} props.jotType The type of the jotting
1212
*/
1313
export default function JottingDetails({ jottingInfo, jotType }) {
@@ -21,8 +21,11 @@ export default function JottingDetails({ jottingInfo, jotType }) {
2121

2222
// componentDidMount(), componentDidUpdate() - getBody
2323
useEffect(() => {
24-
console.log("Fetching body...");
25-
getBody(jottingInfo.id, jotType).then((response) => setBody(response));
24+
if (jottingInfo.body != null) {
25+
setBody(jottingInfo.body);
26+
} else {
27+
getBody(jottingInfo.id, jotType).then((response) => setBody(response));
28+
}
2629
}, [jottingInfo.id]);
2730

2831
if (typeof body == "string") {
@@ -37,5 +40,5 @@ export default function JottingDetails({ jottingInfo, jotType }) {
3740
);
3841
}
3942

40-
return body || body == "" ? bodyEl : <CircularProgress />;
43+
return body != null ? bodyEl : <CircularProgress />;
4144
}

components/Jotting/note.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import jotting from "./jotting.module.css";
2-
import JottingOptionsBar from "./JottingOptionsBar";
3-
import JottingTitle from "./jottingTitle";
41
import JottingDetails from "./jottingDetails";
2+
import Jotting from "./Jotting";
53

64
export default function Note(note) {
75
return (
8-
<div className={jotting.fullJotting}>
9-
<JottingOptionsBar {...note} jotType="note" />
10-
<JottingTitle id={note.id} originalTitle={note.title} jotType="note" />
6+
<Jotting jotType="note" {...note}>
117
<JottingDetails jottingInfo={note} jotType="note" />
12-
</div>
8+
</Jotting>
139
);
1410
}

components/Jotting/notePreview.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import { useRouter } from "next/router";
55
export default function NotePreview(props) {
66
const router = useRouter();
77

8-
const handleNoteBtnClick = e => {
8+
const handleNoteBtnClick = (e) => {
99
Jotting.openJotting(router, "note", props);
1010
};
11-
11+
1212
return (
1313
<data className={jotting.jotting} value={"N" + props.id}>
14-
<button onClick={handleNoteBtnClick}>
15-
{props.title}
16-
</button>
14+
<button onClick={handleNoteBtnClick}>{props.title}</button>
1715
</data>
1816
);
1917
}

components/Jotting/task.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import jotting from "./jotting.module.css";
2-
import JottingOptionsBar from "./JottingOptionsBar";
3-
import JottingTitle from "./jottingTitle";
1+
import Jotting from "./Jotting";
2+
import JottingDetails from "./jottingDetails";
43

54
export default function Task(task) {
5+
console.log(task);
66
return (
7-
<div className={jotting.fullJotting}>
8-
<JottingOptionsBar {...task} jotType="task" />
9-
<JottingTitle id={task.id} originalTitle={task.title} jotType="task" />
10-
</div>
7+
<Jotting jotType="task" {...task}>
8+
<JottingDetails jotType="task" jottingInfo={task} />
9+
</Jotting>
1110
);
1211
}

libs/view.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
1+
import { useEffect } from "react";
2+
import Jotting from "./Jotting";
3+
14
/**
25
* Enum for describing the state of the content's view
36
*/
47
export const CONTENT_STATE = {
5-
VISIBLE: "visible",
6-
FADE_IN: "fadeIn",
7-
FADE_OUT: "fadeOut",
8-
INVISIBLE: "invisible",
8+
VISIBLE: "visible",
9+
FADE_IN: "fadeIn",
10+
FADE_OUT: "fadeOut",
11+
INVISIBLE: "invisible",
912
};
1013

1114
Object.freeze(CONTENT_STATE);
1215

16+
/**
17+
* Hook that clears url if user clicks outside of the component
18+
* @borrows {@link https://stackoverflow.com/a/42234988/9860982}
19+
* @param ref The React ref object returned from useRef()
20+
* @license CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0/)
21+
* @author Ben Bud
22+
*/
23+
export function useOutsideAlerter(ref, router) {
24+
useEffect(() => {
25+
const handleClickOutside = (e) => {
26+
if (ref.current && !ref.current.contains(e.target)) {
27+
Jotting.closeJotting(router);
28+
}
29+
};
30+
31+
// Bind the event listener
32+
document.addEventListener("mousedown", handleClickOutside);
33+
34+
// Unbind the event listener on clean up
35+
return () => document.removeEventListener("mousedown", handleClickOutside);
36+
}, [ref]);
37+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"author": {
55
"name": "Varun Singh"
66
},
7-
"version": "0.2.0",
7+
"version": "0.3.0",
88
"private": true,
99
"scripts": {
1010
"dev": "next dev",

pages/Home/home.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export default function Home({ fade, onFadeInLogin, setFade }) {
126126
router.query.id) ||
127127
urlMatchesDisplayJotting("task")) ? (
128128
<div className={home.fullJotting}>
129-
<Task {...router.query} />
129+
<Task {...tasks.find(item => item.id == router.query.id)} />
130130
</div>
131131
) : (
132132
""

0 commit comments

Comments
 (0)