2020-07-08 19:36:36 +02:00
|
|
|
import React, { useCallback, useEffect, useState } from "react";
|
|
|
|
import { AppCategory } from "./appCategory";
|
|
|
|
|
|
|
|
import {
|
|
|
|
handleResponse,
|
|
|
|
Headline,
|
|
|
|
ListContainer,
|
2020-07-10 00:17:18 +02:00
|
|
|
ErrorMessage,
|
2020-07-08 19:36:36 +02:00
|
|
|
} from "./elements";
|
|
|
|
|
|
|
|
const useAppData = () => {
|
|
|
|
const [appData, setAppData] = useState({
|
|
|
|
categories: [],
|
|
|
|
apps: [],
|
2020-07-10 00:17:18 +02:00
|
|
|
error: false,
|
2020-07-08 19:36:36 +02:00
|
|
|
});
|
|
|
|
const fetchAppData = useCallback(() => {
|
|
|
|
(process.env.NODE_ENV === "production"
|
|
|
|
? fetch("/data/apps.json").then(handleResponse)
|
|
|
|
: import("./data/apps.json")
|
|
|
|
)
|
2020-07-10 00:17:18 +02:00
|
|
|
.then((jsonResponse) => {
|
2020-07-08 19:36:36 +02:00
|
|
|
setAppData({ ...jsonResponse, error: false });
|
|
|
|
})
|
2020-07-10 00:17:18 +02:00
|
|
|
.catch((error) => {
|
2020-07-08 19:36:36 +02:00
|
|
|
setAppData({ categories: [], apps: [], error: error.message });
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchAppData();
|
|
|
|
}, [fetchAppData]);
|
|
|
|
return { appData, fetchAppData };
|
|
|
|
};
|
|
|
|
|
|
|
|
const AppList = () => {
|
|
|
|
const {
|
2020-07-10 00:17:18 +02:00
|
|
|
appData: { categories, apps, error },
|
2020-07-08 19:36:36 +02:00
|
|
|
} = useAppData();
|
2020-07-10 00:17:18 +02:00
|
|
|
|
2020-07-08 19:36:36 +02:00
|
|
|
return (
|
|
|
|
<ListContainer>
|
|
|
|
<Headline>Applications</Headline>
|
|
|
|
{error && <ErrorMessage>{error}</ErrorMessage>}
|
|
|
|
{categories &&
|
|
|
|
categories.map(({ name, items }, idx) => (
|
|
|
|
<AppCategory key={[name, idx].join("")} name={name} items={items} />
|
|
|
|
))}
|
|
|
|
{apps && (
|
|
|
|
<AppCategory
|
2020-09-08 13:18:58 +02:00
|
|
|
name={categories ? "Uncategorized apps" : ""}
|
2020-07-08 19:36:36 +02:00
|
|
|
items={apps}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</ListContainer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AppList;
|