Skip to content

Commit bb1a2e8

Browse files
committed
star finished
1 parent ad720c8 commit bb1a2e8

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

firebase/database.rules.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
".read": true,
99
".write": true
1010
}
11+
},
12+
"stars": {
13+
"$treeId": {
14+
"$other": { ".validate": true },
15+
".read": true,
16+
".write": true
17+
}
1118
}
1219
}
1320
}

src/components/tree/Result.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const Result = () => {
8787
return <Fragment>Life is hard, we're getting your data...</Fragment>;
8888
}
8989

90-
return <Tree data={data} />;
90+
return <Tree data={data} treeId={treeId} />;
9191
};
9292

9393
export default Result;

src/components/tree/Tree.tsx

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import React, { FC, useCallback, useState, Fragment } from "react";
1+
import React, {
2+
FC,
3+
useCallback,
4+
useState,
5+
Fragment,
6+
useContext,
7+
useEffect,
8+
} from "react";
29
import sortBy from "lodash.sortby";
310

411
import StarImgage from "../vectors/StarImage";
@@ -8,9 +15,11 @@ import { mostCommon } from "../../utils/arrays";
815
import { Article } from "../../utils/customTypes";
916

1017
import "./Tree.css";
18+
import FirebaseContext from "../../context/FirebaseContext";
1119

1220
interface Props {
1321
data: { [section: string]: Article[] };
22+
treeId: string;
1423
}
1524

1625
const INFO: {
@@ -40,9 +49,10 @@ const INFO: {
4049
},
4150
};
4251

43-
const Tree: FC<Props> = ({ data }: Props) => {
52+
const Tree: FC<Props> = ({ data, treeId }: Props) => {
4453
const [star, setStar] = useState<{ [label: string]: boolean }>({});
4554
const [show, setShow] = useState<"root" | "trunk" | "leaf" | null>(null);
55+
const firebase = useContext(FirebaseContext);
4656
let keywords: { [label: string]: string[] } = {
4757
root: [],
4858
trunk: [],
@@ -62,9 +72,23 @@ const Tree: FC<Props> = ({ data }: Props) => {
6272
);
6373
}
6474

65-
const toggleStar = useCallback((label: string) => {
66-
setStar((current) => ({ ...current, [label]: !current[label] }));
67-
}, []);
75+
useEffect(() => {
76+
if (!firebase) return;
77+
firebase
78+
.database()
79+
.ref(`stars/${treeId}`)
80+
.on("value", (snapshot) => setStar(snapshot.val()));
81+
}, [firebase, treeId]);
82+
83+
const toggleStar = useCallback(
84+
(label: string) => {
85+
firebase
86+
?.database()
87+
.ref(`stars/${treeId}`)
88+
.set({ ...star, [btoa(label)]: !star[btoa(label)] });
89+
},
90+
[firebase, treeId, star]
91+
);
6892

6993
const toggleShow = useCallback((label: "root" | "trunk" | "leaf") => {
7094
setShow((curr) => {
@@ -112,7 +136,7 @@ const Tree: FC<Props> = ({ data }: Props) => {
112136
</div>
113137
<div className="articles">
114138
{sortBy(data[sectionName], (article) =>
115-
!star[article.label] ? 1 : 0
139+
!star[btoa(article.label)] ? 1 : 0
116140
).map((article) => (
117141
<div className="article" key={`article-${article.label}`}>
118142
<Reference key={article.label} {...article} />
@@ -121,11 +145,15 @@ const Tree: FC<Props> = ({ data }: Props) => {
121145
</button> */}
122146
<button
123147
className={`btn-star ${
124-
star[article.label] ? "favorite" : ""
148+
star[btoa(article.label)] ? "favorite" : ""
125149
}`}
126150
onClick={() => toggleStar(article.label)}
127151
>
128-
{!!star[article.label] ? <StarImgage /> : <StarImgage />}
152+
{!!star[btoa(article.label)] ? (
153+
<StarImgage />
154+
) : (
155+
<StarImgage />
156+
)}
129157
</button>
130158
</div>
131159
))}

0 commit comments

Comments
 (0)