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, flatten, identity, map, prop, propEq, reject, uniq, update } from 'rambda'; import { insert } from 'ramda'; const allJobs = { frontend: 'Frontend', backend: 'Backend', }; 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 } ] } ] }; addWorker = (setFieldValue, rows) => { const updatedRows = [...rows, { id: uuid(), name: nextWorkerName(rows), jobs: [] } ]; setFieldValue('rows', updatedRows); }; render() { return ( (

Konfigurácia projektu

Pracovník Úlohy Prerušenie ( {rows.map((row, i) => )} )} /> this.addWorker(setFieldValue, rows)} style={{ cursor: 'pointer' }}> + Nový pracovník
)}>
) } } 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} – ${jobName}` }); }, reject( value => { const [, idJob] = value.split(':'); return idJob === job.id; }, _existingJobs ) ); return ( {Object.keys(allJobs).map(key => ( ))} Bez nadväznosti Predchodcovia this.handleSelectChange('succ', e)} options={existingJobs} removeSelected value={job.succ} /> ); } }); const SortableList = SortableContainer(({ jobs, existingJobs, prefix, rowIndex }) => (
{jobs.map((job, i) => ( ))}
)); class Row extends React.PureComponent { static contextTypes = { formik: PropTypes.object }; 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); }; 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: 'frontend', t: '', d: '', w: '', succ: [], anc: [], succOrAnc: 0 }; const jobs = [...rows[index].jobs, newJob]; setFieldValue(`rows.${index}.jobs`, jobs); }; 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 existingJobs = this.findExistingJobs(); return ( {name} ( )} /> + Nová úloha ) } } export default Form;