Skip to content

Commit 64d2fed

Browse files
committed
refactor(examples): nested collection query improvements
1 parent 5e3778c commit 64d2fed

File tree

5 files changed

+631
-27
lines changed

5 files changed

+631
-27
lines changed

examples/react/kitchen-sink/src/App.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
22
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
33
import { useState } from "react";
4-
import { Routes, Route, Link, useLocation } from "react-router-dom";
5-
import { IdTokenExample } from "./components/IdTokenExample";
4+
import { Link, Route, Routes, useLocation } from "react-router-dom";
65
import { CollectionQueryExample } from "./components/CollectionQueryExample";
6+
import { IdTokenExample } from "./components/IdTokenExample";
7+
import { NestedCollectionsExample } from "./components/NestedCollectionsExample";
78

89
import "./firebase";
910

@@ -16,7 +17,7 @@ function App() {
1617
staleTime: 60 * 1000,
1718
},
1819
},
19-
})
20+
}),
2021
);
2122

2223
return (
@@ -32,6 +33,10 @@ function App() {
3233
path="/firestore/collection-query"
3334
element={<CollectionQueryExample />}
3435
/>
36+
<Route
37+
path="/nested-collections"
38+
element={<NestedCollectionsExample />}
39+
/>
3540
</Routes>
3641
</div>
3742
</div>

examples/react/kitchen-sink/src/components/CollectionQueryExample.tsx

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import {
2-
addDoc,
2+
useAddDocumentMutation,
3+
useCollectionQuery,
4+
} from "@tanstack-query-firebase/react/firestore";
5+
import {
36
collection,
47
deleteDoc,
58
doc,
@@ -8,11 +11,6 @@ import {
811
where,
912
} from "firebase/firestore";
1013
import { useState } from "react";
11-
import {
12-
useCollectionQuery,
13-
useAddDocumentMutation,
14-
useDeleteDocumentMutation,
15-
} from "@tanstack-query-firebase/react/firestore";
1614

1715
interface Task {
1816
id: string;
@@ -50,9 +48,6 @@ export function CollectionQueryExample() {
5048
// Add task mutation
5149
const addTaskMutation = useAddDocumentMutation(tasksCollection);
5250

53-
// Delete task mutation
54-
const deleteTaskMutation = useDeleteDocumentMutation();
55-
5651
const handleAddTask = async () => {
5752
if (!newTaskTitle.trim()) return;
5853

@@ -74,9 +69,8 @@ export function CollectionQueryExample() {
7469

7570
const handleToggleTask = async (
7671
taskId: string,
77-
currentCompleted: boolean
72+
currentCompleted: boolean,
7873
) => {
79-
const taskRef = doc(firestore, "tasks", taskId);
8074
// Note: In a real app, you'd use useUpdateDocumentMutation here
8175
// For simplicity, we're just showing the query functionality
8276
console.log(`Would toggle task ${taskId} to ${!currentCompleted}`);
@@ -85,7 +79,7 @@ export function CollectionQueryExample() {
8579
const handleDeleteTask = async (taskId: string) => {
8680
const taskRef = doc(firestore, "tasks", taskId);
8781
try {
88-
await deleteTaskMutation.mutateAsync(taskRef);
82+
await deleteDoc(taskRef);
8983
} catch (error) {
9084
console.error("Failed to delete task:", error);
9185
}
@@ -263,15 +257,14 @@ export function CollectionQueryExample() {
263257
</div>
264258
<span
265259
className={`px-2 py-1 rounded-full text-xs font-medium ${getPriorityColor(
266-
task.priority
260+
task.priority,
267261
)}`}
268262
>
269263
{task.priority}
270264
</span>
271265
</div>
272266
<button
273267
onClick={() => handleDeleteTask(task.id)}
274-
disabled={deleteTaskMutation.isPending}
275268
className="ml-4 px-3 py-1 text-red-600 hover:bg-red-50 rounded-md text-sm font-medium"
276269
>
277270
Delete
@@ -298,8 +291,8 @@ export function CollectionQueryExample() {
298291
{filterCompleted === null
299292
? "All"
300293
: filterCompleted
301-
? "Completed"
302-
: "Pending"}
294+
? "Completed"
295+
: "Pending"}
303296
</p>
304297
<p>
305298
<strong>Status:</strong>{" "}

examples/react/kitchen-sink/src/components/IdTokenExample.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function IdTokenExample() {
99
const [refreshCount, setRefreshCount] = useState(0);
1010
const [previousToken, setPreviousToken] = useState<string | null>(null);
1111
const [lastForceRefreshTime, setLastForceRefreshTime] = useState<Date | null>(
12-
null
12+
null,
1313
);
1414

1515
// Listen for auth state changes
@@ -44,7 +44,7 @@ export function IdTokenExample() {
4444
if (token) {
4545
console.log(
4646
"Token retrieved successfully:",
47-
`${token.substring(0, 20)}...`
47+
`${token.substring(0, 20)}...`,
4848
);
4949

5050
// Check if token changed
@@ -129,7 +129,7 @@ export function IdTokenExample() {
129129
) : (
130130
<div>
131131
<div className="space-y-3">
132-
<p className="text-sm text-gray-600 font-mono text-xs">
132+
<p className="text-sm text-gray-600 font-mono">
133133
Token hash: {token ? `${btoa(token).slice(0, 16)}...` : ""}
134134
</p>
135135
{lastRefreshTime && (
@@ -170,7 +170,7 @@ export function IdTokenExample() {
170170
<p className="text-gray-600">Loading fresh token...</p>
171171
) : freshToken ? (
172172
<div className="space-y-3">
173-
<p className="text-sm text-gray-600 font-mono text-xs">
173+
<p className="text-sm text-gray-600 font-mono">
174174
Token hash:{" "}
175175
{freshToken ? `${btoa(freshToken).slice(0, 16)}...` : ""}
176176
</p>
@@ -237,7 +237,7 @@ export function IdTokenExample() {
237237
const result = [];
238238
const maxLength = Math.max(
239239
token.length,
240-
freshToken.length
240+
freshToken.length,
241241
);
242242

243243
for (let i = 0; i < maxLength; i++) {
@@ -252,13 +252,13 @@ export function IdTokenExample() {
252252
className="bg-yellow-300 text-red-600 font-bold"
253253
>
254254
{freshToken[i] || "∅"}
255-
</span>
255+
</span>,
256256
);
257257
} else {
258258
result.push(
259259
<span key={i} className="text-gray-600">
260260
{token[i]}
261-
</span>
261+
</span>,
262262
);
263263
}
264264
}

0 commit comments

Comments
 (0)