Refactor post() and deal with fetched data as a typed interface.
the post() function now uses await syntax rather than .then()
This commit is contained in:
parent
8a1b52749b
commit
1555cf37cd
@ -3,22 +3,27 @@ import '../App.css';
|
|||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
|
|
||||||
function Gallery() {
|
function Gallery() {
|
||||||
const [data, setData] = useState('');
|
const [data, setData] = useState<galleryItemInfo[]>([]);
|
||||||
const [category, setCategory] = useState('');
|
const [category, setCategory] = useState('');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
post(category, (json: string) => {
|
post(category, (data: galleryItemInfo[]) => {
|
||||||
setData(json);
|
setData(data);
|
||||||
});
|
});
|
||||||
});
|
}, [category]);
|
||||||
|
|
||||||
|
const updateCategory = (category: string) => {
|
||||||
|
setData([]);
|
||||||
|
setCategory(category);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="Gallery">
|
<div className="Gallery">
|
||||||
<div className="Header">
|
<div className="Header">
|
||||||
{getContents(data, true, category, setCategory)}
|
{Contents(data, true, category, updateCategory)}
|
||||||
</div>
|
</div>
|
||||||
<div className="Contents">
|
<div className="Contents">
|
||||||
{getContents(data, false, category, setCategory)}
|
{Contents(data, false, category, updateCategory)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@ -30,30 +35,21 @@ interface galleryItemInfo {
|
|||||||
thumbnail_url: string;
|
thumbnail_url: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getContents(
|
function Contents(
|
||||||
data: string,
|
data: galleryItemInfo[],
|
||||||
getDir: boolean,
|
getDir: boolean,
|
||||||
category: string,
|
category: string,
|
||||||
setCategory: (category: string) => void
|
setCategory: (category: string) => void
|
||||||
) {
|
) {
|
||||||
if (data === '') return;
|
if (data === null) return;
|
||||||
|
|
||||||
let obj = null;
|
return data
|
||||||
try {
|
.sort((a: galleryItemInfo, b: galleryItemInfo): number => {
|
||||||
obj = JSON.parse(data);
|
|
||||||
} catch (error: unknown) {
|
|
||||||
if (!(error instanceof SyntaxError)) {
|
|
||||||
throw new Error(error as unknown as undefined);
|
|
||||||
}
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
return obj
|
|
||||||
.sort((a: galleryItemInfo, b: galleryItemInfo) => {
|
|
||||||
if (a.is_dir && b.is_dir) return 0;
|
if (a.is_dir && b.is_dir) return 0;
|
||||||
if (a.is_dir && !b.is_dir) return -1;
|
if (a.is_dir && !b.is_dir) return -1;
|
||||||
if (!a.is_dir && b.is_dir) return 1;
|
if (!a.is_dir && b.is_dir) return 1;
|
||||||
if (!a.is_dir && !b.is_dir) return 0;
|
if (!a.is_dir && !b.is_dir) return 0;
|
||||||
|
return 0;
|
||||||
})
|
})
|
||||||
.filter((item: galleryItemInfo) => {
|
.filter((item: galleryItemInfo) => {
|
||||||
return getDir == item.is_dir;
|
return getDir == item.is_dir;
|
||||||
@ -135,33 +131,28 @@ function getFileName(string: string): string {
|
|||||||
return string.split('/').at(-1) as unknown as string;
|
return string.split('/').at(-1) as unknown as string;
|
||||||
}
|
}
|
||||||
|
|
||||||
function post(category: string, callback: (text: string) => void) {
|
async function post(
|
||||||
fetch(
|
category: string,
|
||||||
'http://localhost/photo-viewer-backend/php/get.php',
|
setDataCallback: (data: galleryItemInfo[]) => void
|
||||||
//'https://dundun.ddns.net/photo-viewer/photo-viewer-backend/php/get.php',
|
) {
|
||||||
{
|
try {
|
||||||
method: 'POST',
|
const response = await fetch(
|
||||||
headers: {
|
'http://localhost/photo-viewer-backend/php/get.php',
|
||||||
'Content-Type': 'application/json',
|
//'https://dundun.ddns.net/photo-viewer/photo-viewer-backend/php/get.php',
|
||||||
},
|
{
|
||||||
body: JSON.stringify({
|
method: 'POST',
|
||||||
category: category,
|
headers: {
|
||||||
}),
|
'Content-Type': 'application/json',
|
||||||
}
|
},
|
||||||
)
|
body: JSON.stringify({
|
||||||
.then((response) => {
|
category: category,
|
||||||
if (!response.ok) {
|
}),
|
||||||
throw new Error('Network response was not ok');
|
|
||||||
}
|
}
|
||||||
return response;
|
);
|
||||||
})
|
setDataCallback(await response.json());
|
||||||
.then((response) => response.text())
|
} catch (error) {
|
||||||
.then((data) => {
|
console.error('Error fetching data: ', error);
|
||||||
callback(data as unknown as string);
|
}
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error('There was a problem with the fetch operation:', error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Gallery;
|
export default Gallery;
|
||||||
|
Loading…
Reference in New Issue
Block a user