Skip to content

Commit

Permalink
feat: Exercise: Refactor dispatch methods to meaningful abstractions …
Browse files Browse the repository at this point in the history
…inside the context
  • Loading branch information
Param-Harrison committed Apr 19, 2020
1 parent 17a9522 commit 3efe261
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/components/add-contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import ContactForm from "./contact-form";
import { useGlobalStore } from "./global-state";

const AddContactForm = (props) => {
const { dispatch } = useGlobalStore();
const { addContact } = useGlobalStore();

const onSubmit = (submitData) => {
dispatch({ type: "ADD_CONTACT", payload: { ...submitData } });
addContact({ ...submitData });
};

return (
<ContactForm title={"Add new contact"} onSubmit={onSubmit} {...props} />
);
};

const AddContact = ({ dispatch }) => {
const AddContact = () => {
const [isFormActive, setFormActive] = useState(false);

return (
Expand Down
4 changes: 2 additions & 2 deletions src/components/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import EditContact from "./edit-contact";
import { useGlobalStore } from "./global-state";

const Contact = (props) => {
const { dispatch } = useGlobalStore();
const { removeContact } = useGlobalStore();
const { name, email, phone, id } = props;
const [isFormActive, setFormActive] = useState(false);

Expand All @@ -16,7 +16,7 @@ const Contact = (props) => {
"Are you sure, you want to remove the contact?"
);
if (confirm) {
dispatch({ type: "REMOVE_CONTACT", payload: { id } });
removeContact(id);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/components/edit-contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import ContactForm from "./contact-form";
import { useGlobalStore } from "./global-state";

const EditContact = ({ id, ...props }) => {
const { dispatch } = useGlobalStore();
const { editContact } = useGlobalStore();

const onSubmit = (submitData) => {
dispatch({ type: "EDIT_CONTACT", payload: { id, ...submitData } });
editContact({ id, ...submitData });
};

return <ContactForm title={"Edit contact"} onSubmit={onSubmit} {...props} />;
Expand Down
16 changes: 15 additions & 1 deletion src/components/global-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,22 @@ export const useGlobalStore = () => {
const GlobalProvider = ({ children }) => {
const [contacts, dispatch] = useContacts();

const removeContact = (id) => {
dispatch({ type: "REMOVE_CONTACT", payload: { id } });
};

const editContact = (contact) => {
dispatch({ type: "EDIT_CONTACT", payload: contact });
};

const addContact = (contact) => {
dispatch({ type: "ADD_CONTACT", payload: contact });
};

return (
<GlobalContext.Provider value={{ contacts, dispatch }}>
<GlobalContext.Provider
value={{ contacts, removeContact, editContact, addContact }}
>
{children}
</GlobalContext.Provider>
);
Expand Down

0 comments on commit 3efe261

Please sign in to comment.