2022-03-14 16:14:42 +01:00
|
|
|
import { DashboardFolder } from 'db'
|
2021-12-06 15:48:50 +01:00
|
|
|
import useSWR from 'swr'
|
2022-01-18 18:25:18 +01:00
|
|
|
import { fetcher } from './utils'
|
2021-12-30 10:24:16 +01:00
|
|
|
import { stringify } from 'qs'
|
2022-06-22 07:36:11 +02:00
|
|
|
import { env, sendRequest } from 'utils'
|
2021-12-06 15:48:50 +01:00
|
|
|
|
|
|
|
export const useFolders = ({
|
|
|
|
parentId,
|
2022-05-13 15:22:44 -07:00
|
|
|
workspaceId,
|
2021-12-06 15:48:50 +01:00
|
|
|
onError,
|
|
|
|
}: {
|
2022-05-13 15:22:44 -07:00
|
|
|
workspaceId?: string
|
2021-12-06 15:48:50 +01:00
|
|
|
parentId?: string
|
|
|
|
onError: (error: Error) => void
|
|
|
|
}) => {
|
2022-05-13 15:22:44 -07:00
|
|
|
const params = stringify({ parentId, workspaceId })
|
2021-12-06 15:48:50 +01:00
|
|
|
const { data, error, mutate } = useSWR<{ folders: DashboardFolder[] }, Error>(
|
2022-05-13 15:22:44 -07:00
|
|
|
workspaceId ? `/api/folders?${params}` : null,
|
2022-01-28 09:42:31 +01:00
|
|
|
fetcher,
|
2022-05-25 08:13:35 -07:00
|
|
|
{
|
2022-08-08 08:21:36 +02:00
|
|
|
dedupingInterval: env('E2E_TEST') === 'true' ? 0 : undefined,
|
2022-05-25 08:13:35 -07:00
|
|
|
}
|
2021-12-06 15:48:50 +01:00
|
|
|
)
|
|
|
|
if (error) onError(error)
|
|
|
|
return {
|
|
|
|
folders: data?.folders,
|
|
|
|
isLoading: !error && !data,
|
|
|
|
mutate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useFolderContent = ({
|
|
|
|
folderId,
|
|
|
|
onError,
|
|
|
|
}: {
|
|
|
|
folderId?: string
|
|
|
|
onError: (error: Error) => void
|
|
|
|
}) => {
|
|
|
|
const { data, error, mutate } = useSWR<{ folder: DashboardFolder }, Error>(
|
|
|
|
`/api/folders/${folderId}`,
|
|
|
|
fetcher
|
|
|
|
)
|
|
|
|
if (error) onError(error)
|
|
|
|
return {
|
|
|
|
folder: data?.folder,
|
|
|
|
isLoading: !error && !data,
|
|
|
|
mutate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const createFolder = async (
|
2022-05-13 15:22:44 -07:00
|
|
|
workspaceId: string,
|
2021-12-06 15:48:50 +01:00
|
|
|
folder: Pick<DashboardFolder, 'parentFolderId'>
|
|
|
|
) =>
|
|
|
|
sendRequest<DashboardFolder>({
|
|
|
|
url: `/api/folders`,
|
|
|
|
method: 'POST',
|
2022-05-13 15:22:44 -07:00
|
|
|
body: { ...folder, workspaceId },
|
2021-12-06 15:48:50 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
export const deleteFolder = async (id: string) =>
|
|
|
|
sendRequest({
|
|
|
|
url: `/api/folders/${id}`,
|
|
|
|
method: 'DELETE',
|
|
|
|
})
|
|
|
|
|
|
|
|
export const updateFolder = async (
|
|
|
|
id: string,
|
|
|
|
folder: Partial<DashboardFolder>
|
|
|
|
) =>
|
|
|
|
sendRequest({
|
|
|
|
url: `/api/folders/${id}`,
|
|
|
|
method: 'PATCH',
|
|
|
|
body: folder,
|
|
|
|
})
|