import React from 'react'; import { Page } from '../Page'; import { connect, DispatchProp } from 'react-redux'; import { selectFlagsIsLoading } from '../../store/selectors/flags'; import { fetchBoards, saveBoard, deleteBoard } from '../../store/actions/boards'; import { fetchProjects } from '../../store/actions/projects'; import uuidv4 from 'uuid/v4'; import { Loader } from '../Loader'; import { RouteComponentProps } from 'react-router'; export interface EditorBoardPageProps extends DispatchProp, RouteComponentProps { isLoading: boolean projects: any } export class EditBoardPage extends React.Component { state = { edited: false, board: { id: "", title: "", description: "", projects: [], lanes: [] }, } onBoardTitleChange: (evt: any) => void; onBoardDescriptionChange: (evt: any) => void; onBoardLaneTitleChange: (laneIndex: any, evt: any) => void; onBoardLaneIssueLabelChange: (laneIndex: any, evt: any) => void; onBoardLaneIssueCollectRemainingIssuesChange: (laneIndex: any, evt: any) => void; static getDerivedStateFromProps(props: any, state: any) { const { board, isLoading } = props; if (isLoading || !board || state.edited) return state; return { edited: false, board: { id: board.id, title: board.title, description: board.description, projects: [ ...board.projects ], lanes: [ ...board.lanes.map((l: any) => ({ ...l })) ] } }; } constructor(props: any) { super(props); this.onBoardTitleChange = this.onBoardAttrChange.bind(this, 'title'); this.onBoardDescriptionChange = this.onBoardAttrChange.bind(this, 'description'); this.onBoardLaneTitleChange = this.onBoardLaneAttrChange.bind(this, 'title'); this.onBoardLaneIssueLabelChange = this.onBoardLaneAttrChange.bind(this, 'issueLabel'); this.onBoardLaneIssueCollectRemainingIssuesChange = this.onBoardLaneAttrChange.bind(this, 'collectRemainingIssues'); this.onDeleteBoardClick = this.onDeleteBoardClick.bind(this); } render() { const { isLoading } = this.props; const { board } = this.state; if (isLoading) { return ( ) }; return (
{ board.id ?

Éditer le tableau

:

Nouveau tableau

}
{ board.id ? : null }

{ this.renderProjectSelect() }
{ this.renderLanesSection() }
); } renderProjectSelect() { const { projects } = this.props; const { board } = this.state; const projectSelectField = (projectIndex: number, value: any, withDeleteAddon: boolean) => { return (
{ withDeleteAddon ?
: null }
); } return ( { board.projects.map((p, i) => { return projectSelectField(i, p, true); }) } { projectSelectField(board.projects.length, '', false) } ) } renderLanesSection() { const { board } = this.state; const laneSection = (laneIndex: number, lane: any) => { return (

); }; const lanes = board.lanes.map((l, i) => laneSection(i, l)) return (

Ajouter une voie

{ lanes }
) } onBoardLaneAdd() { this.setState((state: any) => { const lanes = [ ...state.board.lanes, { id: uuidv4(), title: "", issueLabel: "" } ]; return { ...state, edited: true, board: { ...state.board, lanes } } }); } onBoardProjectDelete(projectIndex: number) { this.setState((state: any) => { const projects = [ ...state.board.projects ] projects.splice(projectIndex, 1); return { ...state, edited: true, board: { ...state.board, projects } } }); } onBoardLaneMove(laneIndex: number, direction: number) { this.setState((state: any) => { const lanes = [ ...state.board.lanes ]; const nextLaneIndex = laneIndex+direction; if (nextLaneIndex < 0 || nextLaneIndex >= lanes.length) { return state; } const l = lanes[laneIndex]; lanes.splice(laneIndex, 1); lanes.splice(nextLaneIndex, 0, l); return { ...state, edited: true, board: { ...state.board, lanes } } }); } onBoardLaneDelete(laneIndex: number) { this.setState((state: any) => { const lanes = [ ...state.board.lanes ] lanes.splice(laneIndex, 1); return { ...state, edited: true, board: { ...state.board, lanes } } }); } onBoardProjectChange(projectIndex: number, evt: React.ChangeEvent) { const value = (evt.target as HTMLInputElement).value ; this.setState((state: any) => { const projects = [ ...state.board.projects ]; projects[projectIndex] = value; return { ...state, edited: true, board: { ...state.board, projects } } }); } onBoardAttrChange(attrName: string, evt: React.ChangeEvent) { const value = (evt.target as HTMLInputElement).value; this.setState((state: any) => { return { ...state, edited: true, board: { ...state.board, [attrName]: value, } } }); } onBoardLaneAttrChange(attrName: string, laneIndex: number, evt: React.ChangeEvent) { const input = evt.target as HTMLInputElement; const value = input.type === "checkbox" ? input.checked : input.value; this.setState((state: any) => { const lanes = [ ...state.board.lanes ]; lanes[laneIndex] = { ...state.board.lanes[laneIndex], [attrName]: value }; console.log(lanes); return { ...state, edited: true, board: { ...state.board, lanes } } }); } onSaveBoardClick() { let { board } = this.state; board = { ...board }; if (!board.id) board.id = uuidv4(); this.props.dispatch(saveBoard({...board})); this.props.history.push('/'); } onDeleteBoardClick() { const { board } = this.state; this.props.dispatch(deleteBoard(board.id)); this.props.history.push('/'); } componentDidMount() { this.props.dispatch(fetchBoards()); this.props.dispatch(fetchProjects()); } } export const ConnectedEditBoardPage = connect(function(state: any, props: any) { const boardID = props.match.params.id; const board = boardID ? state.boards.byID[boardID] : null; const projects = Object.keys(state.projects.byName).sort(); const isLoading = selectFlagsIsLoading(state, 'FETCH_BOARDS', 'FETCH_PROJECTS'); return { board, isLoading, projects }; })(EditBoardPage);