Skip to content

Commit

Permalink
feat(db): 🗃️ Remove duplicate fields in PublicTypebot
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jun 4, 2022
1 parent 8ec117a commit ad32ae0
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 47 deletions.
2 changes: 2 additions & 0 deletions apps/builder/components/auth/SignInForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Stack,
HStack,
Text,
Spinner,
} from '@chakra-ui/react'
import React, { ChangeEvent, FormEvent, useEffect } from 'react'
import { useState } from 'react'
Expand Down Expand Up @@ -70,6 +71,7 @@ export const SignInForm = ({
})
setAuthLoading(false)
}
if (isLoadingProviders) return <Spinner />
if (hasNoAuthProvider)
return (
<Text>
Expand Down
10 changes: 5 additions & 5 deletions apps/builder/components/share/ShareContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import { integrationsList } from './integrations/EmbedButton'

export const ShareContent = () => {
const { workspace } = useWorkspace()
const { typebot, updateOnBothTypebots } = useTypebot()
const { typebot, updateTypebot } = useTypebot()
const { showToast } = useToast()

const handlePublicIdChange = (publicId: string) => {
if (publicId === typebot?.publicId) return
if (publicId.length < 4)
return showToast({ description: 'ID must be longer than 4 characters' })
updateOnBothTypebots({ publicId })
updateTypebot({ publicId })
}

const publicId = typebot
Expand All @@ -46,8 +46,8 @@ export const ShareContent = () => {
handleCustomDomainChange(newDomain)
}

const handleCustomDomainChange = (customDomain: string | null) =>
updateOnBothTypebots({ customDomain })
const handleCustomDomainChange = (customDomain: string | undefined) =>
updateTypebot({ customDomain })

return (
<Flex h="full" w="full" justifyContent="center" align="flex-start">
Expand Down Expand Up @@ -76,7 +76,7 @@ export const ShareContent = () => {
icon={<TrashIcon />}
aria-label="Remove custom domain"
size="xs"
onClick={() => handleCustomDomainChange(null)}
onClick={() => handleCustomDomainChange(undefined)}
/>
</HStack>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const TypebotHeader = () => {
const router = useRouter()
const {
typebot,
updateOnBothTypebots,
updateTypebot,
save,
undo,
Expand All @@ -36,7 +35,7 @@ export const TypebotHeader = () => {
} = useTypebot()
const { setRightPanel, rightPanel, setStartPreviewAtBlock } = useEditor()

const handleNameSubmit = (name: string) => updateOnBothTypebots({ name })
const handleNameSubmit = (name: string) => updateTypebot({ name })

const handleChangeIcon = (icon: string) => updateTypebot({ icon })

Expand Down
21 changes: 1 addition & 20 deletions apps/builder/contexts/TypebotContext/TypebotContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type UpdateTypebotPayload = Partial<{
name: string
publishedTypebotId: string
icon: string
customDomain: string
}>

export type SetTypebot = (
Expand All @@ -77,11 +78,6 @@ const typebotContext = createContext<
webhook: Partial<Webhook>
) => Promise<void>
updateTypebot: (updates: UpdateTypebotPayload) => void
updateOnBothTypebots: (updates: {
publicId?: string
name?: string
customDomain?: string | null
}) => void
publishTypebot: () => void
restorePublishedTypebot: () => void
} & BlocksActions &
Expand Down Expand Up @@ -322,20 +318,6 @@ export const TypebotContext = ({
}
}

const updateOnBothTypebots = async (updates: {
publicId?: string
name?: string
customDomain?: string | null
}) => {
updateLocalTypebot(updates)
await saveTypebot()
if (!publishedTypebot) return
await savePublishedTypebot({
...publishedTypebot,
...updates,
})
}

const restorePublishedTypebot = () => {
if (!publishedTypebot || !localTypebot) return
setLocalTypebot(parsePublicTypebotToTypebot(publishedTypebot, localTypebot))
Expand Down Expand Up @@ -377,7 +359,6 @@ export const TypebotContext = ({
isPublished,
updateTypebot: updateLocalTypebot,
restorePublishedTypebot,
updateOnBothTypebots,
updateWebhook,
...blocksActions(setLocalTypebot as SetTypebot),
...stepsAction(setLocalTypebot as SetTypebot),
Expand Down
3 changes: 0 additions & 3 deletions apps/builder/playwright/services/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,12 @@ const parseTypebotToPublicTypebot = (
typebot: Typebot
): Omit<PublicTypebot, 'createdAt' | 'updatedAt'> => ({
id,
name: typebot.name,
blocks: typebot.blocks,
typebotId: typebot.id,
theme: typebot.theme,
settings: typebot.settings,
publicId: typebot.publicId,
variables: typebot.variables,
edges: typebot.edges,
customDomain: null,
})

const parseTestTypebot = (partialTypebot: Partial<Typebot>): Typebot => ({
Expand Down
9 changes: 3 additions & 6 deletions apps/builder/services/publicTypebot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ export const parseTypebotToPublicTypebot = (
typebotId: typebot.id,
blocks: typebot.blocks,
edges: typebot.edges,
name: typebot.name,
publicId: typebot.publicId,
settings: typebot.settings,
theme: typebot.theme,
variables: typebot.variables,
customDomain: typebot.customDomain,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
})
Expand All @@ -26,12 +23,12 @@ export const parsePublicTypebotToTypebot = (
id: typebot.typebotId,
blocks: typebot.blocks,
edges: typebot.edges,
name: typebot.name,
publicId: typebot.publicId,
name: existingTypebot.name,
publicId: existingTypebot.publicId,
settings: typebot.settings,
theme: typebot.theme,
variables: typebot.variables,
customDomain: typebot.customDomain,
customDomain: existingTypebot.customDomain,
createdAt: existingTypebot.createdAt,
updatedAt: existingTypebot.updatedAt,
publishedTypebotId: typebot.id,
Expand Down
4 changes: 2 additions & 2 deletions apps/viewer/layouts/TypebotPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createResult, updateResult } from '../services/result'
import { ErrorPage } from './ErrorPage'

export type TypebotPageProps = {
typebot?: PublicTypebot
typebot?: PublicTypebot & { typebot: { name: string } }
url: string
isIE: boolean
customHeadCode: string | null
Expand Down Expand Up @@ -97,7 +97,7 @@ export const TypebotPage = ({
<div style={{ height: '100vh' }}>
<SEO
url={url}
typebotName={typebot.name}
typebotName={typebot.typebot.name}
metadata={typebot.settings.metadata}
/>
{showTypebot && (
Expand Down
8 changes: 5 additions & 3 deletions apps/viewer/pages/[[...publicId]].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,18 @@ export const getServerSideProps: GetServerSideProps = async (

const getTypebotFromPublicId = async (publicId?: string) => {
if (!publicId) return null
const typebot = await prisma.publicTypebot.findUnique({
where: { publicId },
const typebot = await prisma.publicTypebot.findFirst({
where: { typebot: { publicId } },
include: { typebot: { select: { name: true } } },
})
if (isNotDefined(typebot)) return null
return omit(typebot as unknown as PublicTypebot, 'createdAt', 'updatedAt')
}

const getTypebotFromCustomDomain = async (customDomain: string) => {
const typebot = await prisma.publicTypebot.findFirst({
where: { customDomain },
where: { typebot: { customDomain } },
include: { typebot: { select: { name: true } } },
})
if (isNotDefined(typebot)) return null
return omit(typebot as unknown as PublicTypebot, 'createdAt', 'updatedAt')
Expand Down
3 changes: 0 additions & 3 deletions apps/viewer/playwright/services/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,12 @@ const parseTypebotToPublicTypebot = (
typebot: Typebot
): PublicTypebot => ({
id,
name: typebot.name,
blocks: typebot.blocks,
typebotId: typebot.id,
theme: typebot.theme,
settings: typebot.settings,
publicId: typebot.publicId,
variables: typebot.variables,
edges: typebot.edges,
customDomain: null,
createdAt: typebot.createdAt,
updatedAt: typebot.updatedAt,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- DropIndex
DROP INDEX "PublicTypebot_customDomain_key";

-- DropIndex
DROP INDEX "PublicTypebot_publicId_key";

-- AlterTable
ALTER TABLE "PublicTypebot" DROP COLUMN "customDomain",
DROP COLUMN "name",
DROP COLUMN "publicId";
3 changes: 0 additions & 3 deletions packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,11 @@ model PublicTypebot {
updatedAt DateTime @default(now()) @updatedAt
typebotId String @unique
typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
name String
blocks Json[]
variables Json[]
edges Json[]
theme Json
settings Json
publicId String? @unique
customDomain String? @unique
}

model Result {
Expand Down

0 comments on commit ad32ae0

Please sign in to comment.