Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 613aa3b

Browse files
committed
Get members details from client side api
1 parent 5527681 commit 613aa3b

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

src/components/members/Member.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ export default function Member({ user }: { user: User }) {
4343
<li className="col-span-1 flex flex-col text-center bg-white rounded-lg shadow divide-y divide-gray-200">
4444
<div className="flex-1 flex flex-col p-8">
4545
<Avatar src={user.image} className="w-32 h-32 mx-auto" />
46-
<h3 className="mt-6 text-gray-900 text-sm font-medium">{user.name}</h3>
46+
<h3 className="mt-6 text-gray-900 text-sm font-medium">
47+
{user.account?.firstName ?? user.name}
48+
</h3>
4749
<dl className="mt-1 flex-grow flex flex-col justify-between">
4850
<dt className="sr-only">Bio</dt>
4951
<dd className="text-gray-500 text-sm">

src/pages/api/fauna/members.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
2+
3+
import faunadb from 'faunadb'
4+
import { User } from 'src/pages/members'
5+
const q = faunadb.query
6+
const isProduction = process.env.NODE_ENV === 'production'
7+
const client = new faunadb.Client({
8+
secret: process.env.FAUNADB_SECRET ?? 'secret',
9+
scheme: isProduction ? 'https' : 'http',
10+
domain: isProduction ? 'db.fauna.com' : 'localhost',
11+
...(isProduction ? {} : { port: 8443 }),
12+
})
13+
14+
const FaunaCreateHandler: NextApiHandler = async (
15+
req: NextApiRequest,
16+
res: NextApiResponse
17+
) => {
18+
try {
19+
const response: any = await client.query(
20+
q.Map(q.Paginate(q.Documents(q.Collection('users'))), (userRef) => {
21+
const user = q.Get(userRef)
22+
return {
23+
id: q.Select(['ref', 'id'], user),
24+
name: q.Select(['data', 'name'], user, null),
25+
username: q.Select(['data', 'username'], user, null),
26+
image: q.Select(['data', 'image'], user, null),
27+
account: {
28+
firstName: q.Select(['data', 'account', 'firstName'], user, ''),
29+
},
30+
socials: q.Select(['data', 'socials'], user, {}),
31+
}
32+
})
33+
)
34+
const users: User[] = response.data
35+
36+
res.status(200).json({ users })
37+
} catch (error) {
38+
console.error({ msg: error.message })
39+
res.status(500).json({ message: error.message })
40+
}
41+
}
42+
43+
export default FaunaCreateHandler

src/pages/members.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PaddedLayout } from 'src/layouts'
33
import { InferGetServerSidePropsType } from 'next'
44

55
import faunadb from 'faunadb'
6+
import { useQuery } from 'react-query'
67
const q = faunadb.query
78
const isProduction = process.env.NODE_ENV === 'production'
89
const client = new faunadb.Client({
@@ -12,12 +13,24 @@ const client = new faunadb.Client({
1213
...(isProduction ? {} : { port: 8443 }),
1314
})
1415

15-
export default function MembersPage({
16-
users,
17-
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
16+
export default function MembersPage() {
17+
const { isLoading, isError, data } = useQuery('/api/fauna/members', () =>
18+
fetch(`/api/fauna/members`).then((res) => {
19+
if (!res.ok) {
20+
throw new Error('Something went wrong!!')
21+
}
22+
return res.json()
23+
})
24+
)
25+
if (isLoading) {
26+
return <p>loading...</p>
27+
}
28+
if (isError) {
29+
return <p>Something went wrong!!!</p>
30+
}
1831
return (
1932
<>
20-
<Members users={users} />
33+
<Members users={data.users} />
2134
</>
2235
)
2336
}

0 commit comments

Comments
 (0)