diff --git a/package.json b/package.json
index e2438b1d..df456eb1 100644
--- a/package.json
+++ b/package.json
@@ -11,11 +11,15 @@
"start": "webpack-dev-server --mode development --open --history-api-fallback"
},
"dependencies": {
+ "bootstrap": "^4.6.0",
+ "promise-retry": "^2.0.1",
"prop-types": "^15.6.2",
"react": "^16.5.0",
+ "react-bootstrap": "^1.4.3",
"react-dom": "^16.5.0",
"react-redux": "^5.0.7",
"redux": "^4.0.0",
+ "shortid": "^2.2.16",
"styled-components": "^3.4.6"
},
"devDependencies": {
@@ -35,6 +39,7 @@
"eslint-plugin-react": "^7.11.1",
"html-webpack-plugin": "^3.2.0",
"react-hot-loader": "^4.3.6",
+ "redux-devtools": "^3.7.0",
"webpack": "^4.18.0",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.8"
diff --git a/src/components/app/Countries.jsx b/src/components/app/Countries.jsx
new file mode 100644
index 00000000..ecbc45d3
--- /dev/null
+++ b/src/components/app/Countries.jsx
@@ -0,0 +1,243 @@
+import React, { Component } from "react";
+import getCountry from "../../api/country";
+import { Button, Form, Spinner, Jumbotron } from "react-bootstrap";
+import shortid from "shortid";
+import promiseRetry from "promise-retry";
+import CsTable from "./Table";
+import styled from "styled-components";
+import {
+ addCountry,
+ selectCountry,
+ updateCountry,
+ deleteCountry,
+} from "../../store/actions";
+import { connect } from "react-redux";
+
+class Countries extends Component {
+ constructor(props) {
+ super(props);
+
+ this.state = {
+ headerData: ["", "Country", "Population"],
+ population: "",
+ countryDdData: [],
+ country: "AFGHANISTAN",
+ sort: "ascending",
+ data: [],
+ selected: null,
+ isLoading: false,
+ };
+ }
+
+ componentDidMount() {
+ promiseRetry((retry, number) => {
+ if (number !== 1) {
+ this.setState({
+ isLoading: true,
+ });
+ }
+
+ return getCountry()
+ .then((data) => {
+ this.setState({
+ countryDdData: data,
+ isLoading: false,
+ });
+ })
+ .catch(retry);
+ });
+ }
+
+ save = (event) => {
+ event.preventDefault();
+ const { population, country, data } = this.state;
+ const newData = {
+ population: population,
+ country: country,
+ id: data.length + 1,
+ };
+
+ this.props.addCountry(newData);
+ };
+
+ changeHandler = (e) => {
+ this.setState({
+ [e.target.name]: e.target.value,
+ });
+ };
+
+ delete = (e) => {
+ this.props.deleteCountry(parseInt(e));
+ };
+
+ select = (e) => {
+ this.props.selectCountry(parseInt(e));
+ };
+
+ componentWillReceiveProps(nextProps) {
+ if (nextProps.countries.selected !== this.state.selected) {
+ const { population, country } = nextProps.countries.selected;
+ const newState = {
+ ...this.state,
+ population,
+ country,
+ selected: nextProps.countries.selected,
+ };
+ this.setState(newState);
+ }
+
+ if (nextProps.countries.data !== this.state.data) {
+ const { data } = nextProps.countries;
+ const newState = {
+ ...this.state,
+ data,
+ selected: null,
+ };
+ this.setState(newState);
+ }
+ }
+
+ update = () => {
+ const { population, country, selected } = this.state;
+ this.props.updateCountry({
+ data: { population, country },
+ id: selected.id,
+ });
+ };
+
+ cancel = () => {
+ this.setState({
+ selected: null,
+ country: "",
+ population: "",
+ });
+ };
+
+ sortByPopulation = () => {
+ const { data, sort } = this.state;
+ let sortData, sortVal;
+
+ if (sort === "ascending") {
+ sortData = data.sort((a, b) => {
+ return a.population - b.population;
+ });
+ sortVal = "descending";
+ } else {
+ sortData = data.sort((a, b) => {
+ return b.population - a.population;
+ });
+ sortVal = "ascending";
+ }
+
+ this.setState({
+ data: sortData,
+ sort: sortVal,
+ });
+ };
+
+ render() {
+ const { countryDdData, isLoading, sort } = this.state;
+
+ const ButtonGroup = styled.div`
+ padding: 10px 15px;
+ `;
+ const Margin = styled.span`
+ margin: 10px 15px;
+ `;
+
+ return (
+ List of Countries
+