2022-11-19 14:21:26 +00:00
import React , { useEffect , useState } from "react" ;
// next
import { useRouter } from "next/router" ;
import type { NextPage } from "next" ;
// swr
import useSWR , { mutate } from "swr" ;
// services
import issuesServices from "lib/services/issues.services" ;
import sprintService from "lib/services/cycles.services" ;
// hooks
import useUser from "lib/hooks/useUser" ;
// fetching keys
import { CYCLE_ISSUES , CYCLE_LIST } from "constants/fetch-keys" ;
2022-12-08 14:59:12 +00:00
// hoc
import withAuth from "lib/hoc/withAuthWrapper" ;
2022-11-19 14:21:26 +00:00
// layouts
2022-12-07 23:27:10 +00:00
import AppLayout from "layouts/AppLayout" ;
2022-11-19 14:21:26 +00:00
// components
2022-11-29 14:19:39 +00:00
import CycleView from "components/project/cycles/CycleView" ;
2022-11-19 14:21:26 +00:00
import ConfirmIssueDeletion from "components/project/issues/ConfirmIssueDeletion" ;
import ConfirmSprintDeletion from "components/project/cycles/ConfirmCycleDeletion" ;
import CreateUpdateIssuesModal from "components/project/issues/CreateUpdateIssueModal" ;
import CreateUpdateSprintsModal from "components/project/cycles/CreateUpdateCyclesModal" ;
// ui
2022-11-29 14:19:39 +00:00
import { BreadcrumbItem , Breadcrumbs , HeaderButton , Spinner , EmptySpace , EmptySpaceItem } from "ui" ;
2022-11-19 14:21:26 +00:00
// icons
import { PlusIcon } from "@heroicons/react/20/solid" ;
2022-11-29 14:19:39 +00:00
import { ArrowPathIcon } from "@heroicons/react/24/outline" ;
2022-11-19 14:21:26 +00:00
// types
2022-12-06 14:38:28 +00:00
import { IIssue , ICycle , SelectSprintType , SelectIssue , CycleIssueResponse } from "types" ;
import { DragDropContext , DropResult } from "react-beautiful-dnd" ;
2022-11-19 14:21:26 +00:00
const ProjectSprints : NextPage = ( ) = > {
const [ isOpen , setIsOpen ] = useState ( false ) ;
const [ selectedSprint , setSelectedSprint ] = useState < SelectSprintType > ( ) ;
const [ isIssueModalOpen , setIsIssueModalOpen ] = useState ( false ) ;
const [ selectedIssues , setSelectedIssues ] = useState < SelectIssue > ( ) ;
const [ deleteIssue , setDeleteIssue ] = useState < string | undefined > ( ) ;
2022-12-06 14:38:28 +00:00
const { activeWorkspace , activeProject , issues } = useUser ( ) ;
2022-11-19 14:21:26 +00:00
const router = useRouter ( ) ;
const { projectId } = router . query ;
2022-11-29 14:19:39 +00:00
const { data : cycles } = useSWR < ICycle [ ] > (
2022-11-19 14:21:26 +00:00
projectId && activeWorkspace ? CYCLE_LIST ( projectId as string ) : null ,
activeWorkspace && projectId
? ( ) = > sprintService . getCycles ( activeWorkspace . slug , projectId as string )
: null
) ;
const openIssueModal = (
2022-11-29 14:19:39 +00:00
cycleId : string ,
2022-11-19 14:21:26 +00:00
issue? : IIssue ,
actionType : "create" | "edit" | "delete" = "create"
) = > {
2022-11-29 14:19:39 +00:00
const cycle = cycles ? . find ( ( cycle ) = > cycle . id === cycleId ) ;
if ( cycle ) {
2022-11-19 14:21:26 +00:00
setSelectedSprint ( {
2022-11-29 14:19:39 +00:00
. . . cycle ,
2022-11-19 14:21:26 +00:00
actionType : "create-issue" ,
} ) ;
if ( issue ) setSelectedIssues ( { . . . issue , actionType } ) ;
setIsIssueModalOpen ( true ) ;
}
} ;
2022-11-29 14:19:39 +00:00
const addIssueToSprint = ( cycleId : string , issueId : string ) = > {
2022-11-19 14:21:26 +00:00
if ( ! activeWorkspace || ! projectId ) return ;
issuesServices
2022-11-29 14:19:39 +00:00
. addIssueToSprint ( activeWorkspace . slug , projectId as string , cycleId , {
2022-11-19 14:21:26 +00:00
issue : issueId ,
} )
. then ( ( response ) = > {
console . log ( response ) ;
2022-11-29 14:19:39 +00:00
mutate ( CYCLE_ISSUES ( cycleId ) ) ;
2022-11-19 14:21:26 +00:00
} )
. catch ( ( error ) = > {
console . log ( error ) ;
} ) ;
} ;
2022-12-06 14:38:28 +00:00
const handleDragEnd = ( result : DropResult ) = > {
if ( ! result . destination ) return ;
const { source , destination } = result ;
if ( source . droppableId === destination . droppableId ) return ;
if ( activeWorkspace && activeProject ) {
// remove issue from the source cycle
mutate < CycleIssueResponse [ ] > (
CYCLE_ISSUES ( source . droppableId ) ,
( prevData ) = > prevData ? . filter ( ( p ) = > p . id !== result . draggableId . split ( "," ) [ 0 ] ) ,
false
) ;
// add issue to the destination cycle
mutate ( CYCLE_ISSUES ( destination . droppableId ) ) ;
// mutate<CycleIssueResponse[]>(
// CYCLE_ISSUES(destination.droppableId),
// (prevData) => {
// const issueDetails = issues?.results.find(
// (i) => i.id === result.draggableId.split(",")[1]
// );
// const targetResponse = prevData?.find((t) => t.cycle === destination.droppableId);
// console.log(issueDetails, targetResponse, prevData);
// if (targetResponse) {
// console.log("if");
// targetResponse.issue_details = issueDetails as IIssue;
// return prevData;
// } else {
// console.log("else");
// return [
// ...(prevData ?? []),
// {
// cycle: destination.droppableId,
// issue_details: issueDetails,
// } as CycleIssueResponse,
// ];
// }
// },
// false
// );
issuesServices
. removeIssueFromCycle (
activeWorkspace . slug ,
activeProject . id ,
source . droppableId ,
result . draggableId . split ( "," ) [ 0 ]
)
. then ( ( res ) = > {
issuesServices
. addIssueToSprint ( activeWorkspace . slug , activeProject . id , destination . droppableId , {
issue : result.draggableId.split ( "," ) [ 1 ] ,
} )
. then ( ( res ) = > {
console . log ( res ) ;
} )
. catch ( ( e ) = > {
console . log ( e ) ;
} ) ;
} )
. catch ( ( e ) = > {
console . log ( e ) ;
} ) ;
}
// console.log(result);
} ;
2022-11-19 14:21:26 +00:00
useEffect ( ( ) = > {
if ( isOpen ) return ;
const timer = setTimeout ( ( ) = > {
setSelectedSprint ( undefined ) ;
clearTimeout ( timer ) ;
} , 500 ) ;
} , [ isOpen ] ) ;
useEffect ( ( ) = > {
if ( selectedIssues ? . actionType === "delete" ) {
setDeleteIssue ( selectedIssues . id ) ;
}
} , [ selectedIssues ] ) ;
return (
2022-12-07 23:27:10 +00:00
< AppLayout
2022-11-19 14:21:26 +00:00
meta = { {
title : "Plane - Cycles" ,
} }
>
< CreateUpdateSprintsModal
isOpen = {
isOpen &&
selectedSprint ? . actionType !== "delete" &&
selectedSprint ? . actionType !== "create-issue"
}
setIsOpen = { setIsOpen }
data = { selectedSprint }
projectId = { projectId as string }
/ >
< ConfirmSprintDeletion
isOpen = { isOpen && ! ! selectedSprint && selectedSprint . actionType === "delete" }
setIsOpen = { setIsOpen }
data = { selectedSprint }
/ >
< ConfirmIssueDeletion
handleClose = { ( ) = > setDeleteIssue ( undefined ) }
isOpen = { ! ! deleteIssue }
data = { selectedIssues }
/ >
< CreateUpdateIssuesModal
isOpen = {
isIssueModalOpen &&
selectedSprint ? . actionType === "create-issue" &&
selectedIssues ? . actionType !== "delete"
}
data = { selectedIssues }
prePopulateData = { { sprints : selectedSprint?.id } }
setIsOpen = { setIsOpen }
projectId = { projectId as string }
/ >
2022-11-29 14:19:39 +00:00
{ cycles ? (
cycles . length > 0 ? (
2022-11-24 13:48:18 +00:00
< div className = "h-full w-full space-y-5" >
< Breadcrumbs >
< BreadcrumbItem title = "Projects" link = "/projects" / >
< BreadcrumbItem title = { ` ${ activeProject ? . name ? ? "Project" } Cycles ` } / >
< / Breadcrumbs >
< div className = "flex items-center justify-between cursor-pointer w-full" >
< h2 className = "text-2xl font-medium" > Project Cycle < / h2 >
< HeaderButton Icon = { PlusIcon } label = "Add Cycle" onClick = { ( ) = > setIsOpen ( true ) } / >
2022-11-19 14:21:26 +00:00
< / div >
2022-12-06 14:38:28 +00:00
< div className = "space-y-5" >
< DragDropContext onDragEnd = { handleDragEnd } >
{ cycles . map ( ( cycle ) = > (
< CycleView
key = { cycle . id }
cycle = { cycle }
selectSprint = { setSelectedSprint }
projectId = { projectId as string }
workspaceSlug = { activeWorkspace ? . slug as string }
openIssueModal = { openIssueModal }
addIssueToSprint = { addIssueToSprint }
/ >
) ) }
< / DragDropContext >
2022-11-24 13:48:18 +00:00
< / div >
< / div >
2022-11-19 14:21:26 +00:00
) : (
2022-11-24 13:48:18 +00:00
< div className = "w-full h-full flex flex-col justify-center items-center px-4" >
< EmptySpace
title = "You don't have any cycle yet."
description = "A cycle is a fixed time period where a team commits to a set number of issues from their backlog. Cycles are usually one, two, or four weeks long."
Icon = { ArrowPathIcon }
>
< EmptySpaceItem
title = "Create a new cycle"
description = {
< span >
Use < pre className = "inline bg-gray-100 px-2 py-1 rounded" > Ctrl / Command + Q < / pre > { " " }
shortcut to create a new cycle
< / span >
}
Icon = { PlusIcon }
action = { ( ) = > setIsOpen ( true ) }
/ >
< / EmptySpace >
2022-11-19 14:21:26 +00:00
< / div >
2022-11-24 13:48:18 +00:00
)
) : (
< div className = "w-full h-full flex justify-center items-center" >
< Spinner / >
< / div >
) }
2022-12-07 23:27:10 +00:00
< / AppLayout >
2022-11-19 14:21:26 +00:00
) ;
} ;
2022-12-08 14:59:12 +00:00
export default withAuth ( ProjectSprints ) ;