forked from github/plane
5d67029b5a
* style: page details * style: page blocks design * chore: pages list end points * feat: add blocks, push blocks to issues * feat: page labels, color options * feat: added labels to pages * fix: update page mutation
173 lines
5.3 KiB
TypeScript
173 lines
5.3 KiB
TypeScript
import { useState } from "react";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
// services
|
|
import pagesService from "services/pages.service";
|
|
// 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";
|
|
|
|
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();
|
|
|
|
const handleEditPage = (page: IPage) => {
|
|
setSelectedPageToUpdate(page);
|
|
setCreateUpdatePageModal(true);
|
|
};
|
|
|
|
const handleDeletePage = (page: IPage) => {
|
|
setSelectedPageToDelete(page);
|
|
setDeletePageModal(true);
|
|
};
|
|
|
|
const handleAddToFavorites = (page: IPage) => {
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
pagesService
|
|
.addPageToFavorites(workspaceSlug as string, projectId as string, {
|
|
page: page.id,
|
|
})
|
|
.then(() => {
|
|
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;
|
|
|
|
pagesService
|
|
.removePageFromFavorites(workspaceSlug as string, projectId as string, page.id)
|
|
.then(() => {
|
|
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.",
|
|
});
|
|
});
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<CreateUpdatePageModal
|
|
isOpen={createUpdatePageModal}
|
|
handleClose={() => setCreateUpdatePageModal(false)}
|
|
data={selectedPageToUpdate}
|
|
/>
|
|
<DeletePageModal
|
|
isOpen={deletePageModal}
|
|
setIsOpen={setDeletePageModal}
|
|
data={selectedPageToDelete}
|
|
/>
|
|
{pages ? (
|
|
pages.length > 0 ? (
|
|
viewType === "list" ? (
|
|
<ul role="list" className="divide-y">
|
|
{pages.map((page) => (
|
|
<SinglePageListItem
|
|
key={page.id}
|
|
page={page}
|
|
handleEditPage={() => handleEditPage(page)}
|
|
handleDeletePage={() => handleDeletePage(page)}
|
|
handleAddToFavorites={() => handleAddToFavorites(page)}
|
|
handleRemoveFromFavorites={() => handleRemoveFromFavorites(page)}
|
|
/>
|
|
))}
|
|
</ul>
|
|
) : viewType === "detailed" ? (
|
|
<div className="rounded-[10px] border border-gray-200 bg-white">
|
|
{pages.map((page) => (
|
|
<SinglePageDetailedItem
|
|
key={page.id}
|
|
page={page}
|
|
handleEditPage={() => handleEditPage(page)}
|
|
handleDeletePage={() => handleDeletePage(page)}
|
|
handleAddToFavorites={() => handleAddToFavorites(page)}
|
|
handleRemoveFromFavorites={() => handleRemoveFromFavorites(page)}
|
|
/>
|
|
))}
|
|
</div>
|
|
) : (
|
|
<div className="rounded-[10px] border border-gray-200 bg-white">
|
|
{pages.map((page) => (
|
|
<SinglePageDetailedItem
|
|
key={page.id}
|
|
page={page}
|
|
handleEditPage={() => handleEditPage(page)}
|
|
handleDeletePage={() => handleDeletePage(page)}
|
|
handleAddToFavorites={() => handleAddToFavorites(page)}
|
|
handleRemoveFromFavorites={() => handleRemoveFromFavorites(page)}
|
|
/>
|
|
))}
|
|
</div>
|
|
)
|
|
) : (
|
|
<EmptyState
|
|
type="page"
|
|
title="Create New Page"
|
|
description="Sprint more effectively with Cycles by confining your project
|
|
to a fixed amount of time. Create new cycle now."
|
|
imgURL={emptyPage}
|
|
/>
|
|
)
|
|
) : viewType === "list" ? (
|
|
<Loader className="space-y-4">
|
|
<Loader.Item height="40px" />
|
|
<Loader.Item height="40px" />
|
|
<Loader.Item height="40px" />
|
|
</Loader>
|
|
) : (
|
|
<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>
|
|
)}
|
|
</>
|
|
);
|
|
};
|