diff --git a/src/components/BubblePage.js b/src/components/BubblePage.js index 47a88f2291..62def3526d 100644 --- a/src/components/BubblePage.js +++ b/src/components/BubblePage.js @@ -13,9 +13,37 @@ const BubblePage = () => { }; const saveEdit = (editColor) => { + axiosWithAuth() + .put(`/api/colors/${editColor.id}`, editColor) + .then((res) => { + console.log(res) + + let index = colors.findIndex((color) => color.id === editColor.id); + colors[index] = editColor + setColors([ + ...colors + ]) + }) + .catch((err) => { + console.log(err) + }) }; + useEffect(() => { + fetchColorService().then((res) => { + setColors(res) + }) + }, []); + const deleteColor = (colorToDelete) => { + axiosWithAuth() + .delete(`/api/colors/${colorToDelete.id}`) + .then((res) => { + setColors(colors.filter(color => color.id !== colorToDelete.id)) + }) + .catch((err) => { + console.log(err) + }) }; return ( diff --git a/src/components/BubblePage.test.js b/src/components/BubblePage.test.js index eeaef0c197..a0d35d972b 100644 --- a/src/components/BubblePage.test.js +++ b/src/components/BubblePage.test.js @@ -3,11 +3,26 @@ import MutationObserver from 'mutationobserver-shim'; import { render, screen} from "@testing-library/react"; import BubblePage from './BubblePage'; +jest.mock('../services/fetchColorService'); + +const testColor = { + color: "blue", + code: { hex: "#7fffd4" }, + id: 1 +} test("Renders without errors", ()=> { - + render() }); test("Renders appropriate number of colors passed in through mock", async ()=> { //Keep in mind that our service is called on mount for this component. + fetchColorServices.mockResolvedValueOnce(testColor); + + render(); + const colors = screen.getAllByTestId("color"); + + await waitFor(() => { + expect(colors).toHaveLength(1); + }) }); \ No newline at end of file diff --git a/src/components/Color.test.js b/src/components/Color.test.js index def97c5555..42085d50a0 100644 --- a/src/components/Color.test.js +++ b/src/components/Color.test.js @@ -5,15 +5,38 @@ import { render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import Color from './Color'; +const testColor = { + color: "blue", + code: { hex: "#7fffd4" }, + id: 1 +} + test("Renders without errors with blank color passed into component", () => { + render(); }); test("Renders the color passed into component", () => { + render(); + const color = screen.queryAllByTestId("color"); + expect(color).toBeInTheDocument(); }); test("Executes handleDelete and toggleEdit property when the 'x' icon is clicked", () => { + const handleDelete = jest.fn(); + const toggleEdit = jest.fn(); + render() + const deleteKey = screen.getByTestId("delete") + userEvent.click(deleteKey) + expect(handleDelete).toBeCalled() + expect(toggleEdit).toBeCalled(); }); test("Executes setEditColor and toggleEdit property when color div is clicked", () => { - + const setEditColor = jest.fn(); + const toggleEdit = jest.fn(); + render() + const colorKey = screen.getByTestId("color") + userEvent.click(colorKey) + expect(setEditColor).toBeCalled() + expect(toggleEdit).toBeCalled(); }); \ No newline at end of file diff --git a/src/components/ColorList.test.js b/src/components/ColorList.test.js index 115b169331..d7b1289053 100644 --- a/src/components/ColorList.test.js +++ b/src/components/ColorList.test.js @@ -4,11 +4,24 @@ import MutationObserver from 'mutationobserver-shim'; import { render, screen} from "@testing-library/react"; import ColorList from './ColorList'; +const testColor = { + color: "blue", + code: { hex: "#7fffd4" }, + id: 1 +} + test("Renders an empty list of colors without errors", () => { + render() }); test("Renders a list of colors without errors", () => { + render() }); test("Renders the EditForm when editing = true and does not render EditForm when editing = false", () => { + const toggleEdit = jest.fn() + render(); + let editing = screen.queryByTestId("color"); + userEvent.click(editing); + expect(toggleEdit).toBeCalled(); }); diff --git a/src/components/Login.js b/src/components/Login.js index 8df9390633..f72d9fd80d 100644 --- a/src/components/Login.js +++ b/src/components/Login.js @@ -1,11 +1,47 @@ -import React from "react"; +import React, {useState} from "react"; +import { useHistory } from "react-router-dom"; +import axiosWithAuth from "../helpers/axiosWithAuth"; + +const initialValues = { + username: '', + password: '' +} const Login = () => { // make a post request to retrieve a token from the api // when you have handled the token, navigate to the BubblePage route + const { push } = useHistory + const [formValues, setFormValues] = useState(initialValues) + const [error, setError] = useState() + + const handleChange = (e) => { + setFormValues({ + ...formValues, + [e.target.name]: e.target.value + }) + } + + const handleSubmit = e => { + e.preventDefault(); + if (formValues.username !== 'Lambda' || formValues.password !== 'School') { + setError('Username or Password incorrect') + } + + axiosWithAuth() + .post('/api/login', formValues) + .then((res) => { + console.log("Axios Login Post", res) + localStorage.setItem('token', res.data.payload) + push('/bubblepage') + }) + .catch((err) => { + console.log({ err }) + }) + } + - const error = ""; - //replace with error state + + return (
@@ -13,6 +49,34 @@ const Login = () => {

Build login form here

+
+
+
+

+ +
+
+ + + +
+

{error}

diff --git a/src/components/PrivateRoute.js b/src/components/PrivateRoute.js index e718313bdf..a93cbb9045 100644 --- a/src/components/PrivateRoute.js +++ b/src/components/PrivateRoute.js @@ -1,2 +1,17 @@ //Task List: -//1. Build a PrivateRoute component that redirects if user is not logged in \ No newline at end of file +//1. Build a PrivateRoute component that redirects if user is not logged in + +import React from 'react'; +import { Route, Redirect } from 'react-router-dom'; + +const PrivateRoute = ({component: Component, ...rest}) => { + return { + if (localStorage.getItem("token")) { + return() + } else { + return + } + }} /> +} + +export default PrivateRoute; \ No newline at end of file diff --git a/src/helpers/axiosWithAuth.js b/src/helpers/axiosWithAuth.js index 1da418ff86..29c07554bc 100644 --- a/src/helpers/axiosWithAuth.js +++ b/src/helpers/axiosWithAuth.js @@ -1,4 +1,16 @@ import axios from "axios"; //Task List: -//Build and export a function used to send in our authorization token \ No newline at end of file +//Build and export a function used to send in our authorization token +const axiosWithAuth = () => { + const token = localStorage.getItem('token'); + + return axios.create({ + headers: { + Authorization: token + }, + baseURL: 'http://localhost:5000' + }) +} + +export default axiosWithAuth; \ No newline at end of file diff --git a/src/services/fetchColorService.js b/src/services/fetchColorService.js index a914deb477..e6c8f5385d 100644 --- a/src/services/fetchColorService.js +++ b/src/services/fetchColorService.js @@ -1,7 +1,15 @@ import axiosWithAuth from '../helpers/axiosWithAuth'; const fetchColorService = () => { - + return axiosWithAuth() + .get('/api/colors') + .then((res) => { + console.log("fetchColorService ", res); + return (res.data) + }) + .catch((err) => { + console.log(err); + }) } export default fetchColorService; \ No newline at end of file