plane/web/components/view/views/create-edit.tsx

89 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-02-05 14:39:17 +00:00
import { FC, ReactNode, useState } from "react";
import { observer } from "mobx-react-lite";
2024-02-02 08:22:38 +00:00
import { Plus } from "lucide-react";
// ui
import { Button } from "@plane/ui";
// components
import { ViewCreateEditForm } from "./create-edit-form";
2024-02-05 14:39:17 +00:00
// constants
import { viewLocalPayload } from "constants/view";
2024-02-02 08:22:38 +00:00
// types
import { TViewOperations } from "../types";
2024-02-05 14:39:17 +00:00
import { TView, TViewFilters, TViewTypes } from "@plane/types";
2024-02-02 08:22:38 +00:00
type TViewCreateEdit = {
workspaceSlug: string;
projectId: string | undefined;
viewId: string | undefined;
viewType: TViewTypes;
viewOperations: TViewOperations;
2024-02-05 14:39:17 +00:00
children?: ReactNode;
2024-02-02 08:22:38 +00:00
};
2024-02-05 14:39:17 +00:00
export const ViewCreateEdit: FC<TViewCreateEdit> = observer((props) => {
const { workspaceSlug, projectId, viewId, viewType, viewOperations, children } = props;
2024-02-02 08:22:38 +00:00
// states
2024-02-05 14:39:17 +00:00
const [currentViewId, setCurrentViewId] = useState<string>();
const [currentFilters, setCurrentFilters] = useState<Partial<TViewFilters>>({});
2024-02-02 08:22:38 +00:00
const [modalToggle, setModalToggle] = useState(false);
2024-02-05 14:39:17 +00:00
const handleModalOpen = () => {
if (viewId === undefined) {
const viewPayload = viewLocalPayload;
setCurrentViewId(viewPayload.id);
viewOperations?.localViewCreate(viewPayload as TView);
} else {
setCurrentViewId(viewId);
}
setModalToggle(true);
};
const handleModalClose = () => {
if (viewId === undefined) {
if (currentViewId) viewOperations?.clearLocalView(currentViewId);
} else {
}
setModalToggle(false);
setCurrentViewId(undefined);
setCurrentFilters({});
};
2024-02-02 08:22:38 +00:00
2024-02-05 14:39:17 +00:00
const onSubmit = async (viewData: Partial<TView>) => {
if (!viewData?.name) return;
try {
await viewOperations.create(viewData);
handleModalClose();
} catch (error) {
console.log(error);
}
2024-02-02 08:22:38 +00:00
};
return (
<>
2024-02-05 14:39:17 +00:00
{currentViewId && (
<ViewCreateEditForm
workspaceSlug={workspaceSlug}
projectId={projectId}
viewId={currentViewId}
viewType={viewType}
viewOperations={viewOperations}
onSubmit={onSubmit}
modalToggle={modalToggle}
handleModalClose={handleModalClose}
/>
)}
<div className="inline-block" onClick={handleModalOpen}>
{children ? (
children
) : (
<Button size="sm" className="flex justify-center items-center">
<Plus size={12} />
<span>New View</span>
</Button>
)}
2024-02-02 08:22:38 +00:00
</div>
</>
);
2024-02-05 14:39:17 +00:00
});