Skip to content

Commit

Permalink
Merge pull request #15 from rafgugi/edit-tree-yaml
Browse files Browse the repository at this point in the history
feat: enable edit tree yaml using modal
  • Loading branch information
rafgugi committed Jul 29, 2023
2 parents 01a38bb + 8897c5c commit 016e75b
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 92 deletions.
57 changes: 33 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"lodash": "^4.17.21",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"reactstrap": "^9.1.9"
"reactstrap": "^9.1.9",
"yaml": "^2.3.1"
},
"scripts": {
"start": "craco start",
Expand Down
32 changes: 28 additions & 4 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Button, Container, Form, FormGroup, Input, Label } from 'reactstrap';
import { useMemo, useState } from 'react';
import { stringify } from 'yaml';
import { Person } from '../family.interface';
import AppContext from './AppContext';
import Family from './Family';
import ModalAddChild from './ModalAddChild';
import ModalAddSpouse from './ModalAddSpouse';
import ModalDeletePerson from './ModalDeletePerson';
import ModalAddTree from './ModalAddTree';
import ModalDeletePerson from './ModalDeletePerson';
import ModalEditYaml from './ModalEditYaml';
import { deletePerson, enrichTreeData, treesToRecord } from '../family.util';
import { useCache } from '../useCache';

