feat: sidebar project ordering functionality added (#1725)

This commit is contained in:
Anmol Singh Bhatia 2023-07-31 17:10:23 +05:30 committed by GitHub
parent c9498fa54d
commit 98d9763f8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 154 additions and 34 deletions

View File

@ -1,7 +1,10 @@
import React, { useState, FC } from "react"; import React, { useState, FC } from "react";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { mutate } from "swr";
// react-beautiful-dnd
import { DragDropContext, Draggable, DropResult, Droppable } from "react-beautiful-dnd";
// hooks // hooks
import useToast from "hooks/use-toast"; import useToast from "hooks/use-toast";
import useTheme from "hooks/use-theme"; import useTheme from "hooks/use-theme";
@ -9,12 +12,17 @@ import useUserAuth from "hooks/use-user-auth";
import useProjects from "hooks/use-projects"; import useProjects from "hooks/use-projects";
// components // components
import { DeleteProjectModal, SingleSidebarProject } from "components/project"; import { DeleteProjectModal, SingleSidebarProject } from "components/project";
// services
import projectService from "services/project.service";
// icons // icons
import { PlusIcon } from "@heroicons/react/24/outline"; import { PlusIcon } from "@heroicons/react/24/outline";
// helpers // helpers
import { copyTextToClipboard } from "helpers/string.helper"; import { copyTextToClipboard } from "helpers/string.helper";
import { orderArrayBy } from "helpers/array.helper";
// types // types
import { IProject } from "types"; import { IProject } from "types";
// fetch-keys
import { PROJECTS_LIST } from "constants/fetch-keys";
export const ProjectSidebarList: FC = () => { export const ProjectSidebarList: FC = () => {
const [deleteProjectModal, setDeleteProjectModal] = useState(false); const [deleteProjectModal, setDeleteProjectModal] = useState(false);
@ -32,6 +40,14 @@ export const ProjectSidebarList: FC = () => {
const { projects: allProjects } = useProjects(); const { projects: allProjects } = useProjects();
const favoriteProjects = allProjects?.filter((p) => p.is_favorite); const favoriteProjects = allProjects?.filter((p) => p.is_favorite);
const orderedAllProjects = allProjects
? orderArrayBy(allProjects, "sort_order", "ascending")
: [];
const orderedFavProjects = favoriteProjects
? orderArrayBy(favoriteProjects, "sort_order", "ascending")
: [];
const handleDeleteProject = (project: IProject) => { const handleDeleteProject = (project: IProject) => {
setProjectToDelete(project); setProjectToDelete(project);
setDeleteProjectModal(true); setDeleteProjectModal(true);
@ -49,6 +65,54 @@ export const ProjectSidebarList: FC = () => {
}); });
}; };
const onDragEnd = async (result: DropResult) => {
const { source, destination, draggableId } = result;
if (!destination || !workspaceSlug) return;
if (source.index === destination.index) return;
const projectList =
destination.droppableId === "all-projects" ? orderedAllProjects : orderedFavProjects;
let updatedSortOrder = projectList[source.index].sort_order;
if (destination.index === 0) {
updatedSortOrder = projectList[0].sort_order - 1000;
} else if (destination.index === projectList.length - 1) {
updatedSortOrder = projectList[projectList.length - 1].sort_order + 1000;
} else {
const destinationSortingOrder = projectList[destination.index].sort_order;
const relativeDestinationSortingOrder =
source.index < destination.index
? projectList[destination.index + 1].sort_order
: projectList[destination.index - 1].sort_order;
updatedSortOrder = Math.round(
(destinationSortingOrder + relativeDestinationSortingOrder) / 2
);
}
mutate<IProject[]>(
PROJECTS_LIST(workspaceSlug as string, { is_favorite: "all" }),
(prevData) => {
if (!prevData) return prevData;
return prevData.map((p) =>
p.id === draggableId ? { ...p, sort_order: updatedSortOrder } : p
);
},
false
);
await projectService
.setProjectView(workspaceSlug as string, draggableId, { sort_order: updatedSortOrder })
.catch(() => {
setToastAlert({
type: "error",
title: "Error!",
message: "Something went wrong. Please try again.",
});
});
};
return ( return (
<> <>
<DeleteProjectModal <DeleteProjectModal
@ -58,39 +122,75 @@ export const ProjectSidebarList: FC = () => {
user={user} user={user}
/> />
<div className="h-full overflow-y-auto px-4"> <div className="h-full overflow-y-auto px-4">
{favoriteProjects && favoriteProjects.length > 0 && ( <DragDropContext onDragEnd={onDragEnd}>
<div className="flex flex-col space-y-2 mt-5"> <Droppable droppableId="favorite-projects">
{!sidebarCollapse && ( {(provided) => (
<h5 className="text-sm font-medium text-custom-sidebar-text-200">Favorites</h5> <div ref={provided.innerRef} {...provided.droppableProps}>
{orderedFavProjects && orderedFavProjects.length > 0 && (
<div className="flex flex-col space-y-2 mt-5">
{!sidebarCollapse && (
<h5 className="text-sm font-medium text-custom-sidebar-text-200">
Favorites
</h5>
)}
{orderedFavProjects.map((project, index) => (
<Draggable key={project.id} draggableId={project.id} index={index}>
{(provided, snapshot) => (
<div ref={provided.innerRef} {...provided.draggableProps}>
<SingleSidebarProject
key={project.id}
project={project}
sidebarCollapse={sidebarCollapse}
provided={provided}
snapshot={snapshot}
handleDeleteProject={() => handleDeleteProject(project)}
handleCopyText={() => handleCopyText(project.id)}
shortContextMenu
/>
</div>
)}
</Draggable>
))}
{provided.placeholder}
</div>
)}
</div>
)} )}
{favoriteProjects.map((project) => ( </Droppable>
<SingleSidebarProject </DragDropContext>
key={project.id} <DragDropContext onDragEnd={onDragEnd}>
project={project} <Droppable droppableId="all-projects">
sidebarCollapse={sidebarCollapse} {(provided) => (
handleDeleteProject={() => handleDeleteProject(project)} <div ref={provided.innerRef} {...provided.droppableProps}>
handleCopyText={() => handleCopyText(project.id)} {orderedAllProjects && orderedAllProjects.length > 0 && (
shortContextMenu <div className="flex flex-col space-y-2 mt-5">
/> {!sidebarCollapse && (
))} <h5 className="text-sm font-medium text-custom-sidebar-text-200">Projects</h5>
</div> )}
)} {orderedAllProjects.map((project, index) => (
{allProjects && allProjects.length > 0 && ( <Draggable key={project.id} draggableId={project.id} index={index}>
<div className="flex flex-col space-y-2 mt-5"> {(provided, snapshot) => (
{!sidebarCollapse && ( <div ref={provided.innerRef} {...provided.draggableProps}>
<h5 className="text-sm font-medium text-custom-sidebar-text-200">Projects</h5> <SingleSidebarProject
key={project.id}
project={project}
sidebarCollapse={sidebarCollapse}
provided={provided}
snapshot={snapshot}
handleDeleteProject={() => handleDeleteProject(project)}
handleCopyText={() => handleCopyText(project.id)}
/>
</div>
)}
</Draggable>
))}
{provided.placeholder}
</div>
)}
</div>
)} )}
{allProjects.map((project) => ( </Droppable>
<SingleSidebarProject </DragDropContext>
key={project.id}
project={project}
sidebarCollapse={sidebarCollapse}
handleDeleteProject={() => handleDeleteProject(project)}
handleCopyText={() => handleCopyText(project.id)}
/>
))}
</div>
)}
{allProjects && allProjects.length === 0 && ( {allProjects && allProjects.length === 0 && (
<button <button
type="button" type="button"

View File

@ -3,6 +3,8 @@ import { useRouter } from "next/router";
import { mutate } from "swr"; import { mutate } from "swr";
// react-beautiful-dnd
import { DraggableProvided, DraggableStateSnapshot } from "react-beautiful-dnd";
// headless ui // headless ui
import { Disclosure, Transition } from "@headlessui/react"; import { Disclosure, Transition } from "@headlessui/react";
// services // services
@ -12,7 +14,7 @@ import useToast from "hooks/use-toast";
// ui // ui
import { CustomMenu, Tooltip } from "components/ui"; import { CustomMenu, Tooltip } from "components/ui";
// icons // icons
import { LinkIcon, StarIcon, TrashIcon } from "@heroicons/react/24/outline"; import { EllipsisVerticalIcon, LinkIcon, StarIcon, TrashIcon } from "@heroicons/react/24/outline";
import { import {
ArchiveOutlined, ArchiveOutlined,
ArticleOutlined, ArticleOutlined,
@ -34,6 +36,8 @@ import { PROJECTS_LIST } from "constants/fetch-keys";
type Props = { type Props = {
project: IProject; project: IProject;
sidebarCollapse: boolean; sidebarCollapse: boolean;
provided: DraggableProvided;
snapshot: DraggableStateSnapshot;
handleDeleteProject: () => void; handleDeleteProject: () => void;
handleCopyText: () => void; handleCopyText: () => void;
shortContextMenu?: boolean; shortContextMenu?: boolean;
@ -75,6 +79,8 @@ const navigation = (workspaceSlug: string, projectId: string) => [
export const SingleSidebarProject: React.FC<Props> = ({ export const SingleSidebarProject: React.FC<Props> = ({
project, project,
sidebarCollapse, sidebarCollapse,
provided,
snapshot,
handleDeleteProject, handleDeleteProject,
handleCopyText, handleCopyText,
shortContextMenu = false, shortContextMenu = false,
@ -130,7 +136,19 @@ export const SingleSidebarProject: React.FC<Props> = ({
<Disclosure key={project?.id} defaultOpen={projectId === project?.id}> <Disclosure key={project?.id} defaultOpen={projectId === project?.id}>
{({ open }) => ( {({ open }) => (
<> <>
<div className="flex items-center gap-x-1 text-custom-sidebar-text-100"> <div
className={`group relative flex items-center gap-x-1 text-custom-sidebar-text-100 ${
snapshot.isDragging ? "opacity-60" : ""
}`}
>
<button
type="button"
className="absolute top-2 left-0 hidden rounded p-0.5 group-hover:!flex"
{...provided.dragHandleProps}
>
<EllipsisVerticalIcon className="h-4" />
<EllipsisVerticalIcon className="-ml-5 h-4" />
</button>
<Tooltip <Tooltip
tooltipContent={`${project?.name}`} tooltipContent={`${project?.name}`}
position="right" position="right"
@ -139,7 +157,7 @@ export const SingleSidebarProject: React.FC<Props> = ({
> >
<Disclosure.Button <Disclosure.Button
as="div" as="div"
className={`flex w-full cursor-pointer select-none items-center rounded-sm py-1 text-left text-sm font-medium ${ className={`flex w-full ml-4 cursor-pointer select-none items-center rounded-sm py-1 text-left text-sm font-medium ${
sidebarCollapse ? "justify-center" : "justify-between" sidebarCollapse ? "justify-center" : "justify-between"
}`} }`}
> >

View File

@ -254,6 +254,7 @@ class ProjectServices extends APIService {
view_props?: ProjectViewTheme; view_props?: ProjectViewTheme;
default_props?: ProjectViewTheme; default_props?: ProjectViewTheme;
preferences?: ProjectPreferences; preferences?: ProjectPreferences;
sort_order?: number;
} }
): Promise<any> { ): Promise<any> {
await this.post(`/api/workspaces/${workspaceSlug}/projects/${projectId}/project-views/`, data) await this.post(`/api/workspaces/${workspaceSlug}/projects/${projectId}/project-views/`, data)

View File

@ -45,6 +45,7 @@ export interface IProject {
network: number; network: number;
page_view: boolean; page_view: boolean;
project_lead: IUserLite | string | null; project_lead: IUserLite | string | null;
sort_order: number;
slug: string; slug: string;
total_cycles: number; total_cycles: number;
total_members: number; total_members: number;