-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c5573c
commit 187136e
Showing
5 changed files
with
66 additions
and
24 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
import { Action, type ActionParams } from "../api"; | ||
import { ensureNumber } from "../util/formatters"; | ||
import { ensureString } from "../util/formatters"; | ||
|
||
export class Hello extends Action { | ||
constructor() { | ||
super({ | ||
name: "hello", | ||
web: { route: "/hello", method: "POST" }, | ||
inputs: { | ||
name: { required: true }, | ||
number: { | ||
name: { | ||
required: true, | ||
default: 42, | ||
formatter: ensureNumber, | ||
validator: (p) => p.length > 0, | ||
formatter: ensureString, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
async run(params: ActionParams<Hello>) { | ||
return { message: `Hello, ${params.name} (${params.number})!` }; | ||
return { message: `Hello, ${params.name}!` }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Button, Form } from "react-bootstrap"; | ||
import type { ActionResponse } from "../api"; | ||
import type { Hello } from "../actions/hello"; | ||
import { useState } from "react"; | ||
|
||
export const HelloCard = () => { | ||
const [error, setError] = useState<string>(); | ||
const [message, setMessage] = useState<string>(); | ||
|
||
async function handleForm(event: React.SyntheticEvent) { | ||
event.preventDefault(); | ||
setError(undefined); | ||
setMessage(undefined); | ||
|
||
const target = event.target as typeof event.target & { | ||
name: { value: string }; | ||
}; | ||
const name = target.name.value; | ||
const body = new FormData(); | ||
body.append("name", name); | ||
const response = (await fetch("/api/hello", { method: "post", body }).then( | ||
(res) => res.json(), | ||
)) as ActionResponse<Hello>; | ||
|
||
if (response.error) { | ||
setError(response.error.message); | ||
} else { | ||
setMessage(response.message); | ||
} | ||
} | ||
|
||
return ( | ||
<div> | ||
<Form onSubmit={handleForm}> | ||
<Form.Group className="mb-3" controlId="name"> | ||
<Form.Label>Name</Form.Label> | ||
<Form.Control type="text" placeholder="Enter name" /> | ||
</Form.Group> | ||
|
||
<Button variant="primary" type="submit"> | ||
Submit | ||
</Button> | ||
</Form> | ||
|
||
<br /> | ||
{error && <div className="alert alert-danger">{error}</div>} | ||
{message && <div className="alert alert-success">{message}</div>} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters