This commit is contained in:
Michael Zhang 2023-03-28 16:36:36 -05:00
parent 0dc50b636a
commit 86a2fabfcf
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
12 changed files with 1401 additions and 232 deletions

16
index.html Normal file
View file

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<link rel="shortcut icon" type="image/ico" href="/src/assets/favicon.ico" />
<title>Solid App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script src="/src/client/index.tsx" type="module"></script>
</body>
</html>

1459
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"dev": "vite"
},
"keywords": [],
"author": "",
@ -15,11 +15,14 @@
"prisma": "^4.11.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.2",
"wetzel": "^0.2.3"
"wetzel": "^0.2.3",
"vite": "^4.2.1",
"vite-plugin-solid": "^2.6.1"
},
"dependencies": {
"@prisma/client": "^4.11.0",
"express": "^4.18.2",
"solid-js": "^1.6.15",
"vm2": "^3.9.14"
}
}

24
src/client/App.tsx Normal file
View file

@ -0,0 +1,24 @@
import Todos from "./Todos";
function App() {
return (
<div>
<header>
<p>
Edit <code>src/App.jsx</code> and save to reload.
</p>
<a
href="https://github.com/solidjs/solid"
target="_blank"
rel="noopener noreferrer"
>
Learn Solid
</a>
<Todos />
</header>
</div>
);
}
export default App;

72
src/client/Todos.tsx Normal file
View file

@ -0,0 +1,72 @@
import { For, batch, createEffect, createSignal } from "solid-js";
import { SetStoreFunction, Store, createStore } from "solid-js/store";
type TodoItem = { title: string; done: boolean };
const Todos = () => {
const [newTitle, setTitle] = createSignal("");
const [todos, setTodos] = createLocalStore<TodoItem[]>("todos", []);
const addTodo = (e: SubmitEvent) => {
e.preventDefault();
batch(() => {
setTodos(todos.length, {
title: newTitle(),
done: false,
});
setTitle("");
});
};
return (
<>
<h3>Simple Todos Example</h3>
<form onSubmit={addTodo}>
<input
placeholder="enter todo and click +"
required
value={newTitle()}
onInput={(e) => setTitle(e.currentTarget.value)}
/>
<button>+</button>
</form>
<For each={todos}>
{(todo, i) => (
<div>
<input
type="checkbox"
checked={todo.done}
onChange={(e) => setTodos(i(), "done", e.currentTarget.checked)}
/>
<input
type="text"
value={todo.title}
onChange={(e) => setTodos(i(), "title", e.currentTarget.value)}
/>
<button onClick={() => setTodos((t) => removeIndex(t, i()))}>
x
</button>
</div>
)}
</For>
</>
);
};
export default Todos;
export function createLocalStore<T extends object>(
name: string,
init: T
): [Store<T>, SetStoreFunction<T>] {
const localState = localStorage.getItem(name);
const [state, setState] = createStore<T>(
localState ? JSON.parse(localState) : init
);
createEffect(() => localStorage.setItem(name, JSON.stringify(state)));
return [state, setState];
}
export function removeIndex<T>(array: readonly T[], index: number): T[] {
return [...array.slice(0, index), ...array.slice(index + 1)];
}

12
src/client/index.tsx Normal file
View file

@ -0,0 +1,12 @@
import { render } from "solid-js/web";
import App from "./App";
const root = document.getElementById("root");
if (import.meta.env.DEV && !(root instanceof HTMLElement)) {
throw new Error(
"Root element not found. Did you forget to add it to your index.html? Or maybe the id attribute got mispelled?"
);
}
render(() => <App />, root);

View file

@ -1 +1 @@
import { Database } from "./db.ts";
import Database from "./db";

View file

@ -8,7 +8,8 @@ app.get("/", (req, res) => {
res.send("Welcome to the Dinosaur API!");
});
app.post("/node", async (req, res) => {
// Create a node
app.post("/api/node", async (req, res) => {
const node = await Database.createNode({
label: "hellosu",
other_nodes: new Map(),
@ -20,5 +21,8 @@ app.post("/node", async (req, res) => {
res.json({ success: true });
});
import todos from "./todos";
app.use("/api/todos", todos);
console.log("Listening.");
app.listen(8605, "0.0.0.0");

8
src/server/todos.ts Normal file
View file

@ -0,0 +1,8 @@
import {Router} from "express";
const router = Router();
router.get("/", async (req, res) => {
});
export default router;

15
tsconfig.json Normal file
View file

@ -0,0 +1,15 @@
{
"compilerOptions": {
"strict": true,
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"jsx": "preserve",
"jsxImportSource": "solid-js",
"types": ["vite/client"],
"noEmit": true,
"isolatedModules": true
}
}

12
vite.config.js Normal file
View file

@ -0,0 +1,12 @@
import { defineConfig } from "vite";
import solidPlugin from "vite-plugin-solid";
export default defineConfig({
plugins: [solidPlugin()],
server: {
port: 3000,
},
build: {
target: "esnext",
},
});