Skip to content

Commit 049ae4c

Browse files
committed
βœ…: open post link directly ; πŸ“Œ analytics
1 parent b0b3ee0 commit 049ae4c

File tree

10 files changed

+113
-44
lines changed

10 files changed

+113
-44
lines changed

β€ŽPipfileβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ gunicorn = "*"
1010
notion-client = "*"
1111
python-dotenv = "*"
1212
django-cors-headers = "*"
13+
requests = "*"
1314

1415
[dev-packages]
1516
black = "*"

β€ŽPipfile.lockβ€Ž

Lines changed: 50 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€ŽREADME.mdβ€Ž

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ I use it everyday from planning and todos to notes and content and now, even blo
116116
- [X] Lazy load images
117117
- [X] Have added loading attribute to the img tag
118118
- [X] Post open, shows loading for a split second but the content still takes time to load - switched to useState instead of Suspense
119-
- [ ] Error when individual post url open instead of opening from home page - need to fix this so i can share articles
120-
- [ ] After above fix add sharing option in navbar
119+
- [X] Error when individual post url open instead of opening from home page - need to fix this so i can share articles
120+
- [X] After above fix add sharing option in navbar
121+
- [ ] Add analytics
121122

122123
### Visual
123124
- [X] make proper padding for all blocks

β€Žsrc/Components/blogpost.jsxβ€Ž

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useParams } from 'react-router-dom';
33
import useArticleStore from '../store';
44
import Post from './post';
55
import PostLoader from './postloader';
6+
import { fetchAllArticles } from './utilities';
67

78

