import { useState, useEffect, useMemo, useRef, useCallback } from 'react'; import { getData } from '../data'; import { Gantt, defaultColumns, Editor } from '../../src/'; import { Field, Switch } from '@svar-ui/react-core'; import './GanttPreventActions.css'; function GanttPreventActions({ skinSettings }) { const data = useMemo(() => getData(), []); const [edit, setEdit] = useState(true); // if false - cannot add and edit task const [drag, setDrag] = useState(true); // if false - cannot drag tasks on scale const [order, setOrder] = useState(true); // if false - cannot reorder tasks in grid const [newLink, setNewLink] = useState(true); // if false - cannot create new links const ignoreRef = useRef(false); const [api, setApi] = useState(); const editRef = useRef(edit); const dragRef = useRef(drag); const orderRef = useRef(order); useEffect(() => { editRef.current = edit; }, [edit]); useEffect(() => { dragRef.current = drag; }, [drag]); useEffect(() => { orderRef.current = order; }, [order]); const init = useCallback((gApi) => { setApi(gApi); gApi.intercept('show-editor', () => editRef.current || ignoreRef.current); gApi.intercept('drag-task', (ev) => { if (typeof ev.top !== 'undefined') return orderRef.current; return dragRef.current; // ev.width && ev.left }); }, []); const columns = useMemo( () => edit ? defaultColumns : defaultColumns.filter((a) => a.id != 'add-task'), [edit], ); // for demo purposes: close editor when checkbox is unchecked useEffect(() => { if (!edit) { ignoreRef.current = true; api.exec('show-editor', { id: null }); ignoreRef.current = false; } }, [edit, api]); return (
{({ id }) => ( setEdit(value)} id={id} /> )} {({ id }) => ( setNewLink(value)} id={id} /> )} {({ id }) => ( setDrag(value)} id={id} /> )} {({ id }) => ( setOrder(value)} id={id} /> )}
{api && }
); } export default GanttPreventActions;