Change getRandomFiles function to async

This commit is contained in:
2022-09-22 15:24:24 +08:00
parent edd5eeb4c0
commit 97693d6bd0

View File

@@ -13,12 +13,17 @@ function GetRandomFiles(props) {
const selectedTag = query.get("t") || ""; const selectedTag = query.get("t") || "";
const { langCode } = useContext(langCodeContext); const { langCode } = useContext(langCodeContext);
function getRandomFiles() { const fetchRandomFiles = async () => {
const resp = await fetch("/api/v1/get_random_files");
const json = await resp.json();
return json.files;
};
async function getRandomFiles() {
setIsLoading(true); setIsLoading(true);
fetch("/api/v1/get_random_files") fetchRandomFiles()
.then((response) => response.json())
.then((data) => { .then((data) => {
setFiles(data.files); setFiles(data);
}) })
.catch((error) => { .catch((error) => {
alert("get_random_files error: " + error); alert("get_random_files error: " + error);
@@ -28,9 +33,8 @@ function GetRandomFiles(props) {
}); });
} }
function getRandomFilesWithTag() { const fetchRandomFilesWithTag = async (selectedTag) => {
setIsLoading(true); const resp = await fetch("/api/v1/get_random_files_with_tag", {
fetch("/api/v1/get_random_files_with_tag", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@@ -38,14 +42,16 @@ function GetRandomFiles(props) {
body: JSON.stringify({ body: JSON.stringify({
id: parseInt(selectedTag), id: parseInt(selectedTag),
}), }),
}) });
.then((response) => response.json()) const json = await resp.json();
.then((data) => { return json.files;
if (data.error) { };
alert(data.error);
} else { function getRandomFilesWithTag() {
setFiles(data.files); setIsLoading(true);
} fetchRandomFilesWithTag(selectedTag)
.then((files) => {
setFiles(files);
}) })
.catch((error) => { .catch((error) => {
alert("get_random_files_with_tag error: " + error); alert("get_random_files_with_tag error: " + error);