-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom field response display
- Loading branch information
Showing
5 changed files
with
144 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Fragment } from "react"; | ||
|
||
import useSWR from "swr"; | ||
|
||
import { fetcher } from "@/lib/utils"; | ||
|
||
type CustomFieldResponse = { | ||
identifier: string; | ||
label: string; | ||
response: string; | ||
}; | ||
|
||
export default function VisitorCustomFields({ | ||
viewId, | ||
teamId, | ||
documentId, | ||
}: { | ||
viewId: string; | ||
teamId: string; | ||
documentId: string; | ||
}) { | ||
const { data: customFieldResponse } = useSWR<CustomFieldResponse[] | null>( | ||
`/api/teams/${teamId}/documents/${documentId}/views/${viewId}/custom-fields`, | ||
fetcher, | ||
); | ||
|
||
console.log("customFieldResponse", customFieldResponse); | ||
|
||
if (!customFieldResponse) return null; | ||
|
||
return ( | ||
<div className="space-y-2 px-1.5 pb-2 md:px-2"> | ||
<dl className="grid grid-cols-[auto_1fr] gap-x-4 gap-y-2"> | ||
{customFieldResponse.map((field, index) => ( | ||
<Fragment key={index}> | ||
<dt className="text-sm text-muted-foreground">{field.label}</dt> | ||
<dd className="text-sm">{field.response}</dd> | ||
</Fragment> | ||
))} | ||
</dl> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
pages/api/teams/[teamId]/documents/[id]/views/[viewId]/custom-fields.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { authOptions } from "@/pages/api/auth/[...nextauth]"; | ||
import { getServerSession } from "next-auth/next"; | ||
|
||
import { errorhandler } from "@/lib/errorHandler"; | ||
import prisma from "@/lib/prisma"; | ||
import { CustomUser } from "@/lib/types"; | ||
|
||
export default async function handle( | ||
req: NextApiRequest, | ||
res: NextApiResponse, | ||
) { | ||
if (req.method === "GET") { | ||
// GET /api/teams/:teamId/documents/:id/views/:viewId/custom-fields | ||
const session = await getServerSession(req, res, authOptions); | ||
if (!session) { | ||
return res.status(401).end("Unauthorized"); | ||
} | ||
|
||
const { | ||
teamId, | ||
id: docId, | ||
viewId, | ||
} = req.query as { | ||
teamId: string; | ||
id: string; | ||
viewId: string; | ||
}; | ||
|
||
const userId = (session.user as CustomUser).id; | ||
|
||
try { | ||
const team = await prisma.team.findUnique({ | ||
where: { | ||
id: teamId, | ||
users: { | ||
some: { | ||
userId: userId, | ||
}, | ||
}, | ||
}, | ||
select: { | ||
id: true, | ||
plan: true, | ||
}, | ||
}); | ||
|
||
if (!team) { | ||
return res.status(401).end("Unauthorized"); | ||
} | ||
|
||
if (team.plan.includes("free")) { | ||
return res.status(403).end("Forbidden"); | ||
} | ||
|
||
const customFields = await prisma.customFieldResponse.findFirst({ | ||
where: { | ||
viewId: viewId, | ||
}, | ||
select: { | ||
data: true, | ||
}, | ||
}); | ||
|
||
const data = customFields?.data; | ||
|
||
return res.status(200).json(data); | ||
} catch (error) { | ||
errorhandler(error, res); | ||
} | ||
} else { | ||
// We only allow GET requests | ||
res.setHeader("Allow", ["GET"]); | ||
return res.status(405).end(`Method ${req.method} Not Allowed`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters