import React from 'react'; import Box from './Box'; import uuid from 'uuid/v4'; import { SortableContainer, SortableElement, arrayMove, } from 'react-sortable-hoc'; import DragHandle from './DragHandle'; import { FieldArray, Formik, Form as FormikForm, Field } from 'formik'; import PropTypes from 'proptypes'; import Select from 'react-select'; import 'react-select/dist/react-select.css'; import { filter, find, findIndex, flatten, forEach, identity, map, prop, propEq, reject, uniq, update } from 'rambda'; import { insert, remove } from 'ramda'; import theme from '../theme'; const allJobs = { whatever: 'Vševediaci', marketResearch: 'Prieskum trhu', designUi: 'Dizajnér – UI', designUx: 'Dizajnér – UX', architect: 'Softwarový architekt', devFrontend: 'Developer – frontend', devBackend: 'Developer – backend', devDb: 'Developer – databázy', admin: 'Serverový administrátor', tester: 'Tester', analyst: 'Analytik', qa: 'Quality assurance', deployManager: 'Deploy' }; const nextWorkerName = (rows) => `${rows.length + 1}. pracovník`; class Form extends React.PureComponent { state = { rows: [ // { // id: uuid(), // name: 'pracovnik 1', // preempt: false, // linkedTo: null, // jobs: [ // { id: uuid(), name: 'frontend', t: '', d: '', w: '', succ: [], anc: [], succOrAnc: 0 }, // { id: uuid(), name: 'backend', t: '', d: '', w: '', succ: [], anc: [], succOrAnc: 0 } // ] // }, // { // id: uuid(), // name: 'pracovnik 2', // preempt: false, // linkedTo: null, // jobs: [ // { id: uuid(), name: 'frontend', t: '', d: '', w: '', succ: [], anc: [], succOrAnc: 0 }, // { id: uuid(), name: 'backend', t: '', d: '', w: '', succ: [], anc: [], succOrAnc: 0 } // ] // } ] }; onSubmit = (values) => { console.log(values); }; addWorker = (setFieldValue, rows) => { const jobs = [{ id: uuid(), name: 'whatever', t: '', d: '', w: '', succ: [], anc: [], succOrAnc: 0 }]; const updatedRows = [...rows, { id: uuid(), name: nextWorkerName(rows), jobs } ]; setFieldValue('rows', updatedRows); }; render() { return ( (

Konfigurácia projektu

{rows.length ? Pracovník Úlohy Prerušenie : null } ( {rows.map((row, i) => )} )} /> this.addWorker(setFieldValue, rows)} {...theme.buttons.primary} > + Nový pracovník Cieľ:
)}>
) } } const SortableJob = SortableElement( class extends React.PureComponent { static contextTypes = { formik: PropTypes.object }; handleSelectChange = (type, selectedPairs) => { const { prefix } = this.props; const { formik: { setFieldValue } } = this.context; const selected = map(prop('value'), selectedPairs); setFieldValue(`${prefix}.${type}`, selected); }; deleteJob = () => { const { rowIndex, myIndex } = this.props; const { formik: { setFieldValue, values: { rows } } } = this.context; const jobs = filter(identity, update(myIndex, null, rows[rowIndex].jobs)); if (jobs.length) { setFieldValue(`rows.${rowIndex}.jobs`, jobs); } else { const updatedRows = filter(identity, update(rowIndex, null, rows)); setFieldValue(`rows`, updatedRows); } }; render() { const { myIndex, rowIndex, prefix, job, existingJobs: _existingJobs } = this.props; const { formik: { values: { rows } } } = this.context; const existingJobs = map( value => { const [idRow, idJob] = value.split(':'); const row = find(propEq('id', idRow), rows); const jobName = find(propEq('id', idJob), row.jobs).name; return ({ value, label: `${row.name} – ${allJobs[jobName]}` }); }, reject( value => { const [, idJob] = value.split(':'); return idJob === job.id; }, _existingJobs ) ); return ( {!rows[rowIndex].linkedTo && } {Object.keys(allJobs).map(key => ( ))} {!rows[rowIndex].linkedTo && } Bez nadväznosti Predchodcovia this.handleSelectChange('succ', e)} options={existingJobs} removeSelected value={job.succ} /> ); } }); const SortableList = SortableContainer(({ jobs, existingJobs, prefix, rowIndex, draggingIndex }) => (
{jobs.map((job, i) => ( ))}
)); class Row extends React.PureComponent { static contextTypes = { formik: PropTypes.object }; state = { draggingIndex: -1 }; onSortStart = ({ index }) => { this.setState({ draggingIndex: index }); }; onSortEnd = ({ oldIndex, newIndex }) => { const { prefix, index } = this.props; const { formik: { setFieldValue, values: { rows }} } = this.context; const updatedJobs = arrayMove(rows[index].jobs, oldIndex, newIndex); setFieldValue(`${prefix}.jobs`, updatedJobs); this.setState({ draggingIndex: -1 }); }; findExistingJobs = () => { const { formik: { values: { rows } } } = this.context; const existingJobs = uniq( flatten( map(({ id: idRow, jobs }) => map(({ id: idJob }) => `${idRow}:${idJob}`, jobs), rows) ) ); return existingJobs; }; addJob = () => { const { index } = this.props; const { formik: { setFieldValue, values: { rows } } } = this.context; const newJob = { id: uuid(), name: 'whatever', t: '', d: '', w: '', succ: [], anc: [], succOrAnc: 0 }; const jobs = [...rows[index].jobs, newJob]; setFieldValue(`rows.${index}.jobs`, jobs); }; deleteRow = () => { const { index, id } = this.props; const { formik: { setFieldValue, values: { rows } } } = this.context; let newRows = remove(index, 1, rows); const alsoDelete = filter(propEq('linkedTo', id), rows); forEach(row => { const index = findIndex(propEq('id', row.id), newRows); newRows = remove(index, 1, newRows); }, alsoDelete); setFieldValue('rows', newRows); }; duplicate = () => { const { id, index, name, preempt, jobs } = this.props; const { formik: { setFieldValue, values: { rows } } } = this.context; const jobsCopy = map(job => ({ ...job }), jobs); const newRow = { id: uuid(), name: `${name} – kópia`, preempt, linkedTo: id, jobs: jobsCopy }; const updatedRows = insert(index + 1, newRow, rows); setFieldValue('rows', updatedRows); }; render() { const { index, prefix, name, preempt, linkedTo, jobs } = this.props; const { draggingIndex } = this.state; const existingJobs = this.findExistingJobs(); return ( {name} ( )} /> {!linkedTo && + Nová úloha } Úlohy pracovníka možno prerušiť {!linkedTo && Duplikovať } ) } } export default Form;