Skip to content

Commit

Permalink
Merge pull request #7 from faginr/patrick_branch
Browse files Browse the repository at this point in the history
added relation between contacts and applications
  • Loading branch information
Patrick2303 authored Jan 31, 2023
2 parents 48f1671 + ea0cd80 commit e46be7c
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 17 deletions.
42 changes: 35 additions & 7 deletions datastore-backend/routes/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function checkAcceptHeader (req, res, next) {
* if not, send an error message.
*/
function checkRequestBody (req, res, next) {
const allKeys = {"last_name": '', "first_name": '', "email": '', "phone": '', "notes": '', "contact_at": ''};
const allKeys = {"last_name": '', "first_name": '', "email": '', "phone": '', "notes": '', "contact_at_id": ''};
const requiredKeys = ["last_name", "first_name"];
let keyError = false;

Expand Down Expand Up @@ -113,9 +113,16 @@ function checkIdExists (req, res, next) {

/* ------------- Begin Lodging Model Functions ------------- */

function post_contact(last_name, first_name, email, phone, notes, contact_at) {
function post_contact(last_name, first_name, email, phone, notes, contact_at_id) {
var key = datastore.key(CONTACT);
const new_contact = { "last_name": last_name, "first_name": first_name, "email": email, "phone": phone, "notes": notes, "contact_at": contact_at };
const new_contact = {
"last_name": last_name,
"first_name": first_name,
"email": email,
"phone": phone,
"notes": notes,
"contact_at_id": contact_at_id
};
// console.log(new_contact)
return datastore.save({ "key": key, "data": new_contact }).then(() => { return key });
}
Expand Down Expand Up @@ -163,9 +170,16 @@ function get_contact(id) {
}


function put_contact(id, last_name, first_name, email, phone, notes, contact_at ) {
function put_contact(id, last_name, first_name, email, phone, notes, contact_at_id ) {
const key = datastore.key([CONTACT, parseInt(id, 10)]);
const contact = { "last_name": last_name, "first_name": first_name, "email": email, "phone": phone, "notes": notes, "contact_at": contact_at };
const contact = {
"last_name": last_name,
"first_name": first_name,
"email": email,
"phone": phone,
"notes": notes,
"contact_at_id": contact_at_id
};
return datastore.save({ "key": key, "data": contact });
}

Expand Down Expand Up @@ -194,7 +208,13 @@ router.get('/', checkAcceptHeader, function (req, res) {

router.post('/', checkContentTypeHeader, checkRequestBody, function (req, res) {
console.log("Post request received!");
post_contact(req.body.last_name, req.body.first_name, req.body.email, req.body.phone, req.body.notes, req.body.contact_at)
post_contact(
req.body.last_name,
req.body.first_name,
req.body.email, req.body.phone,
req.body.notes,
req.body.contact_at_id
)
.then(key => { res.status(201).send('{ "id": ' + key.id + ' }') })
.catch(error => {
console.error(error);
Expand All @@ -205,7 +225,15 @@ router.post('/', checkContentTypeHeader, checkRequestBody, function (req, res) {

router.put('/:id', checkContentTypeHeader, checkRequestBody, function (req, res) {
console.log("Put request received!");
put_contact(req.params.id, req.body.last_name, req.body.first_name, req.body.email, req.body.phone, req.body.notes)
put_contact(
req.params.id,
req.body.last_name,
req.body.first_name,
req.body.email,
req.body.phone,
req.body.notes,
req.body.contact_at_id
)
.then(res.status(200).end())
.catch(error => {
console.error(error);
Expand Down
4 changes: 2 additions & 2 deletions react-frontend/src/components/Contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { MdDeleteForever, MdEdit } from 'react-icons/md';
function Contact({ contact, onDelete, onEdit }) {
return (
<tr>
<td>{contact.last_name}</td>
<td>{contact.first_name}</td>
<td>{contact.last_name}</td>
<td>{contact.email}</td>
<td>{contact.phone}</td>
<td>{contact.notes}</td>
<td>{contact.contact_at}</td>
<td><a href={contact.contact_at_link}>{contact.contact_at_name}</a></td>
<td><MdEdit onClick={() => onEdit(contact)} /></td>
<td><MdDeleteForever onClick={() => onDelete(contact.id)} /></td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion react-frontend/src/components/ContactList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ function ContactList({ contacts, onDelete, onEdit, sorting }) {
<table id="contacts">
<thead>
<tr>
<th onClick={() => sorting("last_name")} >Contact Last Name <br />(click to sort)</th>
<th onClick={() => sorting("first_name")} >Contact First Name <br />(click to sort)</th>
<th onClick={() => sorting("last_name")} >Contact Last Name <br />(click to sort)</th>
<th>Contact Email</th>
<th>Contact Phone</th>
<th>Notes about the contact</th>
Expand Down
8 changes: 4 additions & 4 deletions react-frontend/src/pages/AddContactPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const AddContactPage = () => {
const [email, setEmail] = useState('');
const [phone, setPhone] = useState('');
const [notes, setNotes] = useState('');
const [contact_at, setContactAt] = useState('');
const [contact_at_id, setContactAt] = useState('');

const [apps, setApps] = useState([]);

Expand All @@ -27,7 +27,7 @@ export const AddContactPage = () => {
const addContact = async (e) => {
e.preventDefault();

const newContact = { last_name, first_name, email, phone, notes, contact_at };
const newContact = { last_name, first_name, email, phone, notes, contact_at_id };
console.log(newContact)

const response = await fetch('/contacts', {
Expand Down Expand Up @@ -94,8 +94,8 @@ export const AddContactPage = () => {

<option>Please choose one option</option>
{apps.map((option, index) => {
return <option key={index} value={option.name}>
{option.name}
return <option key={index} value={option.id}>
{option.title}
</option>
})}

Expand Down
19 changes: 19 additions & 0 deletions react-frontend/src/pages/ContactPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function ContactPage({ setContactToEdit }) {

const [contacts, setContacts] = useState([]);
const [order, setOrder] = useState("ASC");
const [apps, setApps] = useState([]);

// sorts the contact table by clicking on the name of the column
const sorting = (col) => {
Expand Down Expand Up @@ -63,10 +64,28 @@ function ContactPage({ setContactToEdit }) {
setContacts(data);
};

const getApps = async () => {
const response = await fetch('/applications');
const data = await response.json();
setApps(data);
};

useEffect(() => {
loadContacts();
getApps();
}, []);


// iterate over contacts and applications, add name and link of application to contact
for (let contact of contacts) {
for (let app of apps) {
if (contact.contact_at_id === app.id) {
contact.contact_at_name = app.title;
contact.contact_at_link = app.link;
}
}
}

return (
<>
<h1>Contact Page</h1>
Expand Down
40 changes: 37 additions & 3 deletions react-frontend/src/pages/EditContactPage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import { useNavigate } from "react-router-dom";

export const EditContactPage = ({ contactToEdit }) => {
Expand All @@ -8,13 +8,16 @@ export const EditContactPage = ({ contactToEdit }) => {
const [email, setEmail] = useState(contactToEdit.email);
const [phone, setPhone] = useState(contactToEdit.phone);
const [notes, setNotes] = useState(contactToEdit.notes);
const [contact_at_id, setContactAt] = useState(contactToEdit.contact_at_id);

const [apps, setApps] = useState([]);

const navigate = useNavigate();

const editContact = async (e) => {
e.preventDefault();

const editedContact = { last_name, first_name, email, phone, notes };
const editedContact = { last_name, first_name, email, phone, notes, contact_at_id };

const response = await fetch(`/contacts/${contactToEdit.id}`, {
method: 'PUT',
Expand All @@ -32,6 +35,25 @@ export const EditContactPage = ({ contactToEdit }) => {

navigate(-1); // goes back to Contact Page
};

const getApps = async () => {
const response = await fetch('/applications');
const data = await response.json();
setApps(data);
};

useEffect(() => {
getApps();
}, []);

let contact_at_name;

// iterate over applications and add name of application to the contact
for (let app of apps) {
if (contactToEdit.contact_at_id === app.id) {
contact_at_name = app.title;
}
}

return (
<div>
Expand All @@ -58,8 +80,20 @@ export const EditContactPage = ({ contactToEdit }) => {
type="text"
value={notes}
onChange={e => setNotes(e.target.value)} />

<select onChange={e => setContactAt(e.target.value)}>

<option>{contact_at_name}</option>
{apps.map((option, index) => {
return <option key={index} value={option.id}>
{option.title}
</option>
})}

</select>

<p>
<input type="submit" value="Submit" />
<input type="submit" value="Submit Changes" />
<> </>
<input type="button" value="Cancel" onClick={() => navigate(-1)} />
</p>
Expand Down

0 comments on commit e46be7c

Please sign in to comment.