({didSubmit})
| 6 | |
| 7 | |
| 8 | export default function LinksCreateForm ({didSubmit}) { |
| 9 | const [results, setResults] = useState(null) |
| 10 | const [message, setMessage] = useState(null) |
| 11 | |
| 12 | const handleForm = async (event) => { |
| 13 | event.preventDefault() |
| 14 | const formData = new FormData(event.target) |
| 15 | const data = Object.fromEntries(formData) |
| 16 | const JSONData = JSON.stringify(data) |
| 17 | const endpoint = "/api/links/" |
| 18 | const options = { |
| 19 | method: "POST", |
| 20 | headers: { |
| 21 | "Content-Type": "application/json" |
| 22 | }, |
| 23 | body: JSONData |
| 24 | } |
| 25 | const response = await fetch(endpoint, options) |
| 26 | |
| 27 | const result = await response.json() |
| 28 | |
| 29 | setResults(result) |
| 30 | if (didSubmit) { |
| 31 | didSubmit(result) |
| 32 | } |
| 33 | if (result.message) { |
| 34 | setMessage(result.message) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return <> |
| 39 | {message && <Alert color="warning">{message}</Alert>} |
| 40 | <form className="flex max-w-md flex-col gap-4" onSubmit={handleForm}> |
| 41 | |
| 42 | <div> |
| 43 | <div className="mb-2 block"> |
| 44 | <Label |
| 45 | htmlFor="url" |
| 46 | value="Enter a link to shorten" |
| 47 | /> |
| 48 | </div> |
| 49 | <TextInput |
| 50 | id="url" |
| 51 | placeholder="Your url to shorten" |
| 52 | required |
| 53 | name="url" |
| 54 | type="text" |
| 55 | /> |
| 56 | </div> |
| 57 | <Button type="submit"> |
| 58 | Shorten |
| 59 | </Button> |
| 60 | |
| 61 | </form> |
| 62 | </> |
| 63 | } |
nothing calls this directly
no outgoing calls
no test coverage detected