feat: show error msg

This commit is contained in:
monoid 2022-06-29 17:02:57 +09:00
parent 853d9fd0fa
commit 15bbad7dfe
1 changed files with 22 additions and 10 deletions

View File

@ -8,6 +8,8 @@ import { toQueryString } from '../accessor/util';
import { useLocation } from 'react-router-dom'; import { useLocation } from 'react-router-dom';
import { QueryStringToMap } from '../accessor/util'; import { QueryStringToMap } from '../accessor/util';
export type GalleryProp = { export type GalleryProp = {
option?: QueryListOption; option?: QueryListOption;
diff: string; diff: string;
@ -18,26 +20,35 @@ type GalleryState = {
export const GalleryInfo = (props: GalleryProp) => { export const GalleryInfo = (props: GalleryProp) => {
const [state, setState] = useState<GalleryState>({ documents: undefined }); const [state, setState] = useState<GalleryState>({ documents: undefined });
const [error, setError] = useState<string | null>(null);
const [loadAll, setLoadAll] = useState(false); const [loadAll, setLoadAll] = useState(false);
useEffect(() => { useEffect(() => {
const abortController = new AbortController(); const abortController = new AbortController();
console.log('load first',props.option); console.log('load first',props.option);
const load = (async () => { const load = (async () => {
const c = await ContentAccessor.findList(props.option); try{
//todo : if c is undefined, retry to fetch 3 times. and show error message. const c = await ContentAccessor.findList(props.option);
setState({ documents: c }); //todo : if c is undefined, retry to fetch 3 times. and show error message.
setLoadAll(c.length == 0); setState({ documents: c });
}) setLoadAll(c.length == 0);
}
catch(e){
if(e instanceof Error){
setError(e.message);
}
else{
setError("unknown error");
}
}
});
load(); load();
}, [props.diff]); }, [props.diff]);
const queryString = toQueryString(props.option ?? {}); const queryString = toQueryString(props.option ?? {});
if (state.documents === undefined && error == null) {
if (state.documents === undefined) {
return (<LoadingCircle />); return (<LoadingCircle />);
} }
else { else {
return ( return (
<Box sx={{ <Box sx={{
display: 'grid', display: 'grid',
gridRowGap: '1rem' gridRowGap: '1rem'
@ -50,15 +61,16 @@ export const GalleryInfo = (props: GalleryProp) => {
<TagChip key={x} tagname={decodeURIComponent(x)} label={decodeURIComponent(x)}></TagChip>))} <TagChip key={x} tagname={decodeURIComponent(x)} label={decodeURIComponent(x)}></TagChip>))}
</Box>} </Box>}
{ {
state.documents.map(x => { state.documents && state.documents.map(x => {
return (<ContentInfo document={x} key={x.id} return (<ContentInfo document={x} key={x.id}
gallery={`/search?${queryString}`} short />); gallery={`/search?${queryString}`} short />);
}) })
} }
{(error && <Typography variant="h5">Error : {error}</Typography>)}
<Typography variant="body1" sx={{ <Typography variant="body1" sx={{
justifyContent: "center", justifyContent: "center",
textAlign:"center" textAlign:"center"
}}>{state.documents.length} loaded...</Typography> }}>{state.documents ? state.documents.length : "null"} loaded...</Typography>
<Button onClick={()=>loadMore()} disabled={loadAll}>{loadAll ? "Load All" : "Load More"}</Button> <Button onClick={()=>loadMore()} disabled={loadAll}>{loadAll ? "Load All" : "Load More"}</Button>
</Box> </Box>
); );