├── src ├── components │ ├── rowBlock │ │ ├── rowBlock.css │ │ ├── index.js │ │ └── rowBlock.js │ ├── itemDetails │ │ ├── itemDetails.css │ │ ├── index.js │ │ └── itemDetails.js │ ├── randomChar │ │ ├── randomChar.css │ │ ├── index.js │ │ ├── __snapshots__ │ │ │ └── randomChar.test.js.snap │ │ ├── randomChar.test.js │ │ └── randomChar.js │ ├── pages │ │ ├── booksItem │ │ │ ├── booksItem.css │ │ │ ├── index.js │ │ │ └── booksItem.js │ │ ├── booksPage │ │ │ ├── booksPage.css │ │ │ ├── index.js │ │ │ └── booksPage.js │ │ ├── housesPage │ │ │ ├── housesPage.css │ │ │ ├── index.js │ │ │ └── housesPage.js │ │ └── characterPage │ │ │ ├── characterPage.css │ │ │ ├── index.js │ │ │ └── characterPage.js │ ├── itemList │ │ ├── itemList.css │ │ ├── index.js │ │ ├── __snapshots__ │ │ │ └── itemList.test.js.snap │ │ ├── itemList.test.js │ │ └── itemList.js │ ├── app │ │ ├── index.js │ │ ├── app.css │ │ └── app.js │ ├── header │ │ ├── index.js │ │ ├── __snapshots__ │ │ │ └── header.test.js.snap │ │ ├── header.test.js │ │ ├── header.css │ │ └── header.js │ ├── spinner │ │ ├── index.js │ │ ├── spinner.js │ │ └── spinner.css │ └── errorMessage │ │ ├── index.js │ │ ├── error.jpeg │ │ ├── error.jpg │ │ ├── errorMessage.css │ │ └── errorMessage.js ├── index.js ├── setupTests.js ├── index.css └── services │ └── gotService.js ├── public ├── favicon.ico └── index.html ├── .gitignore ├── package.json └── README.md /src/components/rowBlock/rowBlock.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/components/itemDetails/itemDetails.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/components/randomChar/randomChar.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/components/pages/booksItem/booksItem.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/components/pages/booksPage/booksPage.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/components/pages/housesPage/housesPage.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/components/pages/characterPage/characterPage.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/components/itemList/itemList.css: -------------------------------------------------------------------------------- 1 | .item { 2 | cursor: pointer; 3 | } -------------------------------------------------------------------------------- /src/components/app/index.js: -------------------------------------------------------------------------------- 1 | import App from "./app"; 2 | export default App; -------------------------------------------------------------------------------- /src/components/header/index.js: -------------------------------------------------------------------------------- 1 | import Header from './header' 2 | export default Header -------------------------------------------------------------------------------- /src/components/itemList/index.js: -------------------------------------------------------------------------------- 1 | import ItemList from './itemList' 2 | export default ItemList -------------------------------------------------------------------------------- /src/components/rowBlock/index.js: -------------------------------------------------------------------------------- 1 | import RowBlock from './rowBlock' 2 | export default RowBlock -------------------------------------------------------------------------------- /src/components/spinner/index.js: -------------------------------------------------------------------------------- 1 | import Spinner from './spinner' 2 | export default Spinner 3 | -------------------------------------------------------------------------------- /src/components/pages/booksPage/index.js: -------------------------------------------------------------------------------- 1 | import BooksPage from './booksPage' 2 | export default BooksPage -------------------------------------------------------------------------------- /src/components/randomChar/index.js: -------------------------------------------------------------------------------- 1 | import RandomChar from "./randomChar"; 2 | export default RandomChar; -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrey-kudinov/react-game-of-thrones/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /src/components/pages/booksItem/index.js: -------------------------------------------------------------------------------- 1 | import BooksItem from "./booksItem"; 2 | export default BooksItem -------------------------------------------------------------------------------- /src/components/pages/housesPage/index.js: -------------------------------------------------------------------------------- 1 | import HousesPage from './housesPage' 2 | export default HousesPage -------------------------------------------------------------------------------- /src/components/errorMessage/index.js: -------------------------------------------------------------------------------- 1 | import ErrorMessage from './errorMessage' 2 | export default ErrorMessage -------------------------------------------------------------------------------- /src/components/pages/characterPage/index.js: -------------------------------------------------------------------------------- 1 | import CharacterPage from './characterPage' 2 | export default CharacterPage -------------------------------------------------------------------------------- /src/components/errorMessage/error.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrey-kudinov/react-game-of-thrones/HEAD/src/components/errorMessage/error.jpeg -------------------------------------------------------------------------------- /src/components/errorMessage/error.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrey-kudinov/react-game-of-thrones/HEAD/src/components/errorMessage/error.jpg -------------------------------------------------------------------------------- /src/components/itemDetails/index.js: -------------------------------------------------------------------------------- 1 | import CharDetails, { Field } from "./itemDetails"; 2 | export default CharDetails; 3 | export { Field }; 4 | -------------------------------------------------------------------------------- /src/components/errorMessage/errorMessage.css: -------------------------------------------------------------------------------- 1 | .img-error { 2 | width: 100%; 3 | height: 100%; 4 | object-fit: cover; 5 | object-position: center; 6 | } -------------------------------------------------------------------------------- /src/components/header/__snapshots__/header.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`Testing
Header have rendered correctly 1`] = `ShallowWrapper {}`; 4 | -------------------------------------------------------------------------------- /src/components/itemList/__snapshots__/itemList.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`Testing snap and state ItemList have rendered correctly 1`] = `ShallowWrapper {}`; 4 | -------------------------------------------------------------------------------- /src/components/randomChar/__snapshots__/randomChar.test.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`Testing Testing snap and state RandomChar have rendered correctly 1`] = `ShallowWrapper {}`; 4 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | 4 | import './index.css'; 5 | import App from './components/app/'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById('root') 12 | ); 13 | -------------------------------------------------------------------------------- /src/components/spinner/spinner.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import "./spinner.css"; 3 | 4 | const Spinner = () => { 5 | return ( 6 |
7 |
8 |
9 |
10 |
11 |
12 | ); 13 | }; 14 | 15 | export default Spinner -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | import { configure } from "enzyme"; 2 | import Adapter from "@wojtekmaj/enzyme-adapter-react-17"; 3 | require("jest-extended"); 4 | 5 | configure({ adapter: new Adapter() }); 6 | 7 | const config = { 8 | jest: { 9 | setupTestFrameworkScriptFile: "jest-extended", 10 | }, 11 | }; 12 | 13 | export default config 14 | -------------------------------------------------------------------------------- /src/components/header/header.test.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import Header from "./header"; 3 | import { shallow } from "enzyme"; 4 | 5 | describe("Testing
", () => { 6 | it("Header have rendered correctly", () => { 7 | const header = shallow(
); 8 | expect(header).toMatchSnapshot(); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /src/components/header/header.css: -------------------------------------------------------------------------------- 1 | .header { 2 | padding: 10px 20px; 3 | background-color: black; 4 | display: grid; 5 | grid-auto-flow: column; 6 | align-items: center; 7 | justify-content: space-between; 8 | } 9 | 10 | .links { 11 | display: grid; 12 | grid-auto-flow: column; 13 | gap: 10px; 14 | list-style: none; 15 | } 16 | 17 | .header a { 18 | color: aliceblue; 19 | text-decoration: none; 20 | } -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/components/errorMessage/errorMessage.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import "./errorMessage.css"; 3 | import img from "./error.jpeg"; 4 | 5 | const ErrorMessage = () => { 6 | return ( 7 | <> 8 |
9 | error 10 |
11 | Something goes wrong :-( 12 | 13 | ); 14 | }; 15 | 16 | export default ErrorMessage; 17 | -------------------------------------------------------------------------------- /src/components/rowBlock/rowBlock.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { Col, Row } from "reactstrap"; 3 | 4 | import './rowBlock.css' 5 | 6 | const RowBlock = ({ left, right }) => { 7 | return ( 8 | 9 | 10 | {left} 11 | 12 | 13 | {right} 14 | 15 | 16 | ); 17 | }; 18 | 19 | export default RowBlock -------------------------------------------------------------------------------- /src/components/header/header.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import {Link} from 'react-router-dom' 3 | import './header.css' 4 | 5 | const Header = () => { 6 | return ( 7 |
8 |

Game of Thrones

9 |
    10 |
  • Characters
  • 11 |
  • Books
  • 12 |
  • Houses
  • 13 |
14 |
15 | ) 16 | } 17 | 18 | export default Header -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 10 | 11 | React App 12 | 13 | 14 | 15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /src/components/pages/booksItem/booksItem.js: -------------------------------------------------------------------------------- 1 | import React, {Component} from 'react'; 2 | import gotService from '../../../services/gotService'; 3 | import ItemDetails, {Field} from '../../itemDetails/itemDetails'; 4 | 5 | export default class BooksItem extends Component { 6 | gotService = new gotService(); 7 | 8 | render() { 9 | return ( 10 | 14 | 15 | 16 | 17 | 18 | ) 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /src/components/app/app.css: -------------------------------------------------------------------------------- 1 | .app { 2 | margin-top: 40px; 3 | margin-bottom: 40px; 4 | } 5 | 6 | .blocks { 7 | display: grid; 8 | grid-auto-flow: column; 9 | grid-template-columns: 1fr 1fr; 10 | gap: 20px; 11 | } 12 | 13 | .block { 14 | display: grid; 15 | place-items: center; 16 | min-height: 300px; 17 | background-color: azure; 18 | border-radius: 15px; 19 | box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px; 20 | padding: 20px; 21 | width: 100%; 22 | } 23 | 24 | .button-toggle { 25 | max-width: 200px; 26 | box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 5px; 27 | } 28 | 29 | .content { 30 | display: grid; 31 | grid-auto-flow: row; 32 | gap: 30px; 33 | } 34 | 35 | ul { 36 | margin-bottom: 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /src/components/spinner/spinner.css: -------------------------------------------------------------------------------- 1 | .lds-ring { 2 | display: inline-block; 3 | position: relative; 4 | width: 80px; 5 | height: 80px; 6 | } 7 | .lds-ring div { 8 | box-sizing: border-box; 9 | display: block; 10 | position: absolute; 11 | width: 64px; 12 | height: 64px; 13 | margin: 8px; 14 | border: 8px solid #cef; 15 | border-radius: 50%; 16 | animation: lds-ring 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite; 17 | border-color: #cef transparent transparent transparent; 18 | } 19 | .lds-ring div:nth-child(1) { 20 | animation-delay: -0.45s; 21 | } 22 | .lds-ring div:nth-child(2) { 23 | animation-delay: -0.3s; 24 | } 25 | .lds-ring div:nth-child(3) { 26 | animation-delay: -0.15s; 27 | } 28 | @keyframes lds-ring { 29 | 0% { 30 | transform: rotate(0deg); 31 | } 32 | 100% { 33 | transform: rotate(360deg); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/components/itemList/itemList.test.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ItemList from "./itemList"; 3 | import { mount, shallow } from "enzyme"; 4 | // import gotService from "../../services/gotService"; 5 | 6 | // describe("Testing ", () => { 7 | // const service = new gotService(); 8 | // const list = mount( 9 | // name} /> 10 | // ); 11 | 12 | // it("Click on item list must rerender all list in 1 instance", () => { 13 | // list.setState({ 14 | // itemList: [ 15 | // { name: "qwe", id: 1 }, 16 | // { name: "asd", id: 2 }, 17 | // ], 18 | // }); 19 | // list.find(".item:first-child").simulate("click"); 20 | // expect(list.find("ul")).toHaveLength(1); 21 | // }); 22 | // }); 23 | 24 | describe("Testing snap and state", () => { 25 | const itemList = shallow(); 26 | 27 | it("ItemList have rendered correctly", () => { 28 | expect(itemList).toMatchSnapshot(); 29 | }); 30 | 31 | }); -------------------------------------------------------------------------------- /src/components/pages/booksPage/booksPage.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import ItemList from "../../itemList"; 3 | import "./booksPage.css"; 4 | import ErrorMessage from "../../errorMessage"; 5 | import gotService from "../../../services/gotService"; 6 | import { withRouter } from "react-router-dom"; 7 | 8 | class BooksPage extends Component { 9 | gotService = new gotService(); 10 | 11 | state = { 12 | error: false, 13 | }; 14 | 15 | componentDidCatch() { 16 | this.setState({ error: true }); 17 | } 18 | 19 | render() { 20 | if (this.state.error) { 21 | return ; 22 | } 23 | 24 | return ( 25 | { 27 | this.props.history.push(`${itemId}`); 28 | }} 29 | getData={this.gotService.getAllBooks} 30 | renderItem={({ name, released }) => 31 | `${name} (${released.substr(0, 4)})` 32 | } 33 | /> 34 | ); 35 | } 36 | } 37 | 38 | export default withRouter(BooksPage); 39 | -------------------------------------------------------------------------------- /src/components/itemDetails/itemDetails.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import "./itemDetails.css"; 3 | 4 | const Field = ({ item, field, label }) => { 5 | return ( 6 |
  • 7 | {label}: {item[field]} 8 |
  • 9 | ); 10 | }; 11 | 12 | export { Field }; 13 | 14 | export default class ItemDetails extends Component { 15 | state = { 16 | item: null, 17 | }; 18 | 19 | componentDidMount() { 20 | this.updateItem(); 21 | } 22 | 23 | componentDidUpdate(prevProps) { 24 | if (this.props.itemId !== prevProps.itemId) { 25 | this.updateItem(); 26 | } 27 | } 28 | 29 | updateItem() { 30 | const { itemId, getDetails } = this.props; 31 | if (!itemId) return; 32 | 33 | getDetails(itemId).then((item) => { 34 | this.setState({ item }); 35 | }); 36 | } 37 | 38 | render() { 39 | if (!this.state.item) { 40 | return Please select a item; 41 | } 42 | const { item } = this.state; 43 | const { name } = item; 44 | 45 | return ( 46 |
    47 |

    Name: {name}

    48 |
      49 | {React.Children.map(this.props.children, (child) => { 50 | return React.cloneElement(child, { item }); 51 | })} 52 |
    53 |
    54 | ); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-game-of-thrones", 3 | "homepage": "https://andreykudinov63.github.io/react-game-of-thrones/", 4 | "version": "0.1.0", 5 | "private": true, 6 | "dependencies": { 7 | "@testing-library/jest-dom": "^5.14.1", 8 | "@testing-library/react": "^11.2.7", 9 | "@testing-library/user-event": "^12.8.3", 10 | "@wojtekmaj/enzyme-adapter-react-17": "^0.6.3", 11 | "enzyme": "^3.11.0", 12 | "http-server": "^13.0.0", 13 | "prop-types": "^15.7.2", 14 | "react": "^17.0.2", 15 | "react-dom": "^17.0.2", 16 | "react-router-dom": "^5.2.0", 17 | "react-scripts": "4.0.3", 18 | "react-test-render": "^1.1.2", 19 | "reactstrap": "^8.9.0", 20 | "styled-components": "^5.3.0", 21 | "web-vitals": "^1.1.2" 22 | }, 23 | "scripts": { 24 | "predeploy": "npm run build", 25 | "deploy": "gh-pages -d build", 26 | "start": "react-scripts start", 27 | "build": "react-scripts build", 28 | "test": "react-scripts test", 29 | "eject": "react-scripts eject" 30 | }, 31 | "eslintConfig": { 32 | "extends": [ 33 | "react-app", 34 | "react-app/jest" 35 | ] 36 | }, 37 | "browserslist": { 38 | "production": [ 39 | ">0.2%", 40 | "not dead", 41 | "not op_mini all" 42 | ], 43 | "development": [ 44 | "last 1 chrome version", 45 | "last 1 firefox version", 46 | "last 1 safari version" 47 | ] 48 | }, 49 | "devDependencies": { 50 | "jest-extended": "^0.11.5" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/components/pages/housesPage/housesPage.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import ItemList from "../../itemList"; 3 | import ItemDetails, { Field } from "../../itemDetails"; 4 | import "./housesPage.css"; 5 | import ErrorMessage from "../../errorMessage"; 6 | import gotService from "../../../services/gotService"; 7 | import RowBlock from "../../rowBlock"; 8 | 9 | export default class HousesPage extends Component { 10 | gotService = new gotService(); 11 | 12 | state = { 13 | selectedItem: null, 14 | error: false, 15 | }; 16 | 17 | componentDidCatch() { 18 | this.setState({ error: true }); 19 | } 20 | 21 | onItemSelected = (id) => { 22 | this.setState({ 23 | selectedItem: id, 24 | }); 25 | }; 26 | 27 | render() { 28 | if (this.state.error) { 29 | return ; 30 | } 31 | 32 | const itemList = ( 33 | name} 37 | /> 38 | ); 39 | 40 | const itemDetails = ( 41 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | ); 52 | 53 | return ; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/components/pages/characterPage/characterPage.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import ItemList from "../../itemList"; 3 | import ItemDetails, { Field } from "../../itemDetails"; 4 | import "./characterPage.css"; 5 | import ErrorMessage from "../../errorMessage"; 6 | import gotService from "../../../services/gotService"; 7 | import RowBlock from "../../rowBlock"; 8 | 9 | export default class CharacterPage extends Component { 10 | gotService = new gotService(); 11 | 12 | state = { 13 | selectedItem: null, 14 | error: false, 15 | }; 16 | 17 | componentDidCatch() { 18 | this.setState({ error: true }); 19 | } 20 | 21 | onItemSelected = (id) => { 22 | this.setState({ 23 | selectedItem: id, 24 | }); 25 | }; 26 | 27 | render() { 28 | if (this.state.error) { 29 | return ; 30 | } 31 | 32 | const itemList = ( 33 | `${item.name} (${item.gender})`} 37 | renderItem={({ name, gender }) => `${name} (${gender})`} 38 | /> 39 | ); 40 | 41 | const itemDetails = ( 42 | 46 | 47 | 48 | 49 | 50 | 51 | ); 52 | 53 | return ; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/components/randomChar/randomChar.test.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import RandomChar from "./randomChar"; 3 | import { shallow } from "enzyme"; 4 | 5 | // import renderer from "react-test-renderer"; 6 | 7 | // describe("Testing ", () => { 8 | // it("RandomChar have rendered correctly", () => { 9 | // const char = renderer.create().toJSON(); 10 | // expect(char).toMatchSnapshot(); 11 | // }); 12 | // }); 13 | 14 | describe("Testing ", () => { 15 | const char = shallow(); 16 | 17 | describe("Testing snap and state", () => { 18 | it("RandomChar have rendered correctly", () => { 19 | expect(char).toMatchSnapshot(); 20 | }); 21 | 22 | // it('Should render a title', () => { 23 | // const wrapper = shallow(); 24 | // expect(wrapper.prop('title')).toEqual('Some title') 25 | // }) 26 | 27 | it('RandomChar state "char" is empty object', () => { 28 | expect(char.state().char).toBeObject(); 29 | }); 30 | 31 | it('RandomChar state "loading" is empty object', () => { 32 | expect(char.state().loading).toBeTruthy(); 33 | }); 34 | 35 | it('RandomChar state "error" is empty object', () => { 36 | expect(char.state().error).toBeFalsy(); 37 | }); 38 | }); 39 | 40 | describe("Handlers test", () => { 41 | it("testing onCharLoaded", () => { 42 | char.instance().onCharLoaded(); 43 | expect(char.state().loading).toBeFalsy(); 44 | }); 45 | 46 | it("testing onError", () => { 47 | char.instance().onError(); 48 | expect(char.state().loading).toBeFalsy(); 49 | expect(char.state().error).toBeTruthy(); 50 | }); 51 | 52 | it("testing updateChar", () => { 53 | char.instance().updateChar(); 54 | expect(char.state().loading).toBeFalsy(); 55 | }); 56 | }); 57 | }); 58 | -------------------------------------------------------------------------------- /src/services/gotService.js: -------------------------------------------------------------------------------- 1 | export default class GotService { 2 | constructor() { 3 | this._apiBase = "https://www.anapioficeandfire.com/api"; 4 | } 5 | 6 | getResource = async (url) => { 7 | const res = await fetch(`${this._apiBase}${url}`); 8 | 9 | if (!res.ok) { 10 | throw new Error(`Ошибка по ${url}, статус: ${res.status}`); 11 | } 12 | return await res.json(); 13 | } 14 | 15 | getAllCharacters = async () => { 16 | const res = await this.getResource("/characters?page=5&pageSize=10"); 17 | return res.map(this._transformCharacter) 18 | } 19 | 20 | getAllHouses = async () => { 21 | const res = await this.getResource("/houses/"); 22 | return res.map(this._transformHouse) 23 | } 24 | 25 | getAllBooks = async () => { 26 | const res = await this.getResource("/books/"); 27 | return res.map(this._transformBook) 28 | } 29 | 30 | getCharacter = async (id) => { 31 | const character = await this.getResource(`/characters/${id}`); 32 | return this._transformCharacter(character) 33 | } 34 | 35 | getBook = async (id) => { 36 | const book = await this.getResource(`/books/${id}`); 37 | return this._transformBook(book) 38 | } 39 | 40 | getHouse = async (id) => { 41 | const house = await this.getResource(`/houses/${id}`); 42 | return this._transformHouse(house) 43 | } 44 | 45 | _transformCharacter(char, i) { 46 | return { 47 | name: char.name || "no data :(", 48 | gender: char.gender || "no data :(", 49 | born: char.born || "no data :(", 50 | died: char.died || "no data :(", 51 | culture: char.culture || "no data :(", 52 | id: i + 41 53 | }; 54 | } 55 | 56 | _transformHouse(house, i) { 57 | return { 58 | name: house.name, 59 | region: house.region, 60 | words: house.words, 61 | titles: house.titles, 62 | ancestralWeapons: house.ancestralWeapons, 63 | id: i + 1 64 | } 65 | } 66 | 67 | _transformBook(book, i) { 68 | return { 69 | name: book.name, 70 | numberOfPages: book.numberOfPages, 71 | publisher: book.publisher, 72 | released: book.released, 73 | id: i + 1 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/components/randomChar/randomChar.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import "./randomChar.css"; 3 | import styled from "styled-components"; 4 | import gotService from "../../services/gotService"; 5 | import Spinner from "../spinner"; 6 | import ErrorMessage from "../errorMessage"; 7 | import PropTypes from 'prop-types' 8 | 9 | const RandomCharBlock = styled.div` 10 | /* padding: 20px; */ 11 | `; 12 | 13 | export default class RandomChar extends Component { 14 | gotService = new gotService(); 15 | 16 | state = { 17 | char: {}, 18 | loading: true, 19 | error: false, 20 | timerStart: 20, 21 | }; 22 | 23 | componentDidMount() { 24 | this.updateChar(); 25 | this.timerId = setInterval(this.updateChar, this.props.interval) 26 | this.timerRefresh = setInterval(this.refreshTimer, 1000) 27 | } 28 | 29 | componentWillUnmount() { 30 | clearInterval(this.timerId) 31 | clearInterval(this.timerRefresh) 32 | } 33 | 34 | onCharLoaded = (char) => { 35 | this.setState({ char, loading: false }); 36 | }; 37 | 38 | onError = (err) => { 39 | console.log(err); 40 | this.setState({ error: true, loading: false }); 41 | }; 42 | 43 | updateChar = () => { 44 | const id = Math.floor(Math.random() * 140 + 25); 45 | // const id = 1300000; 46 | this.gotService 47 | .getCharacter(id) 48 | .then(this.onCharLoaded) 49 | .then(this.setState({ timerStart: 20 })) 50 | .catch(this.onError); 51 | } 52 | 53 | refreshTimer = () => { 54 | this.setState({ timerStart: this.state.timerStart - 1 }) 55 | } 56 | 57 | render() { 58 | const { char, loading, error, timerStart } = this.state; 59 | 60 | const errorMessage = error ? : null; 61 | const spinner = loading ? : null; 62 | const content = !(loading || error) ? : null; 63 | 64 | return ( 65 | 66 | {errorMessage} 67 | {spinner} 68 | {content} 69 | 70 | ); 71 | } 72 | } 73 | 74 | const View = ({ char, timerStart }) => { 75 | const { name, gender, born, died, culture, } = char; 76 | return ( 77 | <> 78 | {timerStart} seconds 79 |

    Random Character: {name}

    80 |
      81 |
    • gender: {gender}
    • 82 |
    • born: {born}
    • 83 |
    • died: {died}
    • 84 |
    • culture: {culture}
    • 85 |
    86 | 87 | ); 88 | }; 89 | 90 | 91 | RandomChar.defaultProps = { 92 | interval: 20000 93 | } 94 | 95 | // RandomChar.propTypes = { 96 | // interval: (props, propName, componentName) => { 97 | // const value = props[propName]; 98 | 99 | // if(typeof value === 'number' && !isNaN(value)) { 100 | // return null 101 | // } 102 | // return new TypeError(`${componentName}: ${propName} must be a number`) 103 | // } 104 | // } 105 | 106 | RandomChar.propTypes = { 107 | interval: PropTypes.number 108 | } 109 | -------------------------------------------------------------------------------- /src/components/app/app.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import { Col, Row, Container, Button } from "reactstrap"; 3 | import RandomChar from "../randomChar"; 4 | import ErrorMessage from "../errorMessage"; 5 | import CharacterPage from "../pages/characterPage/characterPage"; 6 | import HousesPage from "../pages/housesPage/housesPage"; 7 | import BooksPage from "../pages/booksPage/booksPage"; 8 | import gotService from "../../services/gotService"; 9 | import BooksItem from "../pages/booksItem/booksItem"; 10 | import { BrowserRouter as Router, Route } from "react-router-dom"; 11 | import Header from "../header"; 12 | 13 | import "./app.css"; 14 | 15 | export default class App extends Component { 16 | state = { 17 | isCharacter: true, 18 | error: false, 19 | }; 20 | 21 | gotService = new gotService(); 22 | 23 | componentDidCatch() { 24 | this.setState({ error: true }); 25 | } 26 | 27 | toggleCharacter = () => { 28 | this.setState({ isCharacter: !this.state.isCharacter }); 29 | }; 30 | 31 | render() { 32 | const character = this.state.isCharacter ? ( 33 | 34 | 35 | 36 | ) : null; 37 | 38 | if (this.state.error) { 39 | return ; 40 | } 41 | return ( 42 | 43 |
    44 |
    45 | 46 | {character} 47 | 48 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | { 64 | const { id } = match.params; 65 | return ; 66 | }} 67 | /> 68 | 69 | {/* 70 | 71 | item.name} 75 | /> 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | item.name} 88 | /> 89 | 90 | 91 | 92 | 93 | */} 94 | 95 |
    96 | 97 | ); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Create React App 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 4 | 5 | ## Available Scripts 6 | 7 | In the project directory, you can run: 8 | 9 | ### `npm start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 13 | 14 | The page will reload if you make edits.\ 15 | You will also see any lint errors in the console. 16 | 17 | ### `npm test` 18 | 19 | Launches the test runner in the interactive watch mode.\ 20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 21 | 22 | ### `npm run build` 23 | 24 | Builds the app for production to the `build` folder.\ 25 | It correctly bundles React in production mode and optimizes the build for the best performance. 26 | 27 | The build is minified and the filenames include the hashes.\ 28 | Your app is ready to be deployed! 29 | 30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 31 | 32 | ### `npm run eject` 33 | 34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 35 | 36 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 37 | 38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 39 | 40 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 41 | 42 | ## Learn More 43 | 44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 45 | 46 | To learn React, check out the [React documentation](https://reactjs.org/). 47 | 48 | ### Code Splitting 49 | 50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) 51 | 52 | ### Analyzing the Bundle Size 53 | 54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) 55 | 56 | ### Making a Progressive Web App 57 | 58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) 59 | 60 | ### Advanced Configuration 61 | 62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) 63 | 64 | ### Deployment 65 | 66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) 67 | 68 | ### `npm run build` fails to minify 69 | 70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) 71 | -------------------------------------------------------------------------------- /src/components/itemList/itemList.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import "./itemList.css"; 3 | import Spinner from "../spinner"; 4 | // import gotService from "../../services/gotService"; 5 | // import PropTypes from "prop-types"; 6 | 7 | function ItemList({ getData, onItemSelected, renderItem }) { 8 | const [itemList, updateList] = useState([]); 9 | 10 | useEffect(() => { 11 | getData().then((data) => { 12 | updateList(data); 13 | }); 14 | // eslint-disable-next-line react-hooks/exhaustive-deps 15 | }, []); 16 | 17 | function renderItems(arr) { 18 | return arr.map((item, i) => { 19 | const { id } = item; 20 | 21 | const label = renderItem(item); 22 | 23 | return ( 24 |
  • onItemSelected(id)}> 25 | {label} 26 |
  • 27 | ); 28 | }); 29 | } 30 | 31 | if (!itemList) { 32 | return ; 33 | } 34 | 35 | const items = renderItems(itemList); 36 | 37 | return
      {items}
    ; 38 | } 39 | 40 | export default ItemList 41 | 42 | // ! классовый компонент 43 | 44 | // export default class ItemList extends Component { 45 | // state = { 46 | // itemList: null, 47 | // }; 48 | 49 | // renderItems(arr) { 50 | // return arr.map((item, i) => { 51 | // const { id } = item; 52 | 53 | // const label = this.props.renderItem(item); 54 | 55 | // return ( 56 | //
  • this.props.onItemSelected(id)} 60 | // > 61 | // {label} 62 | //
  • 63 | // ); 64 | // }); 65 | // } 66 | 67 | // render() { 68 | // const { itemList } = this.state; 69 | 70 | // if (!itemList) { 71 | // return ; 72 | // } 73 | 74 | // const items = this.renderItems(itemList); 75 | 76 | // return
      {items}
    ; 77 | // } 78 | // } 79 | // componentDidMount() { 80 | // const { getData } = this.props; 81 | 82 | // getData().then((itemList) => { 83 | // this.setState({ itemList }); 84 | // }); 85 | // } 86 | 87 | // ItemList.defaultProps = { 88 | // onItemSelected: () => {} 89 | // } 90 | 91 | // ! компонент высшего порядка 92 | 93 | // class ItemList extends Component { 94 | // static defaultProps = { 95 | // onItemSelected: () => {}, 96 | // }; 97 | 98 | // static propTypes = { 99 | // onItemSelected: PropTypes.func, 100 | // }; 101 | 102 | // renderItems(arr) { 103 | // return arr.map((item) => { 104 | // const { id } = item; 105 | 106 | // const label = this.props.renderItem(item); 107 | 108 | // return ( 109 | //
  • this.props.onItemSelected(id)} 113 | // > 114 | // {label} 115 | //
  • 116 | // ); 117 | // }); 118 | // } 119 | 120 | // render() { 121 | // const { data } = this.props; 122 | // const items = this.renderItems(data); 123 | 124 | // return
      {items}
    ; 125 | // } 126 | // } 127 | 128 | // const withData = (View, getData) => { 129 | // return class extends Component { 130 | // state = { 131 | // data: null, 132 | // }; 133 | 134 | // componentDidMount() { 135 | // getData().then((data) => { 136 | // this.setState({ data }); 137 | // }); 138 | // } 139 | 140 | // render() { 141 | // const { data } = this.state; 142 | 143 | // if (!data) { 144 | // return ; 145 | // } 146 | // return ; 147 | // } 148 | // }; 149 | // }; 150 | 151 | // const {getAllCharacters} = new gotService() 152 | // export default withData(ItemList, getAllCharacters); 153 | --------------------------------------------------------------------------------