Skip to content

Commit

Permalink
feat: handle invalid tree yaml in modal
Browse files Browse the repository at this point in the history
  • Loading branch information
rafgugi committed Jul 29, 2023
1 parent b2bdc24 commit 8897c5c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/components/ModalAddChild.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ function ModalAddChild({
}
};

const validForm = () => person && spouse && child && !childError;
const validForm = person && spouse && child && !childError;

const handleSubmit = () => {
if (!person || !spouse || !validForm()) return;
if (!person || !spouse || !validForm) return;

const marriage = person.marriages.find(
(m: Marriage) => m.spouse.id === spouse.id
Expand Down Expand Up @@ -142,7 +142,7 @@ function ModalAddChild({
)}
</ModalBody>
<ModalFooter>
<Button disabled={!validForm()} onClick={handleSubmit}>
<Button disabled={!validForm} onClick={handleSubmit}>
Submit
</Button>
</ModalFooter>
Expand Down
6 changes: 3 additions & 3 deletions src/components/ModalAddSpouse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ function ModalAddSpouse({
}
};

const validForm = () => person && spouse && !spouseError;
const validForm = person && spouse && !spouseError;

const handleSubmit = () => {
if (!person || !validForm()) return;
if (!person || !validForm) return;

const spousePerson: Person = {
id: spouse,
Expand Down Expand Up @@ -105,7 +105,7 @@ function ModalAddSpouse({
)}
</ModalBody>
<ModalFooter>
<Button disabled={!validForm()} onClick={handleSubmit}>
<Button disabled={!validForm} onClick={handleSubmit}>
Submit
</Button>
</ModalFooter>
Expand Down
6 changes: 3 additions & 3 deletions src/components/ModalAddTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ function ModalAddTree({ isOpen, toggle }: ModalAddTreeProps) {
}
};

const validForm = () => child && !childError;
const validForm = child && !childError;

const handleSubmit = () => {
if (!validForm()) return;
if (!validForm) return;

const tree: Person = {
id: child,
Expand Down Expand Up @@ -67,7 +67,7 @@ function ModalAddTree({ isOpen, toggle }: ModalAddTreeProps) {
</FormGroup>
</ModalBody>
<ModalFooter>
<Button disabled={!validForm()} onClick={handleSubmit}>
<Button disabled={!validForm} onClick={handleSubmit}>
Submit
</Button>
</ModalFooter>
Expand Down
38 changes: 36 additions & 2 deletions src/components/ModalEditYaml.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { useContext, useMemo } from 'react';
import {
useContext,
useDeferredValue,
useEffect,
useMemo,
useState,
} from 'react';
import {
Button,
FormFeedback,
FormGroup,
Input,
Label,
Expand All @@ -9,6 +16,7 @@ import {
ModalFooter,
ModalHeader,
} from 'reactstrap';
import { enrichTreeData } from '../family.util';
import { parse } from 'yaml';
import AppContext from './AppContext';

Expand All @@ -29,19 +37,39 @@ function ModalDeletePerson({
toggle,
}: ModalDeletePersonProps) {
const { setTreesValue } = useContext(AppContext);
const [yamlError, setYamlError] = useState('');
const deferredTreeYaml = useDeferredValue(treeYaml);
const loading = deferredTreeYaml !== treeYaml;

const validForm = treeYaml && !yamlError;

useEffect(() => {
try {
// test parsing the yaml
enrichTreeData(parse(deferredTreeYaml), []);
if (yamlError !== '') {
setYamlError('');
}
} catch (e: any) {
setYamlError(e.message);
}
}, [deferredTreeYaml, yamlError]);

const rows = useMemo(() => {
const lines = treeYaml.split('\n').length;
if (lines < MIN_ROW) return MIN_ROW;
if (lines > MAX_ROW) return MAX_ROW;
return lines;
}, [treeYaml]);

const handleTreeYamlChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const treeYaml = event.target.value;
setTreeYaml(treeYaml);
};

const handleSubmit = () => {
if (loading || !validForm) return;

const trees = parse(treeYaml);
setTreesValue(trees);
toggle();
Expand All @@ -60,11 +88,17 @@ function ModalDeletePerson({
onChange={handleTreeYamlChange}
rows={rows}
style={{ fontFamily: 'monospace' }}
invalid={!validForm}
/>
{yamlError !== '' && (
<FormFeedback>
<pre>{yamlError}</pre>
</FormFeedback>
)}
</FormGroup>
</ModalBody>
<ModalFooter>
<Button color="warning" disabled={!treeYaml} onClick={handleSubmit}>
<Button color="warning" disabled={!validForm} onClick={handleSubmit}>
Update!
</Button>
</ModalFooter>
Expand Down

0 comments on commit 8897c5c

Please sign in to comment.