This repository was archived by the owner on Sep 29, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathindex.tsx
More file actions
119 lines (110 loc) · 3.23 KB
/
Copy pathindex.tsx
File metadata and controls
119 lines (110 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import Head from 'next/head';
import Link from 'next/link';
import { ReactQueryDevtools } from 'react-query/devtools';
import { trpc } from '../utils/trpc';
export default function IndexPage() {
const utils = trpc.useContext();
const postsQuery = trpc.proxy.post.all.useQuery();
const addPost = trpc.proxy.post.add.useMutation({
onSuccess: () => utils.invalidateQueries('post.all'),
});
// prefetch all posts for instant navigation
// useEffect(() => {
// postsQuery.data?.forEach((post) => {
// utils.prefetchQuery(['post.byId', post.id]);
// });
// }, [postsQuery.data, utils]);
return (
<>
<Head>
<title>Prisma Starter</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<h1>Welcome to your tRPC starter!</h1>
<p>
Check <a href="https://trpc.io/docs">the docs</a> whenever you get
stuck, or ping <a href="https://twitter.com/alexdotjs">@alexdotjs</a> on
Twitter.
</p>
<h2>
Posts
{postsQuery.status === 'loading' && '(loading)'}
</h2>
{postsQuery.data?.map((item) => (
<article key={item.id}>
<h3>{item.title}</h3>
<Link href={`/post/${item.id}`}>
<a>View more</a>
</Link>
</article>
))}
<form
onSubmit={async (e) => {
e.preventDefault();
/**
* In a real app you probably don't want to use this manually
* Checkout React Hook Form - it works great with tRPC
* @link https://react-hook-form.com/
*/
const $text: HTMLInputElement = (e as any).target.elements.text;
const $title: HTMLInputElement = (e as any).target.elements.title;
const input = {
title: $title.value,
text: $text.value,
};
try {
await addPost.mutateAsync(input);
$title.value = '';
$text.value = '';
} catch {}
}}
>
<label htmlFor="title">Title:</label>
<br />
<input
id="title"
name="title"
type="text"
disabled={addPost.isLoading}
/>
<br />
<label htmlFor="text">Text:</label>
<br />
<textarea id="text" name="text" disabled={addPost.isLoading} />
<br />
<input type="submit" disabled={addPost.isLoading} />
{addPost.error && (
<p style={{ color: 'red' }}>{addPost.error.message}</p>
)}
</form>
{process.env.NODE_ENV !== 'production' && (
<ReactQueryDevtools initialIsOpen={false} />
)}
</>
);
}
/**
* If you want to statically render this page
* - Export `appRouter` & `createContext` from [trpc].ts
* - Make the `opts` object optional on `createContext()`
*
* @link https://trpc.io/docs/ssg
*/
// export const getStaticProps = async (
// context: GetStaticPropsContext<{ filter: string }>,
// ) => {
// const ssg = createSSGHelpers({
// router: appRouter,
// ctx: await createContext(),
// });
//
// await ssg.fetchQuery('post.all');
//
// return {
// props: {
// trpcState: ssg.dehydrate(),
// filter: context.params?.filter ?? 'all',
// },
// revalidate: 1,
// };
// };