@@ -37,145 +37,4 @@ function Home() {
);
}
-// Used as parent of components that need to render data after fetching
-// but difficult to manage own state or need to render using fetched data
-//
-// Since fetch is async, guaranteeing data received before render function is
-// called is impossible in the same function,
-// so this component handles state of said data and
-// conditionally renders 'ComponentToRender' only when that data is received.
-function DataFetcher({url, ComponentToRender}) {
- const [data, setData] = useState();
- useEffect(() => {
- let ignore = false;
-
- const fetchData = async () => {
- const response = await fetch(url);
- if(!ignore) {
- const json = await response.json();
- setData(json);
- }
- }
-
- fetchData();
-
- return () => {
- ignore = true;
- };
- }, [url]);
-
- return data &&
-}
-
-function DogQueryForm({queryOptions}) {
- const [posted, setPosted] = useState(false);
- const [imageLinks, setImageLinks] = useState([]);
-
- const breedOptions = queryOptionsToHTML(queryOptions);
- // Config number of images to pull from API
- const imageCount = 27;
-
- // TODO: Find way to stop user from spamming fetches
- function handleSubmit(e) {
- e.preventDefault();
- setPosted(true);
- const breed = e.target.breeds.value.trim().toLowerCase();
-
- if(breed !== undefined && breed.length > 0) {
- fetch(`http://localhost:3011/dog/${breed}/get-images/${imageCount}`)
- .then(response => response.json())
- .then(data => setImageLinks(data['message']))
- .catch(error => {
- setImageLinks([]);
- })
- }
- }
-
- let imageSectionHTML =
+ );
+}
diff --git a/app/frontend/src/setupTests.js b/app/frontend/src/setupTests.js
deleted file mode 100644
index 8f2609b..0000000
--- a/app/frontend/src/setupTests.js
+++ /dev/null
@@ -1,5 +0,0 @@
-// jest-dom adds custom jest matchers for asserting on DOM nodes.
-// allows you to do things like:
-// expect(element).toHaveTextContent(/react/i)
-// learn more: https://github.com/testing-library/jest-dom
-import '@testing-library/jest-dom';
diff --git a/app/frontend/src/util.js b/app/frontend/src/util.js
index cbe6fec..ec0d830 100644
--- a/app/frontend/src/util.js
+++ b/app/frontend/src/util.js
@@ -1,4 +1,6 @@
+import React, { useEffect, useState } from 'react';
+// Functional helpers
export function getRandomIntInRange(rangeEnd) {
return Math.floor(Math.random() * rangeEnd);
}
@@ -31,3 +33,67 @@ export function queryOptionsToHTML(data) {
return options;
}
+
+// Component helpers
+
+// Used as parent of components that need to render data after fetching
+// but difficult to manage own state or need to render using fetched data
+//
+// Since fetch is async, guaranteeing data received before render function is
+// called is impossible in the same function,
+// so this component handles state of said data and
+// conditionally renders 'ComponentToRender' only when that data is received.
+export function DataFetcher({url, ComponentToRender}) {
+ const [data, setData] = useState(null);
+ useEffect(() => {
+
+ const fetchData = async () => {
+ const response = await fetch(url);
+ const json = await response.json();
+ setData(json);
+ }
+
+ fetchData();
+
+ }, [url]);
+
+ if(data == null) {
+ return (
+ <>
+
Fetching page data
+
If this page doesn't load by itself, there may be an issue with the server
+ >
+ )
+ }
+ return data &&
+ }
+
+export function ImageList(props) {
+ const srcListLength = props.images.length;
+ const renderLength = Math.min(props.desiredLength, srcListLength);
+ const imageList = [];
+
+ // Track chosen indices to prevent duplicate images from being picked
+ const availableIdx = [...Array(srcListLength).keys()];
+
+ for(let i = 0; i < renderLength; i++) {
+ const randomIndex = getRandomIntInRange(availableIdx.length);
+ const imageIndex = availableIdx[randomIndex];
+
+ // Swap last element with chosen element and pop to prevent duplicate copies
+ availableIdx[randomIndex] = availableIdx[availableIdx.length - 1]
+ availableIdx.pop();
+
+ const imgSrc = props.images[imageIndex];
+ const img = ;
+ imageList.push(img);
+
+ }
+
+ // console.log(imageList);
+ return (
+