diff --git a/src/components/EmployeesList.js b/src/components/EmployeesList.js new file mode 100644 index 0000000..427fd30 --- /dev/null +++ b/src/components/EmployeesList.js @@ -0,0 +1,33 @@ +import React from "react" +import { + useEffect, + useState +} from "react" + +export const EmployeeList = () => { + const [employees, setEmployees] = useState([]) + + useEffect( + () => { + + fetch("http://localhost:8088/employees") + /* updates application state from permanent state and converts the result to JSON */ + .then(res => res.json()) + /* passes the employees state variable to the .then function as a parameter and then calls that parameter in the assignCustomers function to change application state */ + .then((employees) => { + setEmployees(employees) + }) + }, + [] + ) + return( + <> + { + /* .map converting items to convert items from an array to HTML */ + employees.map( + (employeeObject) => { + return

{employeeObject.name}

+ } + ) + } ) +} \ No newline at end of file diff --git a/src/components/Repairs.js b/src/components/Repairs.js index 690cd73..99d0f4b 100644 --- a/src/components/Repairs.js +++ b/src/components/Repairs.js @@ -1,4 +1,5 @@ import { CustomerList } from "./customers/CustomerList" +import { EmployeeList } from "./EmployeesList" /* State is stored directly in the application file */ /* const [customers, assignCustomers] is called a deconstruction array */ @@ -9,8 +10,11 @@ export const Repairs = () => { /* JSX, returns whatever HTML code you want rendered to the DOM */ return ( <> -

Honey Rae's Repair Shop

+

Honey Rae's Repairs

+

Customer List

+

Employee List

+ ) } diff --git a/src/components/customers/CustomerList.js b/src/components/customers/CustomerList.js index d79ba7d..1a0c20d 100644 --- a/src/components/customers/CustomerList.js +++ b/src/components/customers/CustomerList.js @@ -1,4 +1,4 @@ -import { useEffect, useState } from "react" +/* import { useEffect, useState } from "react" export const CustomerList = () => { @@ -7,39 +7,79 @@ export const CustomerList = () => { /* assignCustomers holds an array of the assigned customers a dependancy/subset of customers. i.e. this is your state variable */ /* State is stored directly in the application file */ /* const [customers, assignCustomers] is called a deconstruction array */ - const [customers, setCustomers] = useState([]) +/* const [customers, setCustomers] = useState([]) /* Once the customers array is retrieved, it needs to end up inside of the state variable "customers" */ /* assignCustomers must be invoked to set the value of customers */ /* customerArray => { customers = customerArray } This is not allowed in React, even if it works in vanilla JS. */ - useEffect( +/* useEffect( () => { fetch("http://localhost:8088/customers") /* updates application state from permanent state and converts the result to JSON */ - .then(res => res.json()) + /* .then(res => res.json()) /* passes the customers state variable to the .then function as a parameter and then calls that parameter in the assignCustomers function to change application state */ - .then((customers) => { - setCustomers(customers) } - ) +// .then((customers) => { + // setCustomers(customers) } +/* ) }, [] - ) + ) /* JSX, returns whatever HTML code you want rendered to the DOM */ - return ( + /* return ( <> -

Honey Rae's Repair Shop

{ /* .map converting items to convert items from an array to HTML */ - customers.map( + /* customers.map( (customerObject) => { - return

(customerObject.name)

+ return

{customerObject.name}

} ) } ) -} +} + */ + +import React, { useEffect, useState } from "react" + +export const CustomerList = () => { + // useEffect runs when the state is set by setting it to an empty array + const [customers, setCustomers] = useState([]) + // totalCustomerMessage is the state variable holding an array of all the customers + const [totalCustomerMessage, updateMessage] = useState("") + + useEffect( + () => { + fetch("http://localhost:8088/customers") + .then(res => res.json()) + .then((data) => { + setCustomers(data) + }) + }, + [] + ) +// This useEffect runs only when the state of customers changes + useEffect( + () => { + updateMessage(`You have ${customers.length} customers.`) + }, + [customers] + ) + + return ( + <> +
{totalCustomerMessage}
+ { + customers.slice(0,5).map( + (customerObject) => { + return

{customerObject.name}

+ } + ) + } + + ) +}