Created
October 16, 2020 14:40
-
-
Save GregKluska/28c635bab70c1ed03721f4134cc7b046 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, { useState, useEffect } from 'react'; | |
| import { useStaticQuery, graphql } from 'gatsby'; | |
| import { prepareBlogs, state } from './util'; | |
| const LoadingScreen = () => { | |
| return <div>Still Loading</div>; | |
| }; | |
| const EmptyScreen = () => { | |
| return <div>No posts at the moment. Sorry :(</div>; | |
| }; | |
| const ErrorScreen = () => { | |
| return <div>Something went wrong :(</div>; | |
| }; | |
| const GridScreen = () => { | |
| return <div>hehe good</div>; | |
| }; | |
| const BlogGrid = () => { | |
| const [status, setStatus] = useState(state.LOADING); | |
| const [blogData, setBlogData] = useState([]); | |
| const blogRawData = useStaticQuery(graphql` | |
| query Blogs { | |
| allStoryblokEntry(filter: { field_component: { eq: "blog" } }) { | |
| edges { | |
| node { | |
| uuid | |
| name | |
| content | |
| full_slug | |
| } | |
| } | |
| } | |
| } | |
| `); | |
| useEffect(() => { | |
| setBlogData(prepareBlogs(blogRawData)); | |
| }, []); | |
| // Set status: Ready, Error, Empty. Loading state is initial so no need to do it. | |
| useEffect(() => { | |
| if (blogData.length < 1) { | |
| setStatus(state.EMPTY); | |
| return; | |
| } | |
| if (blogData[0] === -1) { | |
| setStatus(state.ERROR); | |
| return; | |
| } | |
| setStatus(state.READY); | |
| }, [blogData]); | |
| const printScreen = () => { | |
| switch (status) { | |
| case state.EMPTY: | |
| return <EmptyScreen />; | |
| case state.ERROR: | |
| return <ErrorScreen />; | |
| case state.READY: | |
| return <GridScreen />; | |
| default: | |
| return <LoadingScreen />; | |
| } | |
| }; | |
| return <div>{printScreen()}</div>; | |
| }; | |
| export default BlogGrid; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment