|
9 | 9 | // `useSubmissions(action)` lists a failed save's `{ error }` until the next |
10 | 10 | // attempt at the same thing settles. After a form posted without JavaScript |
11 | 11 | // the router seeds the same list from its flash cookie, so the failures |
12 | | -// render on the server too. |
| 12 | +// render on the server too. A retry is the same form again, built from the |
| 13 | +// submission's own input, so it works there as well. |
13 | 14 | import { useSubmissions, type RouteProps, type Submission } from "@solidjs/router"; |
14 | 15 | import { For } from "solid-js"; |
15 | | -import { dynamicComponent } from "@solidjs/web"; |
16 | | -import { clearCompleted, removeTodo, toggleAll, toggleTodo } from "~/actions"; |
| 16 | +import { dynamicComponent, type JSX } from "@solidjs/web"; |
| 17 | +import { clearCompleted, completedOf, removeTodo, toggleAll, toggleTodo } from "~/actions"; |
17 | 18 | import { getTodoList, parseFilter } from "~/todo-list"; |
18 | 19 |
|
19 | 20 | type Answer = Submission<any[], { error: string } | undefined>; |
@@ -46,14 +47,43 @@ function Failures() { |
46 | 47 | }); |
47 | 48 | clearCompleted.onSettled(supersede(clears)); |
48 | 49 |
|
49 | | - const failed = () => |
50 | | - [...toggles, ...removals, ...markAll, ...clears].filter(s => s.result?.error); |
| 50 | + /** The form that asks again: the action bound to the same todo, and the |
| 51 | + * button carrying the same `completed` the first attempt submitted. */ |
| 52 | + const again = (subs: Answer[], retry: (s: Answer) => JSX.Element) => |
| 53 | + subs.filter(s => s.result?.error).map(s => ({ s, retry })); |
| 54 | + const completed = (form: FormData) => (completedOf(form) ? "true" : "false"); |
| 55 | + const failed = () => [ |
| 56 | + ...again(toggles, s => ( |
| 57 | + <form action={toggleTodo.with(s.input[0])} method="post"> |
| 58 | + <button name="completed" value={completed(s.input[1])}> |
| 59 | + Retry |
| 60 | + </button> |
| 61 | + </form> |
| 62 | + )), |
| 63 | + ...again(removals, s => ( |
| 64 | + <form action={removeTodo.with(s.input[0])} method="post"> |
| 65 | + <button>Retry</button> |
| 66 | + </form> |
| 67 | + )), |
| 68 | + ...again(markAll, s => ( |
| 69 | + <form action={toggleAll} method="post"> |
| 70 | + <button name="completed" value={completed(s.input[0])}> |
| 71 | + Retry |
| 72 | + </button> |
| 73 | + </form> |
| 74 | + )), |
| 75 | + ...again(clears, () => ( |
| 76 | + <form action={clearCompleted} method="post"> |
| 77 | + <button>Retry</button> |
| 78 | + </form> |
| 79 | + )) |
| 80 | + ]; |
51 | 81 | return ( |
52 | 82 | <ul class="failures" hidden={failed().length === 0}> |
53 | 83 | <For each={failed()}> |
54 | | - {s => ( |
| 84 | + {f => ( |
55 | 85 | <li> |
56 | | - {s.result!.error}. <button onClick={() => s.retry()}>Retry</button> |
| 86 | + {f.s.result!.error}. {f.retry(f.s)} |
57 | 87 | </li> |
58 | 88 | )} |
59 | 89 | </For> |
|
0 commit comments