Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#36 | BUG/3 Suggestions and original sentences are loaded simultaneously | Artem #37

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 31 additions & 29 deletions client/src/pages/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,38 @@ import NoneOfTheSuggestions from "./NoneOfTheSuggestions";
import SubmitSuggestion from "./SubmitSuggestion";

export function Home() {
const [randomText, setRandomText] = useState("Loading...");
const [suggestionsText, setSuggestionsText] = useState([
"Loading...",
"Loading...",
"Loading...",
]);
const [selectedSuggestion, setSelectedSuggestion] = useState("");
const [nextOriginalText, setNextOriginalText] = useState(1);
//
//
useEffect(() => {
const loadRandomSentenceFromFile = async () => {
const response = await fetch("/api");
const text = await response.json();
setRandomText(text);
};
loadRandomSentenceFromFile();
}, [nextOriginalText]);
const [randomText, setRandomText] = useState("Loading...");
const [suggestionsText, setSuggestionsText] = useState([
"Loading...",
"Loading...",
"Loading...",
]);
const [selectedSuggestion, setSelectedSuggestion] = useState("");
const [nextOriginalText, setNextOriginalText] = useState(1);

//
useEffect(() => {
const getSuggestionsFromApi = async (text) => {
const response = await fetch(
`https://angocair.garg.ed.ac.uk/best/?text=${encodeURIComponent(text)}`
);
const data = await response.json();
setSuggestionsText(data.data);
};
getSuggestionsFromApi(randomText);
}, [randomText]);
const getSuggestionsFromApi = async (text) => {
const response = await fetch(
`https://angocair.garg.ed.ac.uk/best/?text=${encodeURIComponent(text)}`
);
const data = await response.json();
setSuggestionsText(data.data);
setRandomText(text);
};

useEffect(() => {
const loadRandomSentenceFromFile = async () => {
const response = await fetch("/api");
const text = await response.json();
getSuggestionsFromApi(text);
};
loadRandomSentenceFromFile();
}, [nextOriginalText]);

useEffect(() => {
if (randomText !== "Loading...") {
getSuggestionsFromApi(randomText);
}
}, [randomText]);
Comment on lines +39 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this implementation and it will be great to incorporate this with the LoadingSuggestion which is a spinning image from BUG-1 fix.


//
const suggestions = suggestionsText.map((text, i) => {
Expand Down