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