Skip to content

Commit e5469eb

Browse files
authored
Merge pull request #41 from wajeshubham/dev
Dev
2 parents f072673 + cc0b451 commit e5469eb

File tree

12 files changed

+408
-42
lines changed

12 files changed

+408
-42
lines changed

components/Loader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react";
22

33
const Loader = () => {
44
return (
5-
<div className="flex space-x-2 w-full h-screen fixed inset-0 bg-zinc-700/50 z-30 justify-center items-center">
5+
<div className="flex space-x-2 w-full h-screen fixed inset-0 bg-zinc-700/50 z-50 justify-center items-center">
66
<div aria-label="Loading..." role="status">
77
<svg className="h-12 w-12 animate-spin" viewBox="3 3 18 18">
88
<path

components/editor/SnippngCodeArea.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ const SnippngCodeArea: React.FC<Props> = ({ underConstructionTheme }) => {
8383
const savedDoc = await addDoc(collection(db, "snippets"), {
8484
...dataToBeAdded,
8585
ownerUid: user.uid,
86+
owner: {
87+
displayName: user.displayName,
88+
photoURL: user.photoURL,
89+
email: user.email,
90+
},
8691
});
8792
if (savedDoc.id) {
8893
addToast({
@@ -148,6 +153,7 @@ const SnippngCodeArea: React.FC<Props> = ({ underConstructionTheme }) => {
148153
...editorConfig,
149154
uid: undefined,
150155
ownerUid: undefined,
156+
owner: undefined,
151157
});
152158
}, [editorConfig, uid, underConstructionTheme]);
153159

components/editor/SnippngThemeBuilder.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const SnippngThemeBuilder: React.FC<{
4242
const dataToBeAdded = {
4343
...deepClone(theme), // deep clone the theme to avoid mutation
4444
ownerUid: user.uid,
45+
isPublished: false,
4546
owner: {
4647
displayName: user?.displayName,
4748
email: user?.email,
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { db } from "@/config/firebase";
2+
import { SnippngThemeAttributesInterface } from "@/types";
3+
import { SparklesIcon } from "@heroicons/react/24/outline";
4+
import { collection, getDocs, query, where } from "firebase/firestore";
5+
import { useRouter } from "next/router";
6+
import { useEffect, useState } from "react";
7+
import ErrorText from "../ErrorText";
8+
import Loader from "../Loader";
9+
import SnippngThemeItem from "../profile/SnippngThemeItem";
10+
import { useAuth } from "@/context/AuthContext";
11+
12+
const PublishedThemeListing = () => {
13+
const [themes, setThemes] = useState<SnippngThemeAttributesInterface[]>([]);
14+
const [loadingThemes, setLoadingThemes] = useState(false);
15+
const router = useRouter();
16+
const { user } = useAuth();
17+
18+
const fetchPublishedThemes = async () => {
19+
if (!db) return console.log(Error("Firebase is not configured")); // This is to handle error when there is no `.env` file. So, that app doesn't crash while developing without `.env` file.
20+
setLoadingThemes(true);
21+
try {
22+
const _themes: SnippngThemeAttributesInterface[] = [];
23+
const docRef = await getDocs(
24+
query(collection(db, "themes"), where("isPublished", "==", true))
25+
);
26+
docRef.forEach((doc) => {
27+
const theme = doc.data();
28+
_themes.push({
29+
...theme,
30+
uid: doc.id,
31+
} as unknown as SnippngThemeAttributesInterface);
32+
});
33+
setThemes(_themes);
34+
} catch (error) {
35+
console.log("Error fetching snippets", error);
36+
} finally {
37+
setLoadingThemes(false);
38+
}
39+
};
40+
41+
useEffect(() => {
42+
fetchPublishedThemes();
43+
}, [user]);
44+
45+
if (loadingThemes) return <Loader />;
46+
47+
return (
48+
<div className="py-5">
49+
<dl className="grid grid-cols-1 gap-x-4 gap-y-8 sm:grid-cols-2">
50+
<div className="sm:col-span-2">
51+
<dd className="mt-1 text-sm text-zinc-900">
52+
{themes.length ? (
53+
<ul role="list" className="grid md:grid-cols-2 grid-cols-1 gap-4">
54+
{themes.map((theme) => (
55+
<SnippngThemeItem
56+
key={theme.id}
57+
theme={theme}
58+
onDelete={(themeId) => {
59+
setThemes(
60+
[...themes].filter((thm) => thm.id !== themeId)
61+
);
62+
}}
63+
onPublishChange={(themeId) => {
64+
setThemes(
65+
[...themes].map((theme) => {
66+
if (theme.id === themeId) {
67+
theme.isPublished = !theme.isPublished;
68+
}
69+
return theme;
70+
})
71+
);
72+
}}
73+
/>
74+
))}
75+
</ul>
76+
) : (
77+
<ErrorText
78+
errorTitle="No themes found"
79+
errorSubTitle="Please construct some themes to list them here"
80+
errorActionProps={{
81+
children: "Construct",
82+
StartIcon: SparklesIcon,
83+
onClick: () => {
84+
router.push("/theme/create");
85+
},
86+
}}
87+
/>
88+
)}
89+
</dd>
90+
</div>
91+
</dl>
92+
</div>
93+
);
94+
};
95+
96+
export default PublishedThemeListing;

0 commit comments

Comments
 (0)