89
export default function BlogPost(props) {
@@ -11,15 +12,40 @@ export default function BlogPost(props) {
1112

1213
const [isLoading, setLoading] = useState(true);
1314

14-
const { page, getPageFromSlug } = useArticleStore((state) => ({
15+
const { articles, setArticles, page, setPage, getPageFromSlug } = useArticleStore((state) => ({
16+
articles: state.articles,
17+
setArticles: state.setArticles,
1518
page: state.page,
19+
setPage: state.setPage,
1620
getPageFromSlug: state.getPageFromSlug,
1721
}));
1822

23+
async function fetchPage() {
24+
if (articles.length < 1) {
25+
console.log('Making page request')
26+
await fetch(process.env.REACT_APP_BACKEND_URI + '/get-page/' + data.slug,
27+
{
28+
method: "GET",
29+
mode: 'cors',
30+
headers: {
31+
'Content-Type': 'application/json',
32+
"Accept": "application/json",
33+
}
34+
}
35+
).then(async (response) => {
36+
const res = await response.json();
37+
const result = res.page_properties;
38+
setPage(result)
39+
setLoading(false)
40+
})
41+
} else {
42+
getPageFromSlug(data.slug)
43+
setLoading(false)
44+
}
45+
}
1946

2047
useEffect(() => {
21-
getPageFromSlug(data.slug);
22-
setLoading(false);
48+
fetchPage();
2349
}, [])
2450

2551

@@ -28,7 +54,7 @@ export default function BlogPost(props) {
2854
{
2955
isLoading ?
3056
<main>
31-
<section className='max-w-2xl mx-auto my-20'>
57+
<section className='my-20 px-6 md:px-0 mx-auto md:w-sm lg:w-md lg:max-w-4xl'>
3258
<PostLoader />
3359
</section>
3460
</main>

β€Žsrc/Components/navbar.jsxβ€Ž

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ import { Link } from 'react-router-dom'
33

44
export default function Navbar() {
55
return (
6-
<nav className=''>
6+
<nav className='relative flex flex-row justify-between items-center'>
77
<Link to="/" className='text-sm fixed top-4 left-10 flex flex-row justify-start items-center gap-2'>
88
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor" className="w-6 h-6">
99
<path strokeLinecap="round" strokeLinejoin="round" d="M6.75 15.75L3 12m0 0l3.75-3.75M3 12h18" />
1010
</svg>
1111
<p>Back</p>
1212
</Link>
13+
14+
<button className='fixed top-4 right-10 text-sm'>
15+
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-6 h-6">
16+
<path strokeLinecap="round" strokeLinejoin="round" d="M7.217 10.907a2.25 2.25 0 100 2.186m0-2.186c.18.324.283.696.283 1.093s-.103.77-.283 1.093m0-2.186l9.566-5.314m-9.566 7.5l9.566 5.314m0 0a2.25 2.25 0 103.935 2.186 2.25 2.25 0 00-3.935-2.186zm0-12.814a2.25 2.25 0 103.933-2.185 2.25 2.25 0 00-3.933 2.185z" />
17+
</svg>
18+
</button>
1319
</nav>
1420
)
1521
}

β€Žsrc/Components/utilities.jsxβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const containsObject = (obj, list) => {
88
return false;
99
}
1010

11-
export function fetchAllArticles(setArticlesFunc) {
11+
export async function fetchAllArticles(setArticlesFunc) {
1212
fetch(process.env.REACT_APP_BACKEND_URI + '/get-database/',
1313
{
1414
method: "GET",
2 Bytes
Binary file not shown.
202 Bytes
Binary file not shown.

β€Žv1/urls.pyβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
urlpatterns = [
77
path(route="get-database/", view=getDatabase, name="get-database"),
8-
path(route="get-page/<str:id>", view=getPage, name="get-page"),
8+
path(route="get-page/<str:slug>", view=getPage, name="get-page"),
99
path(route="get-blocks/<str:id>", view=getBlocks, name="get-blocks"),
1010
]

β€Žv1/views.pyβ€Ž

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
from doctest import master
21
import os
2+
import requests
33
from rest_framework import status
44
from rest_framework.decorators import api_view
55
from rest_framework.response import Response
66
from notion_client import Client
77

88

9+
notion = Client(auth=os.environ.get("NOTION_TOKEN"))
10+
911
@api_view(["GET"])
1012
def getDatabase(request):
11-
notion = Client(auth=os.environ.get("NOTION_TOKEN"))
13+
global notion
1214
try:
1315
notiondatabse = notion.databases.query(
1416
**{
@@ -50,10 +52,20 @@ def getDatabase(request):
5052

5153

5254
@api_view(["GET"])
53-
def getPage(request, id):
54-
notion = Client(auth=os.environ.get("NOTION_TOKEN"))
55+
def getPage(request, slug):
56+
global notion
5557
try:
56-
notionPage = notion.pages.retrieve(page_id=id)
58+
allArticles = requests.get(
59+
url=os.path.join(os.environ["REACT_APP_BACKEND_URI"], "get-database"),
60+
headers={
61+
"Content-Type": "application/json",
62+
"Accept": "application/json",
63+
},
64+
).json()
65+
allArticles = allArticles["results"]
66+
for page in allArticles:
67+
if page["properties"]["slug"]["rich_text"][0]["plain_text"] == slug:
68+
notionPage = notion.pages.retrieve(page_id=page["id"])
5769
except Exception as e:
5870
return Response(
5971
data={
@@ -100,13 +112,14 @@ def hasChildren(parentBlock: any, notion: Client):
100112
else:
101113
masterBlock["children"] = getChildBlock(notion=notion, id=masterBlock["id"])
102114
for index, child in enumerate(masterBlock["children"]):
103-
masterBlock["children"][index] = hasChildren(parentBlock=child, notion=notion)
115+
masterBlock["children"][index] = hasChildren(
116+
parentBlock=child, notion=notion
117+
)
104118
return masterBlock
105119

106120

107121
@api_view(["GET"])
108122
def getBlocks(request, id):
109-
notion = Client(auth=os.environ.get("NOTION_TOKEN"))
110123
try:
111124
blocks = getChildBlock(notion=notion, id=id)
112125
for index, block in enumerate(blocks):

0 commit comments

Comments
Β (0)