generated from katiaipduarte/cra-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add modal and service to get episode information
- Loading branch information
1 parent
17cd9be
commit 0a7bf7e
Showing
13 changed files
with
239 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const ModalContainer = styled.div` | ||
position: fixed; | ||
z-index: 1; | ||
width: 100vw; | ||
height: 100vh; | ||
background-color: rgba(0, 0, 0, 0.5); | ||
cursor: default; | ||
.content { | ||
background-color: white; | ||
position: absolute; | ||
top: 20%; | ||
left: 19%; | ||
width: 60%; | ||
padding: 20rem; | ||
border-radius: 5rem; | ||
.close { | ||
cursor: pointer; | ||
display: flex; | ||
justify-content: flex-end; | ||
} | ||
.profile { | ||
align-items: center; | ||
display: flex; | ||
justify-content: center; | ||
img { | ||
border-radius: 50%; | ||
flex: 1 1; | ||
margin-right: 15rem; | ||
} | ||
.profile-details { | ||
flex: 2 1; | ||
h1 { | ||
font-weight: bold; | ||
color: green; | ||
font-size: 26rem; | ||
margin-bottom: 25rem; | ||
text-transform: uppercase; | ||
} | ||
} | ||
} | ||
} | ||
`; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { faTimes } from '@fortawesome/free-solid-svg-icons'; | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
import { ModalContainer } from './Modal.style'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { modalStatus, selectCharacter } from '../../store/characters/action'; | ||
import { GlobalState } from '../../store/store'; | ||
import { Character } from '../../interfaces/character'; | ||
import ApiProvider from '../../lib/api-provider'; | ||
import { EpisodeInformation } from '../../interfaces/episode-information'; | ||
|
||
const Modal = (): JSX.Element => { | ||
const dispatch = useDispatch(); | ||
const { getEpisodeInformation } = ApiProvider(); | ||
const [character, setCharacter] = useState<Character>(); | ||
const [episode, setEpisode] = useState<EpisodeInformation>(); | ||
const store = useSelector((state: GlobalState) => state.charactersState); | ||
|
||
useEffect(() => { | ||
const found = store.response.results.find((i) => i.id === store.selected); | ||
|
||
if (found) { | ||
if (found.episode.length > 0) { | ||
getEpisodeInformation(found.episode[0]).then((res) => { | ||
setEpisode(res); | ||
}); | ||
} | ||
setCharacter(found); | ||
} | ||
}, [store.selected]); | ||
|
||
const handleClick = (): void => { | ||
dispatch(modalStatus(false)); | ||
dispatch(selectCharacter(0)); | ||
}; | ||
|
||
const renderEpisodeInformation = (): JSX.Element => { | ||
return ( | ||
<> | ||
<li> | ||
<strong>First episode:</strong> {episode?.name} | ||
</li> | ||
<li> | ||
<strong>Episode Number:</strong> {episode?.episode} | ||
</li> | ||
<li> | ||
<strong>Air date:</strong> {episode?.air_date} | ||
</li> | ||
</> | ||
); | ||
}; | ||
|
||
return ( | ||
<ModalContainer> | ||
<div className="content"> | ||
<span className="close" onClick={() => handleClick()}> | ||
<FontAwesomeIcon icon={faTimes} /> | ||
</span> | ||
{character !== undefined && ( | ||
<div className="profile"> | ||
<img src={character.image} alt={character.name} /> | ||
<div className="profile-details"> | ||
<h1>Character Details</h1> | ||
<ul> | ||
<li> | ||
<strong>Name:</strong> {character.name} | ||
</li> | ||
<li> | ||
<strong>Status:</strong> {character.status} | ||
</li> | ||
<li> | ||
<strong>Gender:</strong> {character.gender} | ||
</li> | ||
<li> | ||
<strong>Species:</strong> {character.species} | ||
</li> | ||
<li> | ||
<strong>Location:</strong> {character.location?.name} | ||
</li> | ||
<li> | ||
<strong>Originally From:</strong> {character.origin?.name} | ||
</li> | ||
<li> | ||
<strong>Episodes Count:</strong> {character.episode.length} | ||
</li> | ||
{episode !== undefined && renderEpisodeInformation()} | ||
</ul> | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
</ModalContainer> | ||
); | ||
}; | ||
|
||
export default Modal; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export type EpisodeInformation = { | ||
id: number; | ||
name: string; | ||
air_date: string; | ||
episode: string; | ||
characters: string[]; | ||
url: string; | ||
created: string; | ||
}; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.