Skip to content

Commit fc1550f

Browse files
authored
Update tutorials for v0.11.6 (#1504)
1 parent d9d04a3 commit fc1550f

File tree

29 files changed

+538
-126
lines changed

29 files changed

+538
-126
lines changed

examples/tutorials/TodoApp/main.wasp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
app TodoApp {
22
wasp: {
3-
version: "^0.11.0"
3+
version: "^0.11.6" // Pins the version of Wasp to use.
44
},
5-
6-
title: "Todo app",
7-
5+
title: "Todo app", // Used as the browser tab title. Note that all strings in Wasp are double quoted!
86
auth: {
7+
// Tells Wasp which entity to use for storing users.
98
userEntity: User,
109
methods: {
10+
// Enable username and password auth.
1111
usernameAndPassword: {}
1212
},
13+
// We'll see how this is used a bit later.
1314
onAuthFailedRedirectTo: "/login"
14-
},
15-
16-
dependencies: [
17-
("react-clock", "3.0.0")
18-
]
15+
}
1916
}
2017

2118
route RootRoute { path: "/", to: MainPage }
2219
page MainPage {
20+
// We specify that the React implementation of the page is the default export
21+
// of `src/client/MainPage.jsx`. This statement uses standard JS import syntax.
22+
// Use `@client` to reference files inside the `src/client` folder.
2323
authRequired: true,
2424
component: import Main from "@client/MainPage.jsx"
2525
}
@@ -35,22 +35,26 @@ page LoginPage {
3535
}
3636

3737
entity User {=psl
38-
id Int @id @default(autoincrement())
39-
username String @unique
40-
password String
41-
tasks Task[]
38+
id Int @id @default(autoincrement())
39+
username String @unique
40+
password String
41+
tasks Task[]
4242
psl=}
4343

4444
entity Task {=psl
4545
id Int @id @default(autoincrement())
4646
description String
4747
isDone Boolean @default(false)
48-
user User? @relation(fields: [userId], references: [id])
48+
user User? @relation(fields: [userId], references: [id])
4949
userId Int?
5050
psl=}
5151

5252
query getTasks {
53+
// Specifies where the implementation for the query function is.
54+
// Use `@server` to import files inside the `src/server` folder.
5355
fn: import { getTasks } from "@server/queries.js",
56+
// Tell Wasp that this query reads from the `Task` entity. By doing this, Wasp
57+
// will automatically update the results of this query when tasks are modified.
5458
entities: [Task]
5559
}
5660

examples/tutorials/TodoApp/src/client/Clocks.jsx

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import { Link } from 'react-router-dom'
2-
32
import { LoginForm } from '@wasp/auth/forms/Login'
43

54
const LoginPage = () => {
65
return (
7-
<>
8-
<div style={{maxWidth: "400px", margin: "0 auto"}}>
9-
<LoginForm/>
10-
<br/>
11-
<span>
12-
I don't have an account yet (<Link to="/signup">go to signup</Link>).
13-
</span>
14-
</div>
15-
</>
6+
<div style={{maxWidth: "400px", margin: "0 auto"}}>
7+
<LoginForm />
8+
<br />
9+
<span>
10+
I don't have an account yet (<Link to="/signup">go to signup</Link>).
11+
</span>
12+
</div>
1613
)
1714
}
1815

19-
export default LoginPage
16+
export default LoginPage
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
1-
import { useQuery } from '@wasp/queries'
21
import getTasks from '@wasp/queries/getTasks'
32
import createTask from '@wasp/actions/createTask'
3+
import { useQuery } from '@wasp/queries'
44
import updateTask from '@wasp/actions/updateTask'
55
import logout from '@wasp/auth/logout'
6-
import Clocks from './Clocks'
76

87
const MainPage = () => {
9-
const { data: tasks, isFetching, error } = useQuery(getTasks)
8+
const { data: tasks, isLoading, error } = useQuery(getTasks)
109

1110
return (
1211
<div>
1312
<NewTaskForm />
1413

1514
{tasks && <TasksList tasks={tasks} />}
1615

17-
<div> <Clocks /> </div>
18-
19-
{isFetching && 'Fetching...'}
16+
{isLoading && 'Loading...'}
2017
{error && 'Error: ' + error}
21-
22-
<button onClick={logout}> Logout </button>
18+
<button onClick={logout}>Logout</button>
2319
</div>
2420
)
2521
}
2622

