@stanlemon/app-template 0.2.2 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/app.js CHANGED
@@ -3,16 +3,28 @@ import {
3
3
  asyncJsonHandler as handler,
4
4
  SimpleUsersDao,
5
5
  } from "@stanlemon/server-with-auth";
6
-
7
- const users = new SimpleUsersDao();
6
+ import { Low, JSONFile } from "lowdb";
8
7
 
9
8
  const app = createAppServer({
10
9
  webpack: "http://localhost:8080",
11
10
  secure: ["/api/"],
12
- ...users,
11
+ ...new SimpleUsersDao(),
13
12
  });
14
13
 
14
+ const db = new Low(new JSONFile("./db.json"));
15
+ await db.read();
16
+ db.data.items ||= [];
17
+
15
18
  app.get(
16
- "/api/users",
17
- handler(() => ({ users: users.users }))
19
+ "/api/items",
20
+ handler(() => ({ items: db.data.items }))
21
+ );
22
+
23
+ app.post(
24
+ "/api/items",
25
+ handler(async (item) => {
26
+ db.data.items.push(item);
27
+ await db.write();
28
+ return db.data.items;
29
+ })
18
30
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stanlemon/app-template",
3
- "version": "0.2.2",
3
+ "version": "0.2.5",
4
4
  "description": "A template for creating apps using the webdev package.",
5
5
  "author": "Stan Lemon <stanlemon@users.noreply.github.com>",
6
6
  "license": "MIT",
@@ -8,6 +8,7 @@
8
8
  "scripts": {
9
9
  "start": "node app.js",
10
10
  "build": "npm run webpack:build",
11
+ "tsc": "tsc",
11
12
  "webpack:serve": "webpack serve",
12
13
  "webpack:build": "NODE_ENV=production webpack",
13
14
  "test": "jest",
@@ -17,7 +18,14 @@
17
18
  "lint:format": "eslint --fix --ext js,jsx,ts,tsx ./src/"
18
19
  },
19
20
  "dependencies": {
21
+ "@stanlemon/server-with-auth": "0.1.4",
20
22
  "@stanlemon/webdev": "*",
21
- "@stanlemon/server-with-auth": "*"
23
+ "react": "^18.0.0",
24
+ "react-dom": "^18.0.0"
25
+ },
26
+ "devDependencies": {
27
+ "@testing-library/react": "^13.1.1",
28
+ "@types/react": "^18.0.5",
29
+ "@types/react-dom": "^18.0.1"
22
30
  }
23
31
  }
package/src/App.test.tsx CHANGED
@@ -36,7 +36,7 @@ test("<App/>", async () => {
36
36
  });
37
37
 
38
38
  // Type some data into the input
39
- userEvent.type(screen.getByLabelText("Item"), "The first item");
39
+ await userEvent.type(screen.getByLabelText("Item"), "The first item");
40
40
 
41
41
  // Click the add button
42
42
  fireEvent.click(screen.getByText("Add", { selector: "button" }));
package/src/App.tsx CHANGED
@@ -22,6 +22,7 @@ export type Session = {
22
22
  token: string | null;
23
23
  user: User | null;
24
24
  };
25
+
25
26
  export type User = {
26
27
  name: string | null;
27
28
  email: string | null;
@@ -78,42 +79,68 @@ export default function App() {
78
79
  return (
79
80
  <SessionContext.Provider value={contextValue}>
80
81
  <Header />
81
- <div>
82
- {!session && (
83
- <>
84
- <p>
85
- <em>You are not currently logged in.</em>
86
- </p>
82
+ {!session && (
83
+ <Row>
84
+ <Column>
85
+ <h2>Login</h2>
87
86
  <Login />
88
- <Spacer />
87
+ </Column>
88
+ <Column>
89
+ <h2>Register</h2>
89
90
  <Register />
90
- <Spacer />
91
- </>
92
- )}
93
- {session?.user && (
91
+ </Column>
92
+ </Row>
93
+ )}
94
+ {session?.user && (
95
+ <>
94
96
  <p>
95
97
  <em>You logged in as {session.user.username}.</em>
96
98
  </p>
97
- )}
98
- </div>
99
- <Input
100
- label="Item"
101
- name="item"
102
- value={value}
103
- onChange={(value) => setValue(value)}
104
- onEnter={addItem}
105
- />
106
- <button onClick={addItem}>Add</button>
107
-
108
- <ul>
109
- {items.map((item, i) => (
110
- <li key={i}>{item}</li>
111
- ))}
112
- </ul>
99
+ <Input
100
+ label="Item"
101
+ name="item"
102
+ value={value}
103
+ onChange={(value) => setValue(value)}
104
+ onEnter={addItem}
105
+ />
106
+ <button onClick={addItem}>Add</button>
107
+ <ul>
108
+ {items.map((item, i) => (
109
+ <li key={i}>{item}</li>
110
+ ))}
111
+ </ul>
112
+ </>
113
+ )}
113
114
  </SessionContext.Provider>
114
115
  );
115
116
  }
116
117
 
117
- function Spacer() {
118
- return <div style={{ minHeight: "2em" }} />;
118
+ function Row({ children }: { children: React.ReactNode }) {
119
+ return (
120
+ <div
121
+ style={{
122
+ display: "flex",
123
+ flexDirection: "row",
124
+ flexWrap: "wrap",
125
+ width: "100%",
126
+ }}
127
+ >
128
+ {children}
129
+ </div>
130
+ );
131
+ }
132
+
133
+ function Column({ children }: { children: React.ReactNode }) {
134
+ return (
135
+ <div
136
+ style={{
137
+ display: "flex",
138
+ flexDirection: "column",
139
+ flexBasis: "100%",
140
+ flex: 1,
141
+ }}
142
+ >
143
+ {children}
144
+ </div>
145
+ );
119
146
  }
package/src/Input.tsx CHANGED
@@ -1,3 +1,16 @@
1
+ export interface Props
2
+ // Inherit everything from input except onChange which we simplify here
3
+ extends Omit<React.ComponentPropsWithRef<"input">, "onChange"> {
4
+ type?: string;
5
+ name: string;
6
+ value?: string;
7
+ label?: string;
8
+ placeholder?: string;
9
+ onChange?: (value: string) => void;
10
+ onEnter?: () => void;
11
+ error?: string;
12
+ }
13
+
1
14
  export default function Input({
2
15
  type = "text",
3
16
  name,
@@ -11,16 +24,8 @@ export default function Input({
11
24
  /* noop */
12
25
  },
13
26
  error,
14
- }: {
15
- type?: string;
16
- name: string;
17
- value?: string;
18
- label?: string;
19
- placeholder?: string;
20
- onChange?: (value: string) => void;
21
- onEnter?: () => void;
22
- error?: string;
23
- }) {
27
+ ...attrs
28
+ }: Props) {
24
29
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
25
30
  onChange(e.currentTarget.value);
26
31
  };
@@ -41,6 +46,7 @@ export default function Input({
41
46
  onKeyPress={handleKeyPress}
42
47
  placeholder={placeholder}
43
48
  value={value}
49
+ {...attrs}
44
50
  />
45
51
  {error && <div>{error}</div>}
46
52
  </>
package/src/Login.tsx CHANGED
@@ -66,6 +66,7 @@ export default function Login() {
66
66
  label="Username"
67
67
  value={values.username}
68
68
  onChange={(value) => setValues({ ...values, username: value })}
69
+ autoCapitalize="off"
69
70
  />
70
71
  <Input
71
72
  name="password"
package/src/Register.tsx CHANGED
@@ -63,6 +63,7 @@ export default function Register() {
63
63
  value={values.username}
64
64
  onChange={(value) => setValues({ ...values, username: value })}
65
65
  error={errors.username}
66
+ autoCapitalize="off"
66
67
  />
67
68
  <Input
68
69
  name="password"
package/src/index.tsx CHANGED
@@ -1,4 +1,17 @@
1
- import ReactDOM from "react-dom";
1
+ import { createRoot } from "react-dom/client";
2
2
  import App from "./App";
3
3
 
4
- ReactDOM.render(<App />, document.getElementById("root"));
4
+ const root = createRoot(
5
+ document.body.appendChild(document.createElement("div"))
6
+ );
7
+ root.render(<App />);
8
+
9
+ const link = document.createElement("link");
10
+ link.setAttribute("rel", "stylesheet");
11
+ link.setAttribute("type", "text/css");
12
+ link.setAttribute(
13
+ "href",
14
+ "https://cdn.jsdelivr.net/npm/water.css@2/out/water.min.css"
15
+ );
16
+
17
+ document.getElementsByTagName("head")[0].appendChild(link);
package/index.html DELETED
@@ -1,18 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
-
4
- <head>
5
- <meta charset="utf-8">
6
- <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
7
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.min.css">
8
- <title>App</title>
9
- </head>
10
-
11
- <body>
12
- <noscript>
13
- You need to enable JavaScript to run this app.
14
- </noscript>
15
- <div id="root"></div>
16
- </body>
17
-
18
- </html>