From c2d5a90df527648c7537deb3e20452f3cf437b71 Mon Sep 17 00:00:00 2001 From: Bastian Meissner Date: Fri, 22 May 2020 00:28:02 +0200 Subject: [PATCH] Added production support for themes also some css improvements --- src/components/appList.js | 21 ++++++++++------- src/components/bookmarkList.js | 24 +++++++++++++------ src/components/elements.js | 1 - src/components/settingsModal.js | 42 +++++++++++++++++++++++++++++---- src/components/themeManager.js | 10 ++++++-- 5 files changed, 75 insertions(+), 23 deletions(-) diff --git a/src/components/appList.js b/src/components/appList.js index 25b0763..ecf3ab2 100644 --- a/src/components/appList.js +++ b/src/components/appList.js @@ -4,7 +4,14 @@ import styled from 'styled-components'; import selectedTheme from './themeManager'; -import { Headline, ListContainer, ItemList, Item, RefreshButton, ErrorMessage } from './elements'; +import { + Headline, + ListContainer, + ItemList, + Item, + RefreshButton, + ErrorMessage +} from './elements'; const IconContainer = styled.div` margin-right: 0.5vh; @@ -41,14 +48,14 @@ const App = styled.div` padding: 1rem; `; -function handleResponse(response) { +const handleResponse = response => { if (response.ok) { return response.json(); } throw new Error('Failed to load app data.'); -} +}; -function useAppData() { +const useAppData = () => { const [appData, setAppData] = useState({ apps: [], error: false }); const fetchAppData = useCallback(() => { (process.env.NODE_ENV === 'production' @@ -62,16 +69,12 @@ function useAppData() { setAppData({ apps: [], error: error.message }); }); }, []); - - useEffect(() => { - console.log(appData) - }, [appData]); useEffect(() => { fetchAppData(); }, [fetchAppData]); return { appData, fetchAppData }; -} +}; const AppList = () => { const { diff --git a/src/components/bookmarkList.js b/src/components/bookmarkList.js index 7a9e518..190842b 100644 --- a/src/components/bookmarkList.js +++ b/src/components/bookmarkList.js @@ -2,7 +2,14 @@ import React, { useCallback, useEffect, useState } from 'react'; import styled from 'styled-components'; import selectedTheme from './themeManager'; -import { Headline, ListContainer, ItemList, Item, RefreshButton, ErrorMessage } from './elements'; +import { + Headline, + ListContainer, + ItemList, + Item, + RefreshButton, + ErrorMessage +} from './elements'; const Group = styled.h4` font-family: Roboto, sans-serif; @@ -28,15 +35,18 @@ const Bookmark = styled.a` font-size: 14px; `; -function handleResponse(response) { +const handleResponse = response => { if (response.ok) { return response.json(); } throw new Error('Failed to load app data.'); -} +}; const useBookmarkData = () => { - const [bookmarkData, setBookmarkData] = useState({ groups: [], error: false }); + const [bookmarkData, setBookmarkData] = useState({ + groups: [], + error: false + }); const fetchBookmarkData = useCallback(() => { (process.env.NODE_ENV === 'production' ? fetch('/bookmarks.json').then(handleResponse) @@ -54,7 +64,7 @@ const useBookmarkData = () => { fetchBookmarkData(); }, [fetchBookmarkData]); return { bookmarkData, fetchBookmarkData }; -} +}; const BookmarkList = () => { const { @@ -63,7 +73,7 @@ const BookmarkList = () => { } = useBookmarkData(); return ( - Applications + Bookmarks refresh {error && {error}} @@ -86,4 +96,4 @@ const BookmarkList = () => { ); }; -export default BookmarkList; \ No newline at end of file +export default BookmarkList; diff --git a/src/components/elements.js b/src/components/elements.js index 57ff3fd..b109113 100644 --- a/src/components/elements.js +++ b/src/components/elements.js @@ -28,7 +28,6 @@ export const ItemList = styled.ul` `; export const Item = styled.li` - max-height: 100px; overflow: hidden; position: relative; list-style: none; diff --git a/src/components/settingsModal.js b/src/components/settingsModal.js index 265e959..9d68039 100644 --- a/src/components/settingsModal.js +++ b/src/components/settingsModal.js @@ -1,13 +1,12 @@ -import React, { useState } from 'react'; +import React, { useCallback, useEffect, useState } from 'react'; import styled from 'styled-components'; import Select from 'react-select'; import searchData from './data/search.json'; -import themeData from './data/themes.json'; import selectedTheme, { setTheme } from './themeManager'; -import { Button, IconButton } from './elements'; +import { Button, IconButton, ErrorMessage } from './elements'; const Modal = styled.div` position: absolute; @@ -103,10 +102,43 @@ const SelectorStyle = { } }; +const handleResponse = response => { + if (response.ok) { + return response.json(); + } + throw new Error('Failed to load app data.'); +}; + +const useThemeData = () => { + const [themeData, setThemeData] = useState({ themes: [], error: false }); + const fetchThemeData = useCallback(() => { + (process.env.NODE_ENV === 'production' + ? fetch('/themes.json').then(handleResponse) + : import('./data/themes.json') + ) + .then(jsonResponse => { + setThemeData({ ...jsonResponse, error: false }); + }) + .catch(error => { + setThemeData({ themes: [], error: error.message }); + }); + }, []); + + useEffect(() => { + fetchThemeData(); + }, [fetchThemeData]); + return { themeData, fetchThemeData }; +}; + const SettingsModal = () => { const [modalHidden, setModalHidden] = useState(true); const [newTheme, setNewTheme] = useState(); + const { + themeData: { themes, error }, + fetchThemeData + } = useThemeData(); + return ( <> { icon="close" onClick={() => setModalHidden(!modalHidden)} /> + {error && {error}} Theme: