2
0

🐛 (typebotLink) Fix typebotIds infinite query param

This commit is contained in:
Baptiste Arnaud
2023-02-15 16:10:32 +01:00
parent 44d7740952
commit 2f7e71f66e
3 changed files with 25 additions and 61 deletions

View File

@@ -124,16 +124,18 @@ export const TypebotProvider = ({
}, },
] = useUndo<Typebot | undefined>(undefined) ] = useUndo<Typebot | undefined>(undefined)
const linkedTypebotIds = localTypebot?.groups const linkedTypebotIds =
localTypebot?.groups
.flatMap((b) => b.blocks) .flatMap((b) => b.blocks)
.reduce<string[]>( .reduce<string[]>(
(typebotIds, block) => (typebotIds, block) =>
block.type === LogicBlockType.TYPEBOT_LINK && block.type === LogicBlockType.TYPEBOT_LINK &&
isDefined(block.options.typebotId) isDefined(block.options.typebotId) &&
!typebotIds.includes(block.options.typebotId)
? [...typebotIds, block.options.typebotId] ? [...typebotIds, block.options.typebotId]
: typebotIds, : typebotIds,
[] []
) ) ?? []
const { typebots: linkedTypebots } = useLinkedTypebots({ const { typebots: linkedTypebots } = useLinkedTypebots({
workspaceId: localTypebot?.workspaceId ?? undefined, workspaceId: localTypebot?.workspaceId ?? undefined,

View File

@@ -11,7 +11,7 @@ export const useLinkedTypebots = ({
}: { }: {
workspaceId?: string workspaceId?: string
typebotId?: string typebotId?: string
typebotIds?: string[] typebotIds: string[]
onError: (error: Error) => void onError: (error: Error) => void
}) => { }) => {
const params = stringify({ typebotIds, workspaceId }, { indices: false }) const params = stringify({ typebotIds, workspaceId }, { indices: false })
@@ -21,8 +21,8 @@ export const useLinkedTypebots = ({
}, },
Error Error
>( >(
workspaceId workspaceId && typebotIds.length > 0
? typebotIds?.every((id) => typebotId === id) ? typebotIds.every((id) => typebotId === id)
? undefined ? undefined
: `/api/typebots?${params}` : `/api/typebots?${params}`
: null, : null,

View File

@@ -1,4 +1,4 @@
import { Plan, WorkspaceRole } from 'db' import { Plan } from 'db'
import prisma from '@/lib/prisma' import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
import { import {
@@ -18,14 +18,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
try { try {
if (req.method === 'GET') { if (req.method === 'GET') {
const workspaceId = req.query.workspaceId as string | undefined const workspaceId = req.query.workspaceId as string | undefined
const folderId = req.query.allFolders
? undefined
: req.query.folderId
? req.query.folderId.toString()
: null
if (!workspaceId) return badRequest(res) if (!workspaceId) return badRequest(res)
const typebotIds = req.query.typebotIds as string[] | undefined const typebotIds = req.query.typebotIds as string[]
if (typebotIds) {
const typebots = await prisma.typebot.findMany({ const typebots = await prisma.typebot.findMany({
where: { where: {
OR: [ OR: [
@@ -50,38 +44,6 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}) })
return res.send({ typebots }) return res.send({ typebots })
} }
const typebots = await prisma.typebot.findMany({
where: {
OR: [
{
isArchived: { not: true },
folderId,
workspace: {
id: workspaceId,
members: {
some: {
userId: user.id,
role: { not: WorkspaceRole.GUEST },
},
},
},
},
{
isArchived: { not: true },
workspace: {
id: workspaceId,
members: {
some: { userId: user.id, role: WorkspaceRole.GUEST },
},
},
},
],
},
orderBy: { createdAt: 'desc' },
select: { name: true, id: true, icon: true },
})
return res.send({ typebots })
}
if (req.method === 'POST') { if (req.method === 'POST') {
const workspace = await prisma.workspace.findFirst({ const workspace = await prisma.workspace.findFirst({
where: { id: req.body.workspaceId }, where: { id: req.body.workspaceId },