diff --git a/src/pages/Character/Character.js b/src/pages/Character/Character.js
new file mode 100644
index 0000000..2380be7
--- /dev/null
+++ b/src/pages/Character/Character.js
@@ -0,0 +1,102 @@
+/* eslint-disable no-console */
+
+import axios from "axios";
+import React, { Component } from "react";
+
+import Layout from "../../components/Layout";
+import CharacterCard from "../../components/CharacterCard";
+import EpisodeCard from "../../components/EpisodeCard";
+
+class Character extends Component {
+ constructor(props) {
+ super(props);
+
+ this.state = {
+ character: null,
+ episodes: [],
+ hasLoaded: false,
+ hasError: false,
+ errorMessage: null,
+ };
+ }
+
+ async componentDidMount() {
+ this.loadCharacter();
+ }
+
+ async loadCharacter() {
+ console.log(this);
+ // eslint-disable-next-line react/destructuring-assignment
+ const id = this.props.match.params.id;
+ const baseURL = `https://rickandmortyapi.com/api/character/ ${id}`;
+ try {
+ const APIcall = await axios.get(baseURL);
+
+ const episodeArray = APIcall.data.episode;
+ const episodes = await axios.all(
+ episodeArray.map((episodeURL) => axios.get(episodeURL)),
+ );
+
+ this.setState({
+ character: APIcall.data,
+ episodes: episodes,
+ hasLoaded: true,
+ });
+ } catch (err) {
+ this.setState({
+ hasError: false,
+ errorMessage: "Character not found!!",
+ });
+ }
+ }
+
+ render() {
+ const {
+ hasLoaded,
+ hasError,
+ errorMessage,
+ character,
+ episodes,
+ } = this.state;
+
+ return (
+
+
+
+ {hasLoaded && !hasError ? (
+
+ ) : (
+
{errorMessage}
+ )}
+
+
+
+
+ {episodes.map((episode) => (
+
+ ))}
+
+
+
+
+
+ );
+ }
+}
+
+export default Character;
diff --git a/src/pages/Character/index.js b/src/pages/Character/index.js
new file mode 100644
index 0000000..f267ceb
--- /dev/null
+++ b/src/pages/Character/index.js
@@ -0,0 +1 @@
+export { default } from "./Character";
diff --git a/src/pages/Episode/Episode.js b/src/pages/Episode/Episode.js
index 8255df5..92b23a0 100644
--- a/src/pages/Episode/Episode.js
+++ b/src/pages/Episode/Episode.js
@@ -1,37 +1,102 @@
+/* eslint-disable no-console */
+import axios from "axios";
import React, { Component } from "react";
import Layout from "../../components/Layout";
-// import CharacterCard from "../../components/CharacterCard";
+import CharacterCard from "../../components/CharacterCard";
class Episode extends Component {
constructor(props) {
super(props);
- this.state = {};
- // episode: null,
- // characters: [],
- // hasLoaded: false,
- // hasError: false,
- // errorMessage: null,
+ this.state = {
+ episode: null,
+ name: "",
+ airDate: "",
+ characters: [],
+ hasLoaded: false,
+ hasError: false,
+ errorMessage: null,
+ };
+ }
+
+ async componentDidMount() {
+ this.loadEpisode();
+ }
+
+ async loadEpisode() {
+ console.log(this);
+ // eslint-disable-next-line react/destructuring-assignment
+ const id = this.props.match.params.id;
+ const baseURL = `https://rickandmortyapi.com/api/episode/ ${id}`;
+ try {
+ const APIcall = await axios.get(baseURL);
+ const charArray = APIcall.data.characters;
+ const chars = await axios.all(
+ charArray.map((charURL) => axios.get(charURL)),
+ );
+
+ this.setState({
+ episode: APIcall.data.episode,
+ characters: chars,
+ name: APIcall.data.name,
+ airDate: APIcall.data.air_date,
+ hasLoaded: true,
+ });
+ } catch (err) {
+ this.setState({
+ hasError: false,
+ errorMessage: "Episode not found!!",
+ });
+ }
}
render() {
+ const {
+ hasLoaded,
+ hasError,
+ errorMessage,
+ characters,
+ name,
+ airDate,
+ episode,
+ } = this.state;
+
return (
- {/* {characters.map((character) => (
-
- ))} */}
+ {hasLoaded && !hasError ? (
+ <>
+
{name}
+
+
{airDate}
+
|
+
{episode}
+
+ >
+ ) : (
+
{errorMessage}
+ )}
+
+
+
+
+
+ {characters.map((character) => (
+
+ ))}
+
+
+
diff --git a/src/pages/Home/Home.js b/src/pages/Home/Home.js
index f86e93c..0e568cd 100644
--- a/src/pages/Home/Home.js
+++ b/src/pages/Home/Home.js
@@ -1,50 +1,131 @@
+/* eslint-disable no-console */
+import axios from "axios";
import React, { Component } from "react";
import Layout from "../../components/Layout";
-// import EpisodeCard from "../../components/EpisodeCard";
+import EpisodeCard from "../../components/EpisodeCard";
class Home extends Component {
constructor(props) {
super(props);
- this.state = {};
- // page: 1,
- // paginationInfo: null,
- // episodes: [],
- // hasLoaded: false,
- // hasError: false,
- // errorMessage: null,
+ this.state = {
+ baseURL: "https://rickandmortyapi.com/api/episode?page=1",
+ episodes: [],
+ hasLoaded: false,
+ hasError: false,
+ errorMessage: null,
+ page: 1,
+ next: null,
+ prev: null,
+ };
}
async componentDidMount() {
- // this.loadEpisodes();
+ this.loadEpisodes();
+ }
+
+ async nextHandler() {
+ const { next } = this.state;
+ await this.setState({
+ baseURL: next,
+ });
+ this.loadEpisodes();
+ }
+
+ async prevHandler() {
+ const { prev } = this.state;
+ await this.setState({
+ baseURL: prev,
+ });
+ this.loadEpisodes();
}
async loadEpisodes() {
console.log(this);
+
+ const { baseURL } = this.state;
+ const currentPage = baseURL.slice(-1);
+
+ try {
+ const APIcall = await axios.get(baseURL);
+ console.log(APIcall);
+ this.setState({
+ episodes: APIcall.data.results,
+ page: currentPage,
+ next: APIcall.data.info.next,
+ prev: APIcall.data.info.prev,
+ hasLoaded: true,
+ });
+ } catch (err) {
+ this.setState({
+ hasError: false,
+ errorMessage: "Episodes not found!!",
+ });
+ }
}
render() {
+ const {
+ episodes,
+ hasLoaded,
+ hasError,
+ errorMessage,
+ prev,
+ next,
+ page,
+ } = this.state;
+
return (
- {/* {hasLoaded && !hasError && (
-
+
+ {hasLoaded && !hasError ? (
Episodes loaded!
+ ) : (
+ {errorMessage}
+ )}
+
+
+
+ {prev && (
+
+ )}
+
+
{page}
+ {next && (
+
+ )}
- )} */}
+
- {/* {episodes.map((episode) => (
-
- ))} */}
+ {episodes.map((episode) => (
+
+ ))}
diff --git a/src/pages/Location/Location.js b/src/pages/Location/Location.js
new file mode 100644
index 0000000..91cbada
--- /dev/null
+++ b/src/pages/Location/Location.js
@@ -0,0 +1,110 @@
+/* eslint-disable no-console */
+import axios from "axios";
+import React, { Component } from "react";
+
+import Layout from "../../components/Layout";
+import CharacterCard from "../../components/CharacterCard";
+
+class Location extends Component {
+ constructor(props) {
+ super(props);
+
+ this.state = {
+ residents: [],
+ dimension: "",
+ name: "",
+ type: "",
+ hasLoaded: false,
+ hasError: false,
+ errorMessage: null,
+ };
+ }
+
+ async componentDidMount() {
+ this.loadLocation();
+ }
+
+ async loadLocation() {
+ console.log(this.props);
+ // eslint-disable-next-line react/destructuring-assignment
+ const id = this.props.match.params.locationId;
+
+ const baseURL = `https://rickandmortyapi.com/api/location/${id}`;
+
+ try {
+ const APIcall = await axios.get(baseURL);
+ console.log(APIcall);
+ const residentsArray = APIcall.data.residents;
+ const res = await axios.all(
+ residentsArray.map((resURL) => axios.get(resURL)),
+ );
+
+ this.setState({
+ dimension: APIcall.data.dimension,
+ residents: res,
+ name: APIcall.data.name,
+ type: APIcall.data.type,
+ hasLoaded: true,
+ });
+ } catch (err) {
+ this.setState({
+ hasError: false,
+ errorMessage: "Location not found!!",
+ });
+ }
+ }
+
+ render() {
+ const {
+ hasLoaded,
+ hasError,
+ errorMessage,
+ residents,
+ name,
+ dimension,
+ type,
+ } = this.state;
+
+ return (
+
+
+
+ {hasLoaded && !hasError ? (
+ <>
+
{name}
+
+
{dimension}
+
|
+
{type}
+
+ >
+ ) : (
+
{errorMessage}
+ )}
+
+
+
+
+
+ {residents.map((character) => (
+
+ ))}
+
+
+
+
+
+
+ );
+ }
+}
+export default Location;
diff --git a/src/pages/Location/index.js b/src/pages/Location/index.js
new file mode 100644
index 0000000..7625c87
--- /dev/null
+++ b/src/pages/Location/index.js
@@ -0,0 +1 @@
+export { default } from "./Location";