|
| 1 | +import { useCallback, useEffect, useState } from 'react'; |
| 2 | +import { StoryExpanded, useProjectQueries, Backlog } from './queries'; |
| 3 | +import { StoryRow } from './StoryRow'; |
| 4 | +import { BacklogHeader} from './BacklogHeader'; |
| 5 | +import { useSubmitForm2 } from './mutations'; |
| 6 | +import { useProjectIdentifier } from '../hooks/useProjectIdentifier'; |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +// <pre>{JSON.stringify(backlogsQuery.data, null, 2)}</pre> |
| 11 | +export interface BacklogProps { |
| 12 | + backlog:Backlog |
| 13 | +} |
| 14 | + |
| 15 | +export function BacklogTable({backlog}:BacklogProps) { |
| 16 | + const projectIdentifier = useProjectIdentifier(); |
| 17 | + const [_, typesQuery, statusesQuery] = useProjectQueries(projectIdentifier); |
| 18 | + const types = typesQuery.data?._embedded.elements ?? []; |
| 19 | + const statuses = statusesQuery.data?._embedded.elements ?? []; |
| 20 | + const [stories, setStories] = useState<StoryExpanded[]>([]); |
| 21 | + const { mutate, error, isSuccess } = useSubmitForm2(projectIdentifier, backlog.sprint.id); |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + if (!types.length || !statuses.length) return; |
| 25 | + |
| 26 | + const expanded = backlog.stories.map((story):StoryExpanded => { |
| 27 | + const type = types.find((t) => t.id === story.type_id)!; |
| 28 | + const status = statuses.find((s) => s.id === story.status_id)!; |
| 29 | + return { ...story, type, status }; |
| 30 | + }); |
| 31 | + |
| 32 | + // Sort by position before storing |
| 33 | + setStories(expanded.sort((a, b) => a.position - b.position)); |
| 34 | + }, [backlog.stories, types, statuses]); |
| 35 | + |
| 36 | + const totalPoints = stories |
| 37 | + .map((story) => story.story_points ?? 0) |
| 38 | + .reduce((accum, value) => accum + value, 0); |
| 39 | + |
| 40 | + const moveItem = useCallback((dragIndex:number, hoverIndex:number) => { |
| 41 | + setStories(prev => { |
| 42 | + const updated = [...prev].sort((a, b) => a.position - b.position); |
| 43 | + const [removed] = updated.splice(dragIndex, 1); |
| 44 | + updated.splice(hoverIndex, 0, removed); |
| 45 | + |
| 46 | + return updated.map((story, i) => ({ ...story, position: i })); |
| 47 | + }); |
| 48 | + }, []); |
| 49 | + |
| 50 | + const getCurrentPosition = (id:number | string) => stories.find(s => s.id === id)?.position ?? 0; |
| 51 | + |
| 52 | + const updatePosition = (id:number|string, position:number) => { |
| 53 | + mutate({ id: Number(id), position }); |
| 54 | + }; |
| 55 | + |
| 56 | + return ( |
| 57 | + <div className="position-relative Box Box--condensed" id={`backlog_${backlog.sprint.id}`}> |
| 58 | + <div className="Box-header color-fg-muted"> |
| 59 | + <BacklogHeader backlog={backlog}></BacklogHeader> |
| 60 | + </div> |
| 61 | + {stories.length > 0 && ( |
| 62 | + <ul className="stories"> |
| 63 | + {stories.sort((a, b) => a.position - b.position).map((story, index) => { |
| 64 | + return ( |
| 65 | + <li key={story.id} className="Box-row"> |
| 66 | + <StoryRow |
| 67 | + story={story} |
| 68 | + projectId={backlog.sprint.project_id} |
| 69 | + sprintId={backlog.sprint.id} |
| 70 | + index={index} |
| 71 | + moveItem={moveItem} |
| 72 | + getCurrentPosition={getCurrentPosition} |
| 73 | + updatePosition={updatePosition} |
| 74 | + ></StoryRow> |
| 75 | + </li> |
| 76 | + ); |
| 77 | + })} |
| 78 | + ; |
| 79 | + </ul> |
| 80 | + )} |
| 81 | + {stories.length === 0 && <div className="Box-body">No content</div>} |
| 82 | + </div> |
| 83 | + ); |
| 84 | +} |
0 commit comments