Skip to content

Commit

Permalink
refactor: use context for modals
Browse files Browse the repository at this point in the history
  • Loading branch information
rafgugi committed Jul 6, 2023
1 parent b032574 commit e760601
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 32 deletions.
17 changes: 9 additions & 8 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function App(props: AppProps) {
setShowModalDeletePerson(true);
};

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

const setTreeValue = function (person: Person) {
const personData: Record<string, Person> = { [person.id]: person };
Expand All @@ -57,7 +57,14 @@ function App(props: AppProps) {

return (
<AppContext.Provider
value={{ split, editMode, hidePersonCode, setTreeValue }}
value={{
split,
editMode,
hidePersonCode,
setTreeValue,
deleteTreePerson,
treeMap,
}}
>
<Container fluid="sm">
<Form className="d-print-none">
Expand Down Expand Up @@ -116,28 +123,22 @@ function App(props: AppProps) {
<ModalAddChild
isOpen={showModalChild}
toggle={toggleModalChild}
record={record}
person={modalPerson}
setPerson={setModalPerson}
spouse={modalSpouse}
setSpouse={setModalSpouse}
setTreeValue={setTreeValue}
/>
<ModalAddSpouse
isOpen={showModalAddSpouse}
toggle={toggleModalAddSpouse}
record={record}
person={modalPerson}
setPerson={setModalPerson}
setTreeValue={setTreeValue}
/>
<ModalDeletePerson
isOpen={showModalDeletePerson}
toggle={toggleModalDeletePerson}
record={record}
person={modalPerson}
setPerson={setModalPerson}
deleteTreePerson={deleteTreePerson}
/>
</AppContext.Provider>
);
Expand Down
4 changes: 4 additions & 0 deletions src/components/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ interface AppContextValue {
editMode: boolean;
hidePersonCode: boolean;
setTreeValue: (p: Person) => void;
deleteTreePerson: (p: Person) => void;
treeMap: Record<string, Person>;
}

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

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

interface ModalAddChildProps {
person: Person | null;
Expand All @@ -19,8 +20,6 @@ interface ModalAddChildProps {
setSpouse: Dispatch<any>;
isOpen: boolean;
toggle: () => void;
record: Record<string, Person>;
setTreeValue: (p: Person) => void;
}

function ModalAddChild({
Expand All @@ -30,18 +29,17 @@ function ModalAddChild({
setSpouse,
isOpen,
toggle,
record,
setTreeValue,
}: ModalAddChildProps) {
const { treeMap, setTreeValue } = useContext(AppContext);
const [child, setChild] = useState('');
const [childError, setChildError] = useState('');

const marriedPeople = Object.values(record).filter(
const marriedPeople = Object.values(treeMap).filter(
person => person.marriages.length > 0
);

const handlePersonChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const person = record[event.target.value];
const person = treeMap[event.target.value];
setPerson(person || null);
setSpouse(null);
if (person.marriages.length === 1) {
Expand All @@ -51,7 +49,7 @@ function ModalAddChild({
};

const handleSpouseChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const spouse = record[event.target.value];
const spouse = treeMap[event.target.value];
setSpouse(spouse || null);
setChild('');
};
Expand All @@ -61,7 +59,7 @@ function ModalAddChild({
setChild(value);

setChildError('');
if (Object.keys(record).includes(value)) {
if (Object.keys(treeMap).includes(value)) {
setChildError(value + ' is already taken');
}
};
Expand Down
14 changes: 6 additions & 8 deletions src/components/ModalAddSpouse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,31 @@ import {
ModalFooter,
ModalHeader,
} from 'reactstrap';
import { Dispatch, useState } from 'react';
import { Dispatch, useContext, useState } from 'react';
import { Marriage, Person } from '../family.interface';
import AppContext from './AppContext';

interface ModalAddSpouseProps {
person: Person | null;
setPerson: Dispatch<any>;
isOpen: boolean;
toggle: () => void;
record: Record<string, Person>;
setTreeValue: (p: Person) => void;
}

function ModalAddSpouse({
person,
setPerson,
isOpen,
toggle,
record,
setTreeValue,
}: ModalAddSpouseProps) {
const { treeMap, setTreeValue } = useContext(AppContext);
const [spouse, setSpouse] = useState('');
const [spouseError, setSpouseError] = useState('');

const people = Object.values(record);
const people = Object.values(treeMap);

const handlePersonChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const person = record[event.target.value];
const person = treeMap[event.target.value];
setPerson(person || null);
setSpouse('');
};
Expand All @@ -45,7 +43,7 @@ function ModalAddSpouse({
setSpouse(event.target.value);

setSpouseError('');
if (Object.keys(record).includes(value)) {
if (Object.keys(treeMap).includes(value)) {
setSpouseError(value + ' is already taken');
}
};
Expand Down
12 changes: 5 additions & 7 deletions src/components/ModalDeletePerson.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,28 @@ import {
ModalFooter,
ModalHeader,
} from 'reactstrap';
import { Dispatch } from 'react';
import { Dispatch, useContext } from 'react';
import { Person } from '../family.interface';
import AppContext from './AppContext';

interface ModalDeletePersonProps {
person: Person | null;
setPerson: Dispatch<any>;
isOpen: boolean;
toggle: () => void;
record: Record<string, Person>;
deleteTreePerson: (person: Person) => void;
}

function ModalDeletePerson({
person,
setPerson,
isOpen,
toggle,
record,
deleteTreePerson,
}: ModalDeletePersonProps) {
const people = Object.values(record);
const { treeMap, deleteTreePerson } = useContext(AppContext);
const people = Object.values(treeMap);

const handlePersonChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const person = record[event.target.value];
const person = treeMap[event.target.value];
setPerson(person || null);
};

Expand Down

0 comments on commit e760601

Please sign in to comment.