Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/tanstack/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Finder (MacOS) folder config
.DS_Store

tmp
54 changes: 31 additions & 23 deletions examples/tanstack/app/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as fs from "node:fs";
import * as path from "node:path";
import { createFileRoute, useRouter } from "@tanstack/react-router";
import { createServerFn } from "@tanstack/react-start";

const filePath = "count.txt";
const countFilePath = "count.txt";

async function readCount() {
return parseInt(
await fs.promises.readFile(filePath, "utf-8").catch(() => "0")
await fs.promises.readFile(countFilePath, "utf-8").catch(() => "0")
);
}

Expand All @@ -16,16 +17,36 @@ const getCount = createServerFn({
return readCount();
});

// Used to post a file input to the server
const postFile = createServerFn({
method: "POST",
})
.validator((data: FormData) => {
const file = data.get("file");
if (!(file instanceof File)) {
throw new Error("Expected a file input");
}
return file;
})
.handler(async ({ data }) => {
const file = data;
const content = await file.text();
const filePath = path.join(import.meta.dirname, "..", "..", "tmp", file.name);

await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
await fs.promises.writeFile(filePath, content);
});

const updateCount = createServerFn({ method: "POST" })
.validator((d: number) => d)
.handler(async ({ data }) => {
const count = await readCount();
await fs.promises.writeFile(filePath, `${count + data}`);
await fs.promises.writeFile(countFilePath, `${count + data}`);
});

// n.b. we can't create a `DELETE` server fn
const resetCount = createServerFn({ method: "POST" }).handler(async () => {
await fs.promises.writeFile(filePath, "0");
await fs.promises.writeFile(countFilePath, "0");
});

export const Route = createFileRoute("/")({
Expand Down Expand Up @@ -66,29 +87,16 @@ function Home() {
</li>
<li>
<form
onSubmit={(e) => {
e.preventDefault();
const form = e.currentTarget as HTMLFormElement;
const formData = new FormData(form);
const recordingId = formData.get("recording-id") as string;

const url = new URL(window.location.href);
url.searchParams.delete("jam-recording");
url.searchParams.set("jam-recording", recordingId);

window.history.replaceState({}, "", url.toString());
window.location.reload();
onSubmit={async (event) => {
event.preventDefault();
await postFile({ data: new FormData(event.currentTarget) });
}}
>
<label>
Recording ID:
<input
name="recording-id"
type="text"
placeholder="Create me at Jam.dev"
/>
Upload a file:
<input type="file" name="file" />
</label>
<button type="submit">Open</button>
<button type="submit">Upload</button>
</form>
</li>
</ul>
Expand Down