implemented basic UI with hardcoded urls and credentials

This commit is contained in:
Alie 2024-04-21 14:11:55 +02:00
parent 940d590249
commit f753684abe
5 changed files with 119 additions and 56 deletions

View File

@ -47,7 +47,7 @@ services:
develop:
watch:
- action: rebuild
path: src/
path: .
ignore:
- node_modules/

View File

@ -2,7 +2,7 @@ db = new Mongo().getDB("bot");
db.createCollection('authorizations');
db.authorization.insert([
db.authorizations.insert([
{
app: "tester",
secret: "test"

View File

@ -1,17 +1,12 @@
import './App.css'
import ImageModerator, { ImageModeratorProps } from './components/ImageModerator/ImageModerator'
const imageModeratorProps: ImageModeratorProps = {
acceptLabel: "Accept",
discardLabel: "Discard"
}
import "./App.css";
import ImageModerator from "./components/ImageModerator/ImageModerator";
function App() {
return (
<>
<ImageModerator {...imageModeratorProps} />
<ImageModerator />
</>
)
);
}
export default App
export default App;

View File

@ -1,50 +1,121 @@
import Button from "../Button/Button";
import { useEffect, useState } from "react";
export interface ImageModeratorProps {
acceptLabel: string;
discardLabel: string;
}
// 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";
export default function ImageModerator({ acceptLabel, discardLabel }: ImageModeratorProps) {
const endpoint = "http://fe-middleware:8081/image";
export default function ImageModerator() {
const acceptLabel = "Accept";
const discardLabel = "Discard";
const [isLoading, setIsLoading] = useState(true);
const [imageSrc, setImageSrc] = useState("");
const [imageAlt, setImageAlt] = useState("No image");
const endpoint = `${MWURL}/image`;
const getNewImage = () => {
setImageSrc("");
setIsLoading(true);
fetch(endpoint, {
method: "GET",
})
.then(response => {
if (!response.ok) throw new Error("Response was not ok");
return response.json();
})
.then(data => {
const imageUrl = data.post[0].file_url;
setImageSrc(imageUrl);
setImageAlt(imageUrl);
})
.catch(error => {
setImageAlt("Error");
console.error(error);
}).finally(() => {
setIsLoading(false);
});
}
const [isLoading, setIsLoading] = useState(true);
const [imageSrc, setImageSrc] = useState("");
const [imageAlt, setImageAlt] = useState("No image");
const [token, setToken] = useState("");
useEffect(getNewImage, []);
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);
});
}, []);
return <div style={{
display: "grid", gridTemplateColumns: "1fr 1fr",
gridTemplateRows: "1fr 50px", gridTemplateAreas: "'. .' '. .'", width: "100vw", height: "100vh"
}}>
{isLoading ? <span>Loading...</span> :
<img src={imageSrc} alt={imageAlt} style={{ width: "100%", height: "100%", gridColumnStart: 1, gridColumnEnd: 3, display: "block", objectFit: "contain" }} />}
<Button label={acceptLabel} />
<Button label={discardLabel} />
console.log(token);
function acceptAction() {
fetch(`${APIURL}/images`, {
method: "POST",
body: `{ "url": "${imageSrc}", "status": "available", "tags": [] }`,
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${token}`,
},
});
setIsLoading(true);
}
function discardAction() {
fetch(`${APIURL}/images`, {
method: "POST",
body: `{ "url": "${imageSrc}", "status": "unavailable", "tags": [] }`,
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${token}`,
},
});
setIsLoading(true);
}
const getNewImage = () => {
setImageSrc("");
fetch(endpoint, {
method: "GET",
})
.then((response) => {
if (!response.ok) throw new Error("Response was not ok");
return response.json();
})
.then((data) => {
const imageUrl = data.url;
setImageSrc(imageUrl);
})
.catch((error) => {
setImageAlt("Error");
console.error(error);
})
.finally(() => {
setIsLoading(false);
});
};
useEffect(getNewImage, [isLoading]);
return (
<div
style={{
display: "grid",
gridTemplateColumns: "1fr 1fr",
gridTemplateRows: "1fr 50px",
gridTemplateAreas: "'. .' '. .'",
width: "100vw",
height: "100vh",
}}>
{isLoading ? (
<span>Loading...</span>
) : (
<img
src={imageSrc}
alt={imageAlt}
style={{
width: "100%",
height: "100%",
gridColumnStart: 1,
gridColumnEnd: 3,
display: "block",
objectFit: "contain",
}}
/>
)}
<Button
label={acceptLabel}
action={acceptAction}
/>
<Button
label={discardLabel}
action={discardAction}
/>
</div>
}
);
}

View File

@ -1,10 +1,7 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import './index.css'
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>,
)