|
| 1 | +import { QueryClient } from "@tanstack/query-core"; |
| 2 | +import { |
| 3 | + useLiveQuery, |
| 4 | + useOptimisticMutation, |
| 5 | + createCollection, |
| 6 | +} from "@tanstack/react-db"; |
| 7 | +import { queryCollectionOptions } from "@tanstack/db-collections"; |
| 8 | + |
| 9 | +import { Client } from "trailbase"; |
| 10 | +import { useState } from "react"; |
| 11 | +import type { FormEvent } from "react"; |
| 12 | + |
| 13 | +import { trailBaseCollectionOptions } from "./lib/trailbase.ts"; |
| 14 | +import "./App.css"; |
| 15 | + |
| 16 | +const client = Client.init("http://localhost:4000"); |
| 17 | + |
| 18 | +type Data = { |
| 19 | + id: number | null; |
| 20 | + updated: number | null; |
| 21 | + data: string; |
| 22 | +}; |
| 23 | + |
| 24 | +const queryClient = new QueryClient(); |
| 25 | +const useTrailBase = true; |
| 26 | + |
| 27 | +const dataCollection = createCollection( |
| 28 | + useTrailBase |
| 29 | + ? trailBaseCollectionOptions<Data>({ |
| 30 | + client, |
| 31 | + recordApi: "data", |
| 32 | + getKey: (item) => item.id ?? -1, |
| 33 | + }) |
| 34 | + : queryCollectionOptions<Data>({ |
| 35 | + id: "data", |
| 36 | + queryKey: ["data"], |
| 37 | + queryFn: async () => |
| 38 | + (await client.records("data").list<Data>()).records, |
| 39 | + getKey: (item) => item.id ?? -1, |
| 40 | + queryClient: queryClient, |
| 41 | + }), |
| 42 | +); |
| 43 | + |
| 44 | +function App() { |
| 45 | + const [input, setInput] = useState(""); |
| 46 | + |
| 47 | + const { data } = useLiveQuery((q) => |
| 48 | + q |
| 49 | + .from({ dataCollection }) |
| 50 | + .orderBy(`@updated`) |
| 51 | + .select(`@id`, `@updated`, `@data`), |
| 52 | + ); |
| 53 | + |
| 54 | + // Define mutations |
| 55 | + const addData = useOptimisticMutation({ |
| 56 | + mutationFn: async ({ transaction }) => { |
| 57 | + const { changes: newData } = transaction.mutations[0]; |
| 58 | + await client.records("data").create(newData as Data); |
| 59 | + |
| 60 | + await dataCollection.utils.refetch(); |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + function handleSubmit(e: FormEvent) { |
| 65 | + e.preventDefault(); // Don't reload the page. |
| 66 | + |
| 67 | + const form = e.target; |
| 68 | + const formData = new FormData(form as HTMLFormElement); |
| 69 | + |
| 70 | + const formJson = Object.fromEntries(formData.entries()); |
| 71 | + const text = formJson.text as string; |
| 72 | + |
| 73 | + if (text) { |
| 74 | + addData.mutate(() => { |
| 75 | + dataCollection.insert({ |
| 76 | + id: null, |
| 77 | + updated: null, |
| 78 | + data: formJson.text as string, |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + console.log(formJson); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <> |
| 88 | + <h1>Local First</h1> |
| 89 | + |
| 90 | + <div className="card"> |
| 91 | + <table> |
| 92 | + <thead> |
| 93 | + <tr> |
| 94 | + <th>id</th> |
| 95 | + <th>updated</th> |
| 96 | + <th>data</th> |
| 97 | + </tr> |
| 98 | + </thead> |
| 99 | + |
| 100 | + <tbody> |
| 101 | + {data.map((d, idx) => ( |
| 102 | + <tr key={`row-${idx}`}> |
| 103 | + <td>{d.id}</td> |
| 104 | + <td>{d.updated}</td> |
| 105 | + <td>{d.data}</td> |
| 106 | + </tr> |
| 107 | + ))} |
| 108 | + </tbody> |
| 109 | + </table> |
| 110 | + </div> |
| 111 | + |
| 112 | + <form method="post" onSubmit={handleSubmit}> |
| 113 | + <p className="read-the-docs"> |
| 114 | + <input |
| 115 | + name="text" |
| 116 | + type="text" |
| 117 | + onInput={(e) => setInput(e.currentTarget.value)} |
| 118 | + /> |
| 119 | + |
| 120 | + <button type="submit" disabled={input === ""}> |
| 121 | + submit |
| 122 | + </button> |
| 123 | + </p> |
| 124 | + </form> |
| 125 | + </> |
| 126 | + ); |
| 127 | +} |
| 128 | + |
| 129 | +export default App; |
0 commit comments