-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWithoutPage.tsx
55 lines (48 loc) · 1.31 KB
/
WithoutPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React, { useEffect, useState } from "react";
import { EpisodeDto, EpisodesResponse } from "../api/dtos";
import { EpisodeBlock } from "../components/EpisodeBlock";
import styled from "styled-components";
import { fetchEpisodes } from "../api/api";
export const WithoutPage = () => {
const [episodesResponse, setEpisodesResponse] = useState<
EpisodesResponse | undefined
>();
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<Error | undefined>();
useEffect(() => {
setLoading(true);
fetchEpisodes(1)
.then((response) => setEpisodesResponse(response))
.catch((error) => setError(error))
.finally(() => setLoading(false));
}, []);
if (error) {
return <p>An error occurred: {error.message}</p>;
}
if (loading) {
return <p>Loading...</p>;
}
if (!episodesResponse) {
return <p>No episodes found</p>;
}
return (
<>
<Episodes>
{episodesResponse.results.map((result: EpisodeDto) => (
<EpisodeBlock
key={result.id}
episodeId={result.id}
name={result.name}
airDate={result.air_date}
/>
))}
</Episodes>
</>
);
};
const Episodes = styled.div`
display: flex;
flex-direction: row;
flex-wrap: wrap;
width: 51.2rem;
`;