27-
const Task = (props) => {
23+
const Task = ({ task }) => {
2824
const handleIsDoneChange = async (event) => {
2925
try {
3026
await updateTask({
31-
taskId: props.task.id,
32-
data: { isDone: event.target.checked }
27+
id: task.id,
28+
isDone: event.target.checked,
3329
})
3430
} catch (error) {
3531
window.alert('Error while updating task: ' + error.message)
@@ -38,26 +34,35 @@ const Task = (props) => {
3834
return (
3935
<div>
4036
<input
41-
type='checkbox' id={props.task.id}
42-
checked={props.task.isDone}
37+
type="checkbox"
38+
id={String(task.id)}
39+
checked={task.isDone}
4340
onChange={handleIsDoneChange}
4441
/>
45-
{props.task.description}
42+
{task.description}
4643
</div>
4744
)
4845
}
4946

50-
const TasksList = (props) => {
51-
if (!props.tasks?.length) return 'No tasks'
52-
return props.tasks.map((task, idx) => <Task task={task} key={idx} />)
47+
const TasksList = ({ tasks }) => {
48+
if (!tasks?.length) return <div>No tasks</div>
49+
50+
return (
51+
<div>
52+
{tasks.map((task, idx) => (
53+
<Task task={task} key={idx} />
54+
))}
55+
</div>
56+
)
5357
}
5458

55-
const NewTaskForm = (props) => {
59+
const NewTaskForm = () => {
5660
const handleSubmit = async (event) => {
5761
event.preventDefault()
5862
try {
59-
const description = event.target.description.value
60-
event.target.reset()
63+
const target = event.target
64+
const description = target.description.value
65+
target.reset()
6166
await createTask({ description })
6267
} catch (err) {
6368
window.alert('Error: ' + err.message)
@@ -66,15 +71,10 @@ const NewTaskForm = (props) => {
6671

6772
return (
6873
<form onSubmit={handleSubmit}>
69-
<input
70-
name='description'
71-
type='text'
72-
defaultValue=''
73-
/>
74-
<input type='submit' value='Create task' />
74+
<input name="description" type="text" defaultValue="" />
75+
<input type="submit" value="Create task" />
7576
</form>
7677
)
7778
}
7879

7980
export default MainPage
80-
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import { Link } from 'react-router-dom'
2-
32
import { SignupForm } from '@wasp/auth/forms/Signup'
43

54
const SignupPage = () => {
65
return (
7-
<>
8-
<div style={{maxWidth: "400px", margin: "0 auto"}}>
9-
<SignupForm/>
10-
<br/>
11-
<span>
12-
I already have an account (<Link to="/login">go to login</Link>).
13-
</span>
14-
</div>
15-
</>
6+
<div style={{maxWidth: "400px", margin: "0 auto"}}>
7+
<SignupForm />
8+
<br />
9+
<span>
10+
I already have an account (<Link to="/login">go to login</Link>).
11+
</span>
12+
</div>
1613
)
1714
}
1815

19-
export default SignupPage
16+
export default SignupPage
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vite'
2+
3+
export default defineConfig({
4+
server: {
5+
open: true,
6+
},
7+
})
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import HttpError from '@wasp/core/HttpError.js'
22

33
export const createTask = async (args, context) => {
4-
if (!context.user) { throw new HttpError(401) }
4+
if (!context.user) {
5+
throw new HttpError(401)
6+
}
57
return context.entities.Task.create({
68
data: {
79
description: args.description,
8-
user: { connect: { id: context.user.id } }
9-
}
10+
user: { connect: { id: context.user.id } },
11+
},
1012
})
1113
}
1214

1315
export const updateTask = async (args, context) => {
14-
if (!context.user) { throw new HttpError(401) }
16+
if (!context.user) {
17+
throw new HttpError(401)
18+
}
1519
return context.entities.Task.updateMany({
16-
where: { id: args.taskId, user: { id: context.user.id } },
17-
data: { isDone: args.data.isDone }
20+
where: { id: args.id, user: { id: context.user.id } },
21+
data: { isDone: args.isDone },
1822
})
1923
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import HttpError from '@wasp/core/HttpError.js'
22

33
export const getTasks = async (args, context) => {
4-
if (!context.user) { throw new HttpError(401) }
5-
return context.entities.Task.findMany(
6-
{ where: { user: { id: context.user.id } } }
7-
)
4+
if (!context.user) {
5+
throw new HttpError(401)
6+
}
7+
return context.entities.Task.findMany({
8+
where: { user: { id: context.user.id } },
9+
})
810
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/.wasp/
2+
3+
# We ignore env files recognized and used by Wasp.
4+
.env.server
5+
.env.client
6+
7+
# To be extra safe, we by default ignore any files with `.env` extension in them.
8+
# If this is too agressive for you, consider allowing specific files with `!` operator,
9+
# or modify/delete these two lines.
10+
*.env
11+
*.env.*
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
File marking the root of Wasp project.

0 commit comments

Comments
 (0)