2022-05-13 15:22:44 -07:00
|
|
|
import { WorkspaceWithMembers } from 'contexts/WorkspaceContext'
|
|
|
|
import { Plan, Workspace } from 'db'
|
|
|
|
import useSWR from 'swr'
|
|
|
|
import { isNotDefined, sendRequest } from 'utils'
|
|
|
|
import { fetcher } from '../utils'
|
|
|
|
|
|
|
|
export const useWorkspaces = ({ userId }: { userId?: string }) => {
|
|
|
|
const { data, error, mutate } = useSWR<
|
|
|
|
{
|
|
|
|
workspaces: WorkspaceWithMembers[]
|
|
|
|
},
|
|
|
|
Error
|
|
|
|
>(userId ? `/api/workspaces` : null, fetcher)
|
|
|
|
return {
|
|
|
|
workspaces: data?.workspaces,
|
|
|
|
isLoading: !error && !data,
|
|
|
|
mutate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const createNewWorkspace = async (
|
|
|
|
body: Omit<Workspace, 'id' | 'icon' | 'createdAt' | 'stripeId'>
|
|
|
|
) =>
|
|
|
|
sendRequest<{
|
|
|
|
workspace: Workspace
|
|
|
|
}>({
|
|
|
|
url: `/api/workspaces`,
|
|
|
|
method: 'POST',
|
|
|
|
body,
|
|
|
|
})
|
|
|
|
|
|
|
|
export const updateWorkspace = async (updates: Partial<Workspace>) =>
|
|
|
|
sendRequest<{
|
|
|
|
workspace: Workspace
|
|
|
|
}>({
|
|
|
|
url: `/api/workspaces/${updates.id}`,
|
|
|
|
method: 'PATCH',
|
|
|
|
body: updates,
|
|
|
|
})
|
|
|
|
|
2022-05-17 08:59:02 -07:00
|
|
|
export const deleteWorkspace = (workspaceId: string) =>
|
|
|
|
sendRequest<{
|
|
|
|
workspace: Workspace
|
|
|
|
}>({
|
|
|
|
url: `/api/workspaces/${workspaceId}`,
|
|
|
|
method: 'DELETE',
|
|
|
|
})
|
|
|
|
|
2022-05-13 15:22:44 -07:00
|
|
|
export const planToReadable = (plan?: Plan) => {
|
|
|
|
if (!plan) return
|
|
|
|
switch (plan) {
|
|
|
|
case Plan.FREE:
|
|
|
|
return 'Free'
|
|
|
|
case Plan.LIFETIME:
|
|
|
|
return 'Lifetime'
|
|
|
|
case Plan.OFFERED:
|
|
|
|
return 'Offered'
|
|
|
|
case Plan.PRO:
|
|
|
|
return 'Pro'
|
|
|
|
case Plan.TEAM:
|
|
|
|
return 'Team'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isFreePlan = (workspace?: Pick<Workspace, 'plan'>) =>
|
|
|
|
isNotDefined(workspace) || workspace?.plan === Plan.FREE
|