Open History to see the last commit
1import { Match, Show, Switch, createSignal } from 'solid-js'
2import { Link, createFileRoute } from '@tanstack/solid-router'
3import {
4 onlineManager,
5 useMutation,
6 useQuery,
7 useQueryClient,
8} from '@tanstack/solid-query'
9import { moviesMutations, moviesQueries } from '../services/movies/queries'
10β
11export const Route = createFileRoute('/$movieId')({
12 loader: ({ context: { queryClient, isRestoring }, params: { movieId } }) =>
13 queryClient.getQueryData(moviesQueries.detail(movieId).queryKey) ??
14 // do not load if we are offline or hydrating because it returns a promise that is pending until we go online again
15 // we just let the Detail component handle it
16 (onlineManager.isOnline() && !isRestoring()
17 ? queryClient.query(moviesQueries.detail(movieId))
18 : undefined),
19 component: Detail,
20})
21β
22function Detail() {
23 const params = Route.useParams()
24 const queryClient = useQueryClient()
25 const movieQuery = useQuery(() => moviesQueries.detail(params().movieId))
26β
27 const [commentDraft, setCommentDraft] = createSignal<string | undefined>()
28β
29 const updateMovie = useMutation(() => ({
30 ...moviesMutations.update(),
31 mutationKey: moviesQueries.detail(params().movieId).queryKey,
32 onMutate: async (variables) => {
33 await queryClient.cancelQueries({
34 queryKey: moviesQueries.detail(params().movieId).queryKey,
35 })
36 const { queryKey } = moviesQueries.detail(params().movieId)
37 const previousData = queryClient.getQueryData(queryKey)
38β
39 // remove local state so that server state is taken instead
40 setCommentDraft(undefined)
41β
42 queryClient.setQueryData(
43 queryKey,
44 (old) =>
45 old && {
46 ...old,
47 movie: {
48 ...old.movie,
49 comment: variables.comment,
50 },
51 },
52 )
53β
54 return { previousData }
55 },
56 onError: (_, __, onMutateResult) => {
57 queryClient.setQueryData(
58 moviesQueries.detail(params().movieId).queryKey,
59 onMutateResult?.previousData,
60 )
61 },
62 onSettled: () => {
63 queryClient.invalidateQueries({
64 queryKey: moviesQueries.detail(params().movieId).queryKey,
65 })
66 },
67 }))
68β
69 const comment = () => commentDraft() ?? movieQuery.data?.movie.comment
70β
71 function submitForm(event: SubmitEvent) {
72 event.preventDefault()
73β
74 const currentComment = comment()
75 if (currentComment === undefined) {
76 return
77 }
78β
79 updateMovie.mutate({
80 id: params().movieId,
81 comment: currentComment,
82 })
83 }
84β
85 return (
86 <Switch>
87 <Match when={movieQuery.isLoading}>Loading...</Match>
88 <Match when={movieQuery.data}>
89 <form onSubmit={submitForm}>
90 <Link to="/">Back</Link>
91 <h1>Movie: {movieQuery.data!.movie.title}</h1>
92 <p>
93 Try to mock offline behavior with the button in the devtools, then
94 update the comment. The optimistic update will succeed, but the
95 actual mutation will be paused and resumed once you go online again.
96 </p>
97 <p>
98 You can also reload the page, which will make the persisted mutation
99 resume, as you will be online again when you "come back".
100 </p>
101 <p>
102 <label>
103 Comment: <br />
104 <textarea
105 name="comment"
106 value={comment() ?? ''}
107 onInput={(event) => setCommentDraft(event.currentTarget.value)}
108 />
109 </label>
110 </p>
111 <button type="submit">Submit</button>
112 <div>
113 Updated at: {new Date(movieQuery.data!.ts).toLocaleTimeString()}
114 </div>
115 <Show when={movieQuery.isFetching}>
116 <div>fetching...</div>
117 </Show>
118 <div>
119 {updateMovie.isPaused
120 ? 'mutation paused - offline'
121 : updateMovie.isPending && 'updating...'}
122 </div>
123 </form>
124 </Match>
125 <Match when={movieQuery.isError}>
126 <div>
127 <Link to="/">Back</Link>
128 <h1>Couldn't load movie!</h1>
129 <div>{movieQuery.error?.message}</div>
130 </div>
131 </Match>
132 <Match when={movieQuery.isPaused}>
133 We're offline and have no data to show :(
134 </Match>
135 </Switch>
136 )
137}