Expand All @@ -24,6 +26,7 @@ function App(props: AppProps) {

const [modalPerson, setModalPerson] = useState(null as Person | null);
const [modalSpouse, setModalSpouse] = useState(null as Person | null);
const [treeYaml, setTreeYaml] = useState('');

const [showModalAddTree, setShowModalAddTree] = useState(false);
const toggleModalAddTree = () => setShowModalAddTree(!showModalAddTree);
Expand All @@ -45,6 +48,13 @@ function App(props: AppProps) {
setShowModalAddSpouse(true);
};

const [showModalEditYaml, setShowModalEditYaml] = useState(false);
const toggleModalEditYaml = () => setShowModalEditYaml(!showModalEditYaml);
const openModalEditYaml = () => {
setTreeYaml(stringify(trees));
setShowModalEditYaml(true);
};

const [showModalDeletePerson, setShowModalDeletePerson] = useState(false);
const toggleModalDeletePerson = () =>
setShowModalDeletePerson(!showModalDeletePerson);
Expand All @@ -55,7 +65,7 @@ function App(props: AppProps) {

const treeMap = useMemo(() => treesToRecord(trees), [trees]);

const setTreeValue = (person: Person) => {
const upsertPerson = (person: Person) => {
const personData: Record<string, Person> = { [person.id]: person };
setTreesValue(enrichTreeData(trees, personData));
};
Expand All @@ -70,9 +80,10 @@ function App(props: AppProps) {
split,
editMode,
hidePersonCode,
setTreeValue,
deleteTreePerson,
treeMap,
setTreesValue,
upsertPerson,
deletePerson: deleteTreePerson,
}}
>
<Container className="d-print-none" fluid="sm">
Expand Down Expand Up @@ -120,6 +131,13 @@ function App(props: AppProps) {
<Button size="sm" onClick={() => openModalAddSpouse(trees[0])}>
Add spouse
</Button>{' '}
<Button
size="sm"
onClick={() => openModalEditYaml()}
color="warning"
>
Edit tree
</Button>{' '}
<Button
size="sm"
onClick={() => openModalDeletePerson()}
Expand Down Expand Up @@ -150,6 +168,12 @@ function App(props: AppProps) {
person={modalPerson}
setPerson={setModalPerson}
/>
<ModalEditYaml
isOpen={showModalEditYaml}
toggle={toggleModalEditYaml}
treeYaml={treeYaml}
setTreeYaml={setTreeYaml}
/>
<ModalDeletePerson
isOpen={showModalDeletePerson}
toggle={toggleModalDeletePerson}
Expand Down
10 changes: 6 additions & 4 deletions src/components/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ interface AppContextValue {
split: boolean;
editMode: boolean;
hidePersonCode: boolean;
setTreeValue: (p: Person) => void;
deleteTreePerson: (p: Person) => void;
setTreesValue: (ps: Person[]) => void;
upsertPerson: (p: Person) => void;
deletePerson: (p: Person) => void;
treeMap: Record<string, Person>;
}

const AppContext = createContext<AppContextValue>({
split: false,
editMode: false,
hidePersonCode: false,
setTreeValue: () => {},
deleteTreePerson: () => {},
setTreesValue: () => {},
upsertPerson: () => {},
deletePerson: () => {},
treeMap: {},
});

Expand Down
4 changes: 2 additions & 2 deletions src/components/FamilyGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ function FamilyRows({ person, ...props }: PersonRowProps) {
}

function PersonRow({ person }: PersonRowProps) {
const { editMode, hidePersonCode, setTreeValue } = useContext(AppContext);
const { editMode, hidePersonCode, upsertPerson } = useContext(AppContext);
const name = person.name || person.id;

const updatePerson = function (e: ChangeEvent<any>, key: string) {
setTreeValue({ ...person, [key]: e.target.value });
upsertPerson({ ...person, [key]: e.target.value });
};

let inputClass = 'd-none';
Expand Down
16 changes: 8 additions & 8 deletions src/components/ModalAddChild.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {
ModalFooter,
ModalHeader,
} from 'reactstrap';
import { Dispatch, useContext, useState } from 'react';
import { useContext, useState } from 'react';
import { Marriage, Person } from '../family.interface';
import AppContext from './AppContext';

interface ModalAddChildProps {
person: Person | null;
setPerson: Dispatch<any>;
setPerson: (p: Person | null) => void;
spouse: Person | null;
setSpouse: Dispatch<any>;
setSpouse: (p: Person | null) => void;
isOpen: boolean;
toggle: () => void;
}
Expand All @@ -30,7 +30,7 @@ function ModalAddChild({
isOpen,
toggle,
}: ModalAddChildProps) {
const { treeMap, setTreeValue } = useContext(AppContext);
const { treeMap, upsertPerson } = useContext(AppContext);
const [child, setChild] = useState('');
const [childError, setChildError] = useState('');

Expand Down 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 All @@ -81,7 +81,7 @@ function ModalAddChild({
};
marriage.children.push(childPerson);

setTreeValue(person);
upsertPerson(person);
setPerson(null);
setSpouse(null);
setChild('');
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
14 changes: 7 additions & 7 deletions src/components/ModalAddSpouse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {
ModalFooter,
ModalHeader,
} from 'reactstrap';
import { Dispatch, useContext, useState } from 'react';
import { useContext, useState } from 'react';
import { Marriage, Person } from '../family.interface';
import AppContext from './AppContext';

interface ModalAddSpouseProps {
person: Person | null;
setPerson: Dispatch<any>;
setPerson: (p: Person | null) => void;
isOpen: boolean;
toggle: () => void;
}
Expand All @@ -26,7 +26,7 @@ function ModalAddSpouse({
isOpen,
toggle,
}: ModalAddSpouseProps) {
const { treeMap, setTreeValue } = useContext(AppContext);
const { treeMap, upsertPerson } = useContext(AppContext);
const [spouse, setSpouse] = useState('');
const [spouseError, setSpouseError] = useState('');

Expand All @@ -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 All @@ -63,7 +63,7 @@ function ModalAddSpouse({
children: [],
});

setTreeValue(person);
upsertPerson(person);
setPerson(null);
setSpouse('');
toggle();
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
10 changes: 5 additions & 5 deletions src/components/ModalAddTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface ModalAddTreeProps {
}

function ModalAddTree({ isOpen, toggle }: ModalAddTreeProps) {
const { treeMap, setTreeValue } = useContext(AppContext);
const { treeMap, upsertPerson } = useContext(AppContext);
const [child, setChild] = useState('');
const [childError, setChildError] = useState('');

Expand All @@ -33,18 +33,18 @@ 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,
code: '',
marriages: [] as Marriage[],
};

setTreeValue(tree);
upsertPerson(tree);
setChild('');
toggle();
};
Expand All @@ -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
Loading

0 comments on commit 016e75b

Please sign in to comment.