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)
const linkedTypebotIds = localTypebot?.groups
.flatMap((b) => b.blocks)
.reduce<string[]>(
(typebotIds, block) =>
block.type === LogicBlockType.TYPEBOT_LINK &&
isDefined(block.options.typebotId)
? [...typebotIds, block.options.typebotId]
: typebotIds,
[]
)
const linkedTypebotIds =
localTypebot?.groups
.flatMap((b) => b.blocks)
.reduce<string[]>(
(typebotIds, block) =>
block.type === LogicBlockType.TYPEBOT_LINK &&
isDefined(block.options.typebotId) &&
!typebotIds.includes(block.options.typebotId)
? [...typebotIds, block.options.typebotId]
: typebotIds,
[]
) ?? []
const { typebots: linkedTypebots } = useLinkedTypebots({
workspaceId: localTypebot?.workspaceId ?? undefined,

View File

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

View File

@ -1,4 +1,4 @@
import { Plan, WorkspaceRole } from 'db'
import { Plan } from 'db'
import prisma from '@/lib/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import {
@ -18,67 +18,29 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
try {
if (req.method === 'GET') {
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)
const typebotIds = req.query.typebotIds as string[] | undefined
if (typebotIds) {
const typebots = await prisma.typebot.findMany({
where: {
OR: [
{
workspace: { members: { some: { userId: user.id } } },
id: { in: typebotIds },
isArchived: { not: true },
},
{
id: { in: typebotIds },
collaborators: {
some: {
userId: user.id,
},
},
isArchived: { not: true },
},
],
},
orderBy: { createdAt: 'desc' },
select: { name: true, id: true, groups: true, variables: true },
})
return res.send({ typebots })
}
const typebotIds = req.query.typebotIds as string[]
const typebots = await prisma.typebot.findMany({
where: {
OR: [
{
workspace: { members: { some: { userId: user.id } } },
id: { in: typebotIds },
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 },
id: { in: typebotIds },
collaborators: {
some: {
userId: user.id,
},
},
isArchived: { not: true },
},
],
},
orderBy: { createdAt: 'desc' },
select: { name: true, id: true, icon: true },
select: { name: true, id: true, groups: true, variables: true },
})
return res.send({ typebots })
}