diff --git a/src/Interfaces.tsx b/src/Interfaces.tsx index 3eebe9e..aecb54e 100644 --- a/src/Interfaces.tsx +++ b/src/Interfaces.tsx @@ -14,7 +14,7 @@ export interface UserData { } export interface UserInformationProps { - userData?: Partial; + userData: UserData; } export interface CompanyAttributes { @@ -48,56 +48,116 @@ export interface JobApplication { updated_at: string; } +export interface DashBoardDattaI{ + job_applications: [ null | + { + id: number; + position_title: string; + date_applied: string; + status: number; + notes: string; + job_description: string; + application_url: string; + created_at: string; + updated_at: string; + company_id: number; + user_id: number; + } + ], + contacts: [ null | + { + id: number; + first_name: string; + last_name: string; + email: string; + phone_number: string; + notes: string; + created_at: string; + updated_at: string; + user_id: number; + company_id: number; + } + ], + companies: [ null | + { + id: number; + user_id: number; + name: string; + website: string; + street_address: string; + city: string; + state: string; + zip_code: string; + notes: string; + created_at: string; + updated_at: string ; + } + ] +} + export interface ContactAttributes { + company: { name: string }; + first_name: string; + last_name: string; + } + export interface Contact { + id: string; + attributes: ContactAttributes; + } + export interface ContactData { + id: string; + type: string; + attributes: { + first_name: string; + last_name: string; + company_id: number; + email: string; + phone_number: string; + notes: string; + user_id: number; + company: { + id: number; + name: string; + website: string; + street_address: string; + city: string; + state: string; + zip_code: string; + notes: string; + }; + }; + } - export interface DashBoardDattaI{ - job_applications: [ null | - { - id: number; - position_title: string; - date_applied: string; - status: number; - notes: string; - job_description: string; - application_url: string; - created_at: string; - updated_at: string; - company_id: number; - user_id: number; - } - ], - contacts: [ null | + export interface FormInputData { + firstName: string; + lastName: string; + email: string; + phoneNumber: string; + companyId?: number | null; + notes: string; + }; - { - id: number; - first_name: string; - last_name: string; - email: string; - phone_number: string; - notes: string; - created_at: string; - updated_at: string; - user_id: number; - company_id: number; - } - ], - companies: [ null | + export interface NewContact { + first_name: string, + last_name: string, + email: string, + phone_number: string, + notes: string, + } - { - id: number; - user_id: number; - name: string; - website: string; - street_address: string; - city: string; - state: string; - zip_code: string; - notes: string; - created_at: string; - updated_at: string ; - } - ] + export interface DataCompile { + token?: string; + id?: number; + name?: string; + email?: string; + [key: string]: any; + } + export interface UserRegistrationData { + name: string, + email: string, + password: string, + passwordConfirmation: string } \ No newline at end of file diff --git a/src/apiCalls.tsx b/src/apiCalls.tsx index b1ccfb7..c38738d 100644 --- a/src/apiCalls.tsx +++ b/src/apiCalls.tsx @@ -1,5 +1,4 @@ -import { FormInputData } from "./components/contacts/NewContact" -import { NewContact } from "./components/contacts/NewContact" +import { UserRegistrationData, FormInputData, NewContact, Company } from "./Interfaces" const apiURL = process.env.REACT_APP_BACKEND_API_URL const backendURL = `${apiURL}api/v1/` /*-----------------------------------// GET //--------------------------------------*/ @@ -58,17 +57,7 @@ export const fetchApplicationsData = async (userId: number, token: string) => { } }; - -/*-----------------------------------// POST - Register New User //--------------------------------------*/ - -interface UserData { - name: string, - email: string, - password: string, - passwordConfirmation: string -} - -export const registerUser = async (userData: UserData): Promise => { +export const registerUser = async (userData: UserRegistrationData): Promise => { try { console.log(`backend ${backendURL}`) const response = await fetch(`${backendURL}users`, { @@ -104,103 +93,146 @@ export const fetchDashBoardData = async (userId: number, token: string | null) = 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, - }); - if (!response.ok) { throw new Error(`Failed to fetch applications: ${response.statusText}`); } - const result = await response.json(); - const formattedData = result.data.attributes.dashboard.weekly_summary - return formattedData; - } catch (error) { - throw error; - } } /*-----------------------------------// Index - Contacts //--------------------------------------*/ - export const fetchContacts = async (userId: number, token: string | null) => { - try { - const apiURL = process.env.REACT_APP_BACKEND_API_URL - const backendURL = `${apiURL}api/v1/` - const response = await fetch(`${backendURL}users/${userId}/contacts`, { - method: "GET", +export const fetchContacts = async (userId: number, token: string | null) => { + try { + const apiURL = process.env.REACT_APP_BACKEND_API_URL + const backendURL = `${apiURL}api/v1/` + const response = await fetch(`${backendURL}users/${userId}/contacts`, { + method: "GET", + headers: { + "Authorization": `Bearer ${token}`, + "Content-Type": "application/json" + }, + }); + if (!response.ok) { + throw new Error(`Failed to fetch contacts: ${response.statusText}`); + } + const result = await response.json(); + const formattedData = result.data + return formattedData; + } catch (error) { + throw error; + } +} + + /*-----------------------------------// Index - Companies //--------------------------------------*/ +export const fetchCompanies = async (userId: number | undefined, token: string | null) => { + try { + const apiURL = process.env.REACT_APP_BACKEND_API_URL + const backendURL = `${apiURL}api/v1/` + const response = await fetch(`${backendURL}users/${userId}/companies`, { headers: { - "Authorization": `Bearer ${token}`, - "Content-Type": "application/json" + Authorization: `Bearer ${token}`, + "Content-Type": "application/json", }, - }); - if (!response.ok) { - throw new Error(`Failed to fetch contacts: ${response.statusText}`); } - const result = await response.json(); - const formattedData = result.data - return formattedData; - } catch (error) { - throw error; + ); + + if (!response.ok) { + throw new Error("Failed to fetch companies"); } + + const result = await response.json(); + const companyList = result.data.map((company: Company) => ({ + id: company.id, + name: company.attributes.name, + })); + return companyList + } catch (error: any) { + console.error("Error fetching companies:", error.message); + throw error; } +} - /*-----------------------------------// Index - Companies //--------------------------------------*/ - export const fetchCompanies = async (userId: number | undefined, token: string | null) => { + + /*-----------------------------------// Post- Contact //--------------------------------------*/ + export const fetchNewContact = async (userId: number | undefined, token: string | null, formInputData: FormInputData, newContact: NewContact) => { try { - const apiURL = process.env.REACT_APP_BACKEND_API_URL - const backendURL = `${apiURL}api/v1/` - const response = await fetch(`${backendURL}users/${userId}/companies`, { + let url = `${backendURL}users/${userId}/contacts`; + if (formInputData.companyId) { + url = `${backendURL}users/${userId}/companies/${formInputData.companyId}/contacts`; + } + const response = await fetch(url, { + method: "POST", headers: { - Authorization: `Bearer ${token}`, + authorization: `Bearer ${token}`, "Content-Type": "application/json", }, - } - ); + body: JSON.stringify(newContact), + }); - if (!response.ok) { - throw new Error("Failed to fetch companies"); - } - - const result = await response.json(); - const companyList = result.data.map((company: any) => ({ - id: company.id, - name: company.attributes.name, - })); - return companyList - } catch (error: any) { - console.error("Error fetching companies:", error.message); - throw error; + if (!response.ok) { + const errorData = await response.json(); + console.log('response', errorData) + throw new Error(errorData.message || "Failed to add the contact"); } + } catch (error: any) { + console.error("Error adding contact:", error.message); + throw (error); } +} + /*-----------------------------------// Show - Contact //--------------------------------------*/ +export const fetchShowContact = async (userId: number | undefined, token: string | null, contactId: string | undefined) => { + try { + const response = await fetch( + `${backendURL}users/${userId}/contacts/${contactId}`, + { + method: "GET", + headers: { + Authorization: `Bearer ${token}`, + "Content-Type": "application/json", + }, + } + ); + if (!response.ok) { + throw new Error(`Failed to fetch contact: ${response.statusText}`); + } + const result = await response.json(); + return result + } catch (error: any) { + console.error("Error fetching contact:", error.message); + throw error; + } +} - /*-----------------------------------// Post- Contact //--------------------------------------*/ - export const fetchNewContact = async (userId: number | undefined, token: string | null, formInputData: FormInputData, newContact: NewContact) => { - try { - let url = `${backendURL}users/${userId}/contacts`; - if (formInputData.companyId) { - url = `${backendURL}users/${userId}/companies/${formInputData.companyId}/contacts`; - } - const response = await fetch(url, { - method: "POST", - headers: { - authorization: `Bearer ${token}`, - "Content-Type": "application/json", - }, - body: JSON.stringify(newContact), - }); - - if (!response.ok) { - const errorData = await response.json(); - console.log('response', errorData) - throw new Error(errorData.message || "Failed to add the contact"); + /*-----------------------------------// Company Contacts //--------------------------------------*/ +export const fetchCompanyContact = async (userId: number | undefined, token: string | null, companyId: string) => { + try { + const response = await fetch( + `${backendURL}users/${userId}/companies/${companyId}/contacts`, + { + method: "GET", + headers: { + Authorization: `Bearer ${token}`, + "Content-Type": "application/json", + }, } - } catch (error: any) { - console.error("Error adding contact:", error); - throw (error); + ); + if (!response.ok) { + throw new Error( + `Failed to fetch a companies contacts: ${response.statusText}` + ); } - } \ No newline at end of file + const result = await response.json(); + const contactsList = result.contacts.data; + return contactsList + } catch (error: any) { + console.error("Please try again later", error.message); + throw error; + } +} \ No newline at end of file diff --git a/src/components/contacts/Contacts.tsx b/src/components/contacts/Contacts.tsx index 1333a0e..0b37af5 100644 --- a/src/components/contacts/Contacts.tsx +++ b/src/components/contacts/Contacts.tsx @@ -1,38 +1,9 @@ import { useState, useEffect } from "react"; import { Link } from "react-router-dom"; import { useUserLoggedContext } from "../../context/UserLoggedContext"; -import { UserData } from "../../Interfaces"; +import { ContactData, UserInformationProps } from "../../Interfaces"; import { fetchContacts } from '../../apiCalls'; - -interface ContactData { - id: string; - type: string; - attributes: { - first_name: string; - last_name: string; - company_id: number; - email: string; - phone_number: string; - notes: string; - user_id: number; - company: { - id: number; - name: string; - website: string; - street_address: string; - city: string; - state: string; - zip_code: string; - notes: string; - }; - }; -} - -interface UserInformationProps { - userData: UserData; -} - function Contacts({ userData }: UserInformationProps) { const [contacts, setContacts] = useState([]); const [allContacts, setAllContacts] = useState([]); diff --git a/src/components/contacts/NewContact.tsx b/src/components/contacts/NewContact.tsx index 9ec7842..39a81ab 100644 --- a/src/components/contacts/NewContact.tsx +++ b/src/components/contacts/NewContact.tsx @@ -1,32 +1,10 @@ import { useState, useEffect } from "react"; import { useNavigate } from "react-router-dom"; import { useUserLoggedContext } from "../../context/UserLoggedContext"; -import { UserData } from '../../Interfaces'; +import { UserInformationProps, FormInputData } from '../../Interfaces'; import { fetchCompanies } from "../../apiCalls"; import { fetchNewContact } from "../../apiCalls"; - -export interface FormInputData { - firstName: string; - lastName: string; - email: string; - phoneNumber: string; - companyId?: number | null; - notes: string; -}; - -export interface NewContact { - first_name: string, - last_name: string, - email: string, - phone_number: string, - notes: string, -} - -interface UserInformationProps { - userData: UserData; -}; - const NewContact = ( {userData}: UserInformationProps ) => { const navigate = useNavigate(); diff --git a/src/components/contacts/ShowContact.tsx b/src/components/contacts/ShowContact.tsx index 79f9579..7ae8afa 100644 --- a/src/components/contacts/ShowContact.tsx +++ b/src/components/contacts/ShowContact.tsx @@ -2,41 +2,8 @@ import { useState, useEffect } from "react"; import { useParams } from "react-router-dom"; import { useUserLoggedContext } from "../../context/UserLoggedContext"; import { Link } from "react-router-dom"; - -interface ContactAttributes { - company: { name: string }; - first_name: string; - last_name: string; -} - -interface Contact { - id: string; - attributes: ContactAttributes; -} - -interface ContactData { - id: string; - type: string; - attributes: { - first_name: string; - last_name: string; - company_id: number; - email: string; - phone_number: string; - notes: string; - user_id: number; - company: { - id: number; - name: string; - website: string; - street_address: string; - city: string; - state: string; - zip_code: string; - notes: string; - }; - }; -} +import { Contact, ContactData } from "../../Interfaces" +import { fetchShowContact, fetchCompanyContact } from "../../apiCalls" function ShowContact() { const { token, userData } = useUserLoggedContext(); @@ -45,76 +12,42 @@ function ShowContact() { const [contact, setContact] = useState(null); const [otherContacts, setOtherContact] = useState([]); const [fetchError, setFetchError] = useState(null); + const userId = userData.user.data.id; useEffect(() => { - - const fetchShowContact = async () => { - + const contactFetcher = async () => { try { - const apiURL = process.env.REACT_APP_BACKEND_API_URL - const backendURL = `${apiURL}api/v1/` - const userId = userData.user.data.id; - const response = await fetch( - `${backendURL}users/${userId}/contacts/${contactId}`, - { - method: "GET", - headers: { - Authorization: `Bearer ${token}`, - "Content-Type": "application/json", - }, - } - ); - if (!response.ok) { - throw new Error(`Failed to fetch contact: ${response.statusText}`); - } - const data = await response.json(); - console.log("Contact Data: ", data); - - setContact(data.data); - - const companyId = data.data.attributes.company?.id; + const allData = await fetchShowContact(userId, token, contactId) + setContact(allData.data) + + const companyId = allData.data.attributes.company?.id; console.log("CompanyID: ", companyId); - if(!companyId) { console.log("No company for this contact"); setOtherContact([]); - return + return; } + try { + const companyContacts = await fetchCompanyContact(userId, token, companyId) - const companyContacts = await fetch( - `${backendURL}users/${userId}/companies/${companyId}/contacts`, - { - method: "GET", - headers: { - Authorization: `Bearer ${token}`, - "Content-Type": "application/json", - }, - } - ); if (companyContacts.status === 404) { setOtherContact([]); return; } - if (!companyContacts.ok) { - throw new Error( - `Failed to fetch a companies contacts: ${companyContacts.statusText}` - ); - } - const companyContactsData = await companyContacts.json(); - console.log("Company Contacts Data: ", companyContactsData); - const contactsList = companyContactsData.contacts.data; - console.log("Contacts List:", contactsList); - setOtherContact(contactsList); + setOtherContact(companyContacts); + } catch (error) { + setFetchError(`${(error as Error).message}. Please try again later.`); + } } catch (error) { setFetchError(`${(error as Error).message}. Please try again later.`); } - }; + } if (contactIdInt) { - fetchShowContact(); + contactFetcher(); } }, [contactId, token]); diff --git a/src/components/pages/userInformation.tsx b/src/components/pages/userInformation.tsx index 584a933..15f1f57 100644 --- a/src/components/pages/userInformation.tsx +++ b/src/components/pages/userInformation.tsx @@ -1,19 +1,8 @@ import { useState } from 'react'; -import { UserData } from '../../Interfaces' +import { UserInformationProps, DataCompile } from '../../Interfaces' import { updateUser } from '../../trackerApiCalls' -interface UserInformationProps { - userData: UserData; -} - function UserInformation({userData}: UserInformationProps) { - interface DataCompile { - token?: string; - id?: number; - name?: string; - email?: string; - [key: string]: any; - } const [name, setName] = useState(userData.user.data.attributes.name); const [email, setEmail] = useState(userData.user.data.attributes.email); const [password, setPassword] = useState(''); @@ -23,7 +12,6 @@ function UserInformation({userData}: UserInformationProps) { const submit = (e: React.FormEvent) => { e.preventDefault(); const compileData: DataCompile = { - // id: userData.user.data.id, id: userData.user.data.id ? Number(userData.user.data.id) : undefined, token: userData.token, name: name,