2023-03-25 18:09:46 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
|
2023-04-12 12:37:50 +00:00
|
|
|
import useSWR, { mutate } from "swr";
|
2023-03-25 18:09:46 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
// services
|
|
|
|
import pagesService from "services/pages.service";
|
2023-04-12 12:37:50 +00:00
|
|
|
import projectService from "services/project.service";
|
2023-03-25 18:09:46 +00:00
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
// components
|
|
|
|
import {
|
|
|
|
CreateUpdatePageModal,
|
|
|
|
DeletePageModal,
|
|
|
|
SinglePageDetailedItem,
|
|
|
|
SinglePageListItem,
|
|
|
|
} from "components/pages";
|
|
|
|
// ui
|
|
|
|
import { EmptyState, Loader } from "components/ui";
|
|
|
|
// images
|
|
|
|
import emptyPage from "public/empty-state/empty-page.svg";
|
|
|
|
// types
|
|
|
|
import { IPage, TPageViewProps } from "types";
|
2023-03-27 17:49:05 +00:00
|
|
|
import {
|
|
|
|
ALL_PAGES_LIST,
|
|
|
|
FAVORITE_PAGES_LIST,
|
|
|
|
MY_PAGES_LIST,
|
2023-04-12 12:37:50 +00:00
|
|
|
PROJECT_MEMBERS,
|
2023-03-27 17:49:05 +00:00
|
|
|
RECENT_PAGES_LIST,
|
|
|
|
} from "constants/fetch-keys";
|
2023-03-25 18:09:46 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
pages: IPage[] | undefined;
|
|
|
|
viewType: TPageViewProps;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const PagesView: React.FC<Props> = ({ pages, viewType }) => {
|
|
|
|
const [createUpdatePageModal, setCreateUpdatePageModal] = useState(false);
|
|
|
|
const [selectedPageToUpdate, setSelectedPageToUpdate] = useState<IPage | null>(null);
|
|
|
|
|
|
|
|
const [deletePageModal, setDeletePageModal] = useState(false);
|
|
|
|
const [selectedPageToDelete, setSelectedPageToDelete] = useState<IPage | null>(null);
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
2023-04-12 12:37:50 +00:00
|
|
|
const { data: people } = useSWR(
|
2023-04-20 20:45:21 +00:00
|
|
|
workspaceSlug && projectId ? PROJECT_MEMBERS(projectId.toString()) : null,
|
2023-04-12 12:37:50 +00:00
|
|
|
workspaceSlug && projectId
|
2023-04-20 20:45:21 +00:00
|
|
|
? () => projectService.projectMembers(workspaceSlug.toString(), projectId.toString())
|
2023-04-12 12:37:50 +00:00
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
2023-03-25 18:09:46 +00:00
|
|
|
const handleEditPage = (page: IPage) => {
|
|
|
|
setSelectedPageToUpdate(page);
|
|
|
|
setCreateUpdatePageModal(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDeletePage = (page: IPage) => {
|
|
|
|
setSelectedPageToDelete(page);
|
|
|
|
setDeletePageModal(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleAddToFavorites = (page: IPage) => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
2023-03-27 17:49:05 +00:00
|
|
|
mutate<IPage[]>(
|
2023-04-20 20:45:21 +00:00
|
|
|
ALL_PAGES_LIST(projectId.toString()),
|
2023-03-27 17:49:05 +00:00
|
|
|
(prevData) =>
|
|
|
|
(prevData ?? []).map((p) => {
|
|
|
|
if (p.id === page.id) p.is_favorite = true;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
mutate<IPage[]>(
|
2023-04-20 20:45:21 +00:00
|
|
|
MY_PAGES_LIST(projectId.toString()),
|
2023-03-27 17:49:05 +00:00
|
|
|
(prevData) =>
|
|
|
|
(prevData ?? []).map((p) => {
|
|
|
|
if (p.id === page.id) p.is_favorite = true;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
mutate<IPage[]>(
|
2023-04-20 20:45:21 +00:00
|
|
|
FAVORITE_PAGES_LIST(projectId.toString()),
|
|
|
|
(prevData) => [page, ...(prevData ?? [])],
|
2023-03-27 17:49:05 +00:00
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2023-03-25 18:09:46 +00:00
|
|
|
pagesService
|
2023-04-20 20:45:21 +00:00
|
|
|
.addPageToFavorites(workspaceSlug.toString(), projectId.toString(), {
|
2023-03-25 18:09:46 +00:00
|
|
|
page: page.id,
|
|
|
|
})
|
|
|
|
.then(() => {
|
2023-04-20 20:45:21 +00:00
|
|
|
mutate(RECENT_PAGES_LIST(projectId.toString()));
|
2023-03-25 18:09:46 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Success!",
|
|
|
|
message: "Successfully added the page to favorites.",
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Couldn't add the page to favorites. Please try again.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleRemoveFromFavorites = (page: IPage) => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
2023-03-27 17:49:05 +00:00
|
|
|
mutate<IPage[]>(
|
2023-04-20 20:45:21 +00:00
|
|
|
ALL_PAGES_LIST(projectId.toString()),
|
2023-03-27 17:49:05 +00:00
|
|
|
(prevData) =>
|
|
|
|
(prevData ?? []).map((p) => {
|
|
|
|
if (p.id === page.id) p.is_favorite = false;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
mutate<IPage[]>(
|
2023-04-20 20:45:21 +00:00
|
|
|
MY_PAGES_LIST(projectId.toString()),
|
2023-03-27 17:49:05 +00:00
|
|
|
(prevData) =>
|
|
|
|
(prevData ?? []).map((p) => {
|
|
|
|
if (p.id === page.id) p.is_favorite = false;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
mutate<IPage[]>(
|
2023-04-20 20:45:21 +00:00
|
|
|
FAVORITE_PAGES_LIST(projectId.toString()),
|
2023-03-27 17:49:05 +00:00
|
|
|
(prevData) => (prevData ?? []).filter((p) => p.id !== page.id),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2023-03-25 18:09:46 +00:00
|
|
|
pagesService
|
2023-04-20 20:45:21 +00:00
|
|
|
.removePageFromFavorites(workspaceSlug.toString(), projectId.toString(), page.id)
|
2023-03-25 18:09:46 +00:00
|
|
|
.then(() => {
|
2023-04-20 20:45:21 +00:00
|
|
|
mutate(RECENT_PAGES_LIST(projectId.toString()));
|
2023-03-25 18:09:46 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Success!",
|
|
|
|
message: "Successfully removed the page from favorites.",
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Couldn't remove the page from favorites. Please try again.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-04-03 18:00:29 +00:00
|
|
|
const partialUpdatePage = (page: IPage, formData: Partial<IPage>) => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
|
|
|
mutate<IPage[]>(
|
2023-04-20 20:45:21 +00:00
|
|
|
ALL_PAGES_LIST(projectId.toString()),
|
2023-04-20 06:39:55 +00:00
|
|
|
(prevData) => (prevData ?? []).map((p) => ({ ...p, ...(p.id === page.id ? formData : {}) })),
|
2023-04-03 18:00:29 +00:00
|
|
|
false
|
|
|
|
);
|
|
|
|
mutate<IPage[]>(
|
2023-04-20 20:45:21 +00:00
|
|
|
MY_PAGES_LIST(projectId.toString()),
|
2023-04-20 06:39:55 +00:00
|
|
|
(prevData) => (prevData ?? []).map((p) => ({ ...p, ...(p.id === page.id ? formData : {}) })),
|
2023-04-03 18:00:29 +00:00
|
|
|
false
|
|
|
|
);
|
|
|
|
mutate<IPage[]>(
|
2023-04-20 20:45:21 +00:00
|
|
|
FAVORITE_PAGES_LIST(projectId.toString()),
|
2023-04-20 06:39:55 +00:00
|
|
|
(prevData) => (prevData ?? []).map((p) => ({ ...p, ...(p.id === page.id ? formData : {}) })),
|
2023-04-03 18:00:29 +00:00
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
pagesService
|
2023-04-20 20:45:21 +00:00
|
|
|
.patchPage(workspaceSlug.toString(), projectId.toString(), page.id, formData)
|
2023-04-03 18:00:29 +00:00
|
|
|
.then(() => {
|
2023-04-20 20:45:21 +00:00
|
|
|
mutate(RECENT_PAGES_LIST(projectId.toString()));
|
2023-04-03 18:00:29 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-25 18:09:46 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<CreateUpdatePageModal
|
|
|
|
isOpen={createUpdatePageModal}
|
|
|
|
handleClose={() => setCreateUpdatePageModal(false)}
|
|
|
|
data={selectedPageToUpdate}
|
|
|
|
/>
|
|
|
|
<DeletePageModal
|
|
|
|
isOpen={deletePageModal}
|
|
|
|
setIsOpen={setDeletePageModal}
|
|
|
|
data={selectedPageToDelete}
|
|
|
|
/>
|
|
|
|
{pages ? (
|
|
|
|
pages.length > 0 ? (
|
|
|
|
viewType === "list" ? (
|
2023-04-20 20:45:21 +00:00
|
|
|
<ul role="list" className="divide-y divide-brand-base">
|
2023-03-25 18:09:46 +00:00
|
|
|
{pages.map((page) => (
|
|
|
|
<SinglePageListItem
|
|
|
|
key={page.id}
|
|
|
|
page={page}
|
2023-04-12 12:37:50 +00:00
|
|
|
people={people}
|
2023-03-25 18:09:46 +00:00
|
|
|
handleEditPage={() => handleEditPage(page)}
|
|
|
|
handleDeletePage={() => handleDeletePage(page)}
|
|
|
|
handleAddToFavorites={() => handleAddToFavorites(page)}
|
|
|
|
handleRemoveFromFavorites={() => handleRemoveFromFavorites(page)}
|
2023-04-03 18:00:29 +00:00
|
|
|
partialUpdatePage={partialUpdatePage}
|
2023-03-25 18:09:46 +00:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
) : viewType === "detailed" ? (
|
2023-04-20 20:45:21 +00:00
|
|
|
<div className="divide-y divide-brand-base rounded-[10px] border border-brand-base bg-brand-base">
|
2023-03-25 18:09:46 +00:00
|
|
|
{pages.map((page) => (
|
|
|
|
<SinglePageDetailedItem
|
|
|
|
key={page.id}
|
|
|
|
page={page}
|
2023-04-12 12:37:50 +00:00
|
|
|
people={people}
|
2023-03-25 18:09:46 +00:00
|
|
|
handleEditPage={() => handleEditPage(page)}
|
|
|
|
handleDeletePage={() => handleDeletePage(page)}
|
|
|
|
handleAddToFavorites={() => handleAddToFavorites(page)}
|
|
|
|
handleRemoveFromFavorites={() => handleRemoveFromFavorites(page)}
|
2023-04-03 18:00:29 +00:00
|
|
|
partialUpdatePage={partialUpdatePage}
|
2023-03-25 18:09:46 +00:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
) : (
|
2023-04-20 20:45:21 +00:00
|
|
|
<div className="rounded-[10px] border border-brand-base">
|
2023-03-25 18:09:46 +00:00
|
|
|
{pages.map((page) => (
|
|
|
|
<SinglePageDetailedItem
|
|
|
|
key={page.id}
|
|
|
|
page={page}
|
2023-04-12 12:37:50 +00:00
|
|
|
people={people}
|
2023-03-25 18:09:46 +00:00
|
|
|
handleEditPage={() => handleEditPage(page)}
|
|
|
|
handleDeletePage={() => handleDeletePage(page)}
|
|
|
|
handleAddToFavorites={() => handleAddToFavorites(page)}
|
|
|
|
handleRemoveFromFavorites={() => handleRemoveFromFavorites(page)}
|
2023-04-03 18:00:29 +00:00
|
|
|
partialUpdatePage={partialUpdatePage}
|
2023-03-25 18:09:46 +00:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
) : (
|
|
|
|
<EmptyState
|
|
|
|
type="page"
|
|
|
|
title="Create New Page"
|
2023-03-30 21:21:39 +00:00
|
|
|
description="Create and document issues effortlessly in one place with Plane Notes, AI-powered for ease."
|
2023-03-25 18:09:46 +00:00
|
|
|
imgURL={emptyPage}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
) : viewType === "list" ? (
|
|
|
|
<Loader className="space-y-4">
|
|
|
|
<Loader.Item height="40px" />
|
|
|
|
<Loader.Item height="40px" />
|
|
|
|
<Loader.Item height="40px" />
|
|
|
|
</Loader>
|
2023-04-20 20:45:21 +00:00
|
|
|
) : viewType === "detailed" ? (
|
|
|
|
<Loader className="space-y-4">
|
|
|
|
<Loader.Item height="150px" />
|
|
|
|
<Loader.Item height="150px" />
|
|
|
|
</Loader>
|
2023-03-25 18:09:46 +00:00
|
|
|
) : (
|
|
|
|
<Loader className="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3">
|
|
|
|
<Loader.Item height="150px" />
|
|
|
|
<Loader.Item height="150px" />
|
|
|
|
<Loader.Item height="150px" />
|
|
|
|
</Loader>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|