diff --git a/src/App.tsx b/src/App.tsx index 0dbec40..f726c8e 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,12 +1,32 @@ +import { useState } from "react"; import "./App.css"; import ImageModerator from "./components/ImageModerator/ImageModerator"; +import Login from "./components/Login/Login"; function App() { - return ( - <> - - - ); + const [{ apiUrl, middlewareUrl, token }, setRemote] = useState({ + apiUrl: "", + middlewareUrl: "", + token: "", + }); + + if (token) { + return ( + <> + + + ); + } else { + return ( + <> + + + ); + } } export default App; diff --git a/src/components/ImageModerator/ImageModerator.tsx b/src/components/ImageModerator/ImageModerator.tsx index 88d597b..0bb477b 100644 --- a/src/components/ImageModerator/ImageModerator.tsx +++ b/src/components/ImageModerator/ImageModerator.tsx @@ -1,43 +1,32 @@ import Button from "../Button/Button"; import { useEffect, useState } from "react"; -// Let the user input the middleware URL, API url and app + secret -const MWURL = "http://localhost:8081"; -const APIURL = "http://localhost:8080"; -const app = "tester"; -const secret = "test"; +interface ImageModeratorProps { + token: string; + apiUrl: string; + middlewareUrl: string; +} -export default function ImageModerator() { +export default function ImageModerator({ + token, + apiUrl, + middlewareUrl, +}: ImageModeratorProps) { const acceptLabel = "Accept"; const discardLabel = "Discard"; - const endpoint = `${MWURL}/image`; - const [isLoading, setIsLoading] = useState(true); - const [imageSrc, setImageSrc] = useState(""); + const [imageData, setImageData] = useState({ url: "", tags: [] }); const [imageAlt, setImageAlt] = useState("No image"); - const [token, setToken] = useState(""); - - useEffect(() => { - fetch(`${APIURL}/login`, { - method: "POST", - body: JSON.stringify({ app, secret }), - headers: { "Content-type": "application/json" }, - }) - .then((response) => { - return response.json(); - }) - .then((body) => { - setToken(body.token); - }); - }, []); - - console.log(token); function acceptAction() { - fetch(`${APIURL}/images`, { + fetch(`${apiUrl}/images`, { method: "POST", - body: `{ "url": "${imageSrc}", "status": "available", "tags": [] }`, + body: JSON.stringify({ + url: imageData.url, + status: "available", + tags: imageData.tags, + }), headers: { "Content-type": "application/json", Authorization: `Bearer ${token}`, @@ -47,9 +36,13 @@ export default function ImageModerator() { } function discardAction() { - fetch(`${APIURL}/images`, { + fetch(`${apiUrl}/images`, { method: "POST", - body: `{ "url": "${imageSrc}", "status": "unavailable", "tags": [] }`, + body: JSON.stringify({ + url: imageData.url, + status: "unavailable", + tags: imageData.tags, + }), headers: { "Content-type": "application/json", Authorization: `Bearer ${token}`, @@ -59,8 +52,8 @@ export default function ImageModerator() { } const getNewImage = () => { - setImageSrc(""); - fetch(endpoint, { + setImageData({ url: "", tags: [] }); + fetch(`${middlewareUrl}/image`, { method: "GET", }) .then((response) => { @@ -68,8 +61,9 @@ export default function ImageModerator() { return response.json(); }) .then((data) => { - const imageUrl = data.url; - setImageSrc(imageUrl); + const url = data.url; + const tags = data.tags; + setImageData({ url, tags }); }) .catch((error) => { setImageAlt("Error"); @@ -96,7 +90,7 @@ export default function ImageModerator() { Loading... ) : ( {imageAlt} { + return response.json(); + }) + .then((body) => { + setRemote({ apiUrl, middlewareUrl, token: body.token }); + }); + } + + function handleChange( + e: { + target: { value: React.SetStateAction }; + }, + setter: React.Dispatch> + ): void { + setter(e.target.value); + } + useEffect(() => { + const savedRemote = localStorage.getItem("credentials"); + + if (savedRemote) { + const savedData = JSON.parse(savedRemote); + setApiUrl(savedData.apiUrl); + setMiddlewareUrl(savedData.middlewareUrl); + setApp(savedData.app); + setSecret(savedData.secret); + } + }, []); + + useEffect(() => { + if (secret) { + sendAction(); + } + }, [secret]); + + return ( +
+
+ + handleChange(e, setApiUrl)} + /> + + handleChange(e, setMiddlewareUrl)} + /> + + handleChange(e, setApp)} + /> + + handleChange(e, setSecret)} + /> + +
+
+ ); +}