feat: ✨ Add collaboration
This commit is contained in:
46
apps/builder/services/user/credentials.ts
Normal file
46
apps/builder/services/user/credentials.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { Credentials } from 'models'
|
||||
import useSWR from 'swr'
|
||||
import { sendRequest } from 'utils'
|
||||
import { fetcher } from '../utils'
|
||||
|
||||
export const useCredentials = ({
|
||||
userId,
|
||||
onError,
|
||||
}: {
|
||||
userId?: string
|
||||
onError: (error: Error) => void
|
||||
}) => {
|
||||
const { data, error, mutate } = useSWR<{ credentials: Credentials[] }, Error>(
|
||||
userId ? `/api/users/${userId}/credentials` : null,
|
||||
fetcher
|
||||
)
|
||||
if (error) onError(error)
|
||||
return {
|
||||
credentials: data?.credentials ?? [],
|
||||
isLoading: !error && !data,
|
||||
mutate,
|
||||
}
|
||||
}
|
||||
|
||||
export const createCredentials = async (
|
||||
userId: string,
|
||||
credentials: Omit<Credentials, 'ownerId' | 'id' | 'iv'>
|
||||
) =>
|
||||
sendRequest<{
|
||||
credentials: Credentials
|
||||
}>({
|
||||
url: `/api/users/${userId}/credentials`,
|
||||
method: 'POST',
|
||||
body: credentials,
|
||||
})
|
||||
|
||||
export const deleteCredentials = async (
|
||||
userId: string,
|
||||
credentialsId: string
|
||||
) =>
|
||||
sendRequest<{
|
||||
credentials: Credentials
|
||||
}>({
|
||||
url: `/api/users/${userId}/credentials/${credentialsId}`,
|
||||
method: 'DELETE',
|
||||
})
|
47
apps/builder/services/user/customDomains.ts
Normal file
47
apps/builder/services/user/customDomains.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { CustomDomain } from 'db'
|
||||
import { Credentials } from 'models'
|
||||
import useSWR from 'swr'
|
||||
import { sendRequest } from 'utils'
|
||||
import { fetcher } from '../utils'
|
||||
|
||||
export const useCustomDomains = ({
|
||||
userId,
|
||||
onError,
|
||||
}: {
|
||||
userId?: string
|
||||
onError: (error: Error) => void
|
||||
}) => {
|
||||
const { data, error, mutate } = useSWR<
|
||||
{ customDomains: CustomDomain[] },
|
||||
Error
|
||||
>(userId ? `/api/users/${userId}/customDomains` : null, fetcher)
|
||||
if (error) onError(error)
|
||||
return {
|
||||
customDomains: data?.customDomains,
|
||||
isLoading: !error && !data,
|
||||
mutate,
|
||||
}
|
||||
}
|
||||
|
||||
export const createCustomDomain = async (
|
||||
userId: string,
|
||||
customDomain: Omit<CustomDomain, 'ownerId'>
|
||||
) =>
|
||||
sendRequest<{
|
||||
credentials: Credentials
|
||||
}>({
|
||||
url: `/api/users/${userId}/customDomains`,
|
||||
method: 'POST',
|
||||
body: customDomain,
|
||||
})
|
||||
|
||||
export const deleteCustomDomain = async (
|
||||
userId: string,
|
||||
customDomain: string
|
||||
) =>
|
||||
sendRequest<{
|
||||
credentials: Credentials
|
||||
}>({
|
||||
url: `/api/users/${userId}/customDomains/${customDomain}`,
|
||||
method: 'DELETE',
|
||||
})
|
3
apps/builder/services/user/index.ts
Normal file
3
apps/builder/services/user/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './user'
|
||||
export * from './customDomains'
|
||||
export * from './credentials'
|
42
apps/builder/services/user/sharedTypebots.ts
Normal file
42
apps/builder/services/user/sharedTypebots.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { Typebot } from 'models'
|
||||
import { fetcher } from 'services/utils'
|
||||
import useSWR from 'swr'
|
||||
import { isNotDefined } from 'utils'
|
||||
|
||||
export const useSharedTypebotsCount = ({
|
||||
userId,
|
||||
onError,
|
||||
}: {
|
||||
userId?: string
|
||||
onError: (error: Error) => void
|
||||
}) => {
|
||||
const { data, error, mutate } = useSWR<{ count: number }, Error>(
|
||||
userId ? `/api/users/${userId}/sharedTypebots?count=true` : null,
|
||||
fetcher
|
||||
)
|
||||
if (error) onError(error)
|
||||
return {
|
||||
totalSharedTypebots: data?.count ?? 0,
|
||||
isLoading: !error && isNotDefined(data?.count),
|
||||
mutate,
|
||||
}
|
||||
}
|
||||
|
||||
export const useSharedTypebots = ({
|
||||
userId,
|
||||
onError,
|
||||
}: {
|
||||
userId?: string
|
||||
onError: (error: Error) => void
|
||||
}) => {
|
||||
const { data, error, mutate } = useSWR<
|
||||
{ sharedTypebots: Pick<Typebot, 'name' | 'id' | 'publishedTypebotId'>[] },
|
||||
Error
|
||||
>(userId ? `/api/users/${userId}/sharedTypebots` : null, fetcher)
|
||||
if (error) onError(error)
|
||||
return {
|
||||
sharedTypebots: data?.sharedTypebots,
|
||||
isLoading: !error && isNotDefined(data),
|
||||
mutate,
|
||||
}
|
||||
}
|
12
apps/builder/services/user/user.ts
Normal file
12
apps/builder/services/user/user.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { Plan, User } from 'db'
|
||||
import { isNotDefined, sendRequest } from 'utils'
|
||||
|
||||
export const updateUser = async (id: string, user: User) =>
|
||||
sendRequest({
|
||||
url: `/api/users/${id}`,
|
||||
method: 'PUT',
|
||||
body: user,
|
||||
})
|
||||
|
||||
export const isFreePlan = (user?: User) =>
|
||||
isNotDefined(user) || user?.plan === Plan.FREE
|
Reference in New Issue
Block a user