Regions example ready
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
/* @flow */
|
||||||
|
|
||||||
|
import React, { Component, Fragment } from "react";
|
||||||
|
import { Provider } from "react-redux";
|
||||||
|
|
||||||
|
import {
|
||||||
|
ConnectedCountrySelect,
|
||||||
|
ConnectedRegionsTable
|
||||||
|
} from "./regionsApp";
|
||||||
|
|
||||||
|
import { getCountries, getRegions } from "./regionsApp/serviceApi.js";
|
||||||
|
import { store } from "./regionsApp/store.js";
|
||||||
|
|
||||||
|
const dispatcher = fn => (...args) => store.dispatch(fn(...args));
|
||||||
|
|
||||||
|
class App extends Component<{}> {
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<Provider store={store}>
|
||||||
|
<Fragment>
|
||||||
|
<ConnectedCountrySelect
|
||||||
|
getCountries={dispatcher(getCountries)}
|
||||||
|
onSelect={dispatcher(getRegions)}
|
||||||
|
/>
|
||||||
|
<ConnectedRegionsTable />
|
||||||
|
</Fragment>
|
||||||
|
</Provider>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default App;
|
||||||
@@ -11,16 +11,14 @@ import {
|
|||||||
import { getCountries, getRegions } from "./regionsApp/serviceApi.js";
|
import { getCountries, getRegions } from "./regionsApp/serviceApi.js";
|
||||||
import { store } from "./regionsApp/store.js";
|
import { store } from "./regionsApp/store.js";
|
||||||
|
|
||||||
const dispatcher = fn => (...args) => store.dispatch(fn(...args));
|
|
||||||
|
|
||||||
class App extends Component<{}> {
|
class App extends Component<{}> {
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<Provider store={store}>
|
<Provider store={store}>
|
||||||
<Fragment>
|
<Fragment>
|
||||||
<ConnectedCountrySelect
|
<ConnectedCountrySelect
|
||||||
getCountries={dispatcher(getCountries)}
|
getCountries={() => store.dispatch(getCountries())}
|
||||||
onSelect={dispatcher(getRegions)}
|
onSelect={c => store.dispatch(getRegions(c))}
|
||||||
/>
|
/>
|
||||||
<ConnectedRegionsTable />
|
<ConnectedRegionsTable />
|
||||||
</Fragment>
|
</Fragment>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/* @flow */
|
/* @flow */
|
||||||
|
|
||||||
import React from "../../../../../.cache/typescript/2.9/node_modules/@types/react";
|
import React from "react";
|
||||||
import { PropTypes } from "../../../../../.cache/typescript/2.9/node_modules/@types/prop-types";
|
import { PropTypes } from "prop-types";
|
||||||
|
|
||||||
export class ClicksDisplay extends React.PureComponent<{
|
export class ClicksDisplay extends React.PureComponent<{
|
||||||
clicks: number
|
clicks: number
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/* @flow */
|
/* @flow */
|
||||||
|
|
||||||
import React from "../../../../../.cache/typescript/2.9/node_modules/@types/react";
|
import React from "react";
|
||||||
import { PropTypes } from "../../../../../.cache/typescript/2.9/node_modules/@types/prop-types";
|
import { PropTypes } from "prop-types";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
increment,
|
increment,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import ReactDOM from "react-dom";
|
import ReactDOM from "react-dom";
|
||||||
|
|
||||||
import App from "./App.regions.js";
|
import App from "./App.counter.js";
|
||||||
import registerServiceWorker from "./registerServiceWorker";
|
import registerServiceWorker from "./registerServiceWorker";
|
||||||
|
|
||||||
ReactDOM.render(<App />, document.getElementById("root"));
|
ReactDOM.render(<App />, document.getElementById("root"));
|
||||||
|
|||||||
@@ -16,7 +16,9 @@ export class CountrySelect extends React.PureComponent<{
|
|||||||
};
|
};
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.props.getCountries();
|
if (this.props.list.length === 0) {
|
||||||
|
this.props.getCountries();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onSelect = (e: { target: HTMLOptionElement }) =>
|
onSelect = (e: { target: HTMLOptionElement }) =>
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import {
|
|||||||
export const getCountries = () => async dispatch => {
|
export const getCountries = () => async dispatch => {
|
||||||
try {
|
try {
|
||||||
dispatch(countriesRequest());
|
dispatch(countriesRequest());
|
||||||
|
// the next line delays execution for 5 seconds:
|
||||||
|
// await new Promise(resolve => setTimeout(resolve, 5000));
|
||||||
const result = await axios.get(`http://fk-server:8080/countries`);
|
const result = await axios.get(`http://fk-server:8080/countries`);
|
||||||
dispatch(countriesSuccess(result.data));
|
dispatch(countriesSuccess(result.data));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -24,7 +26,30 @@ export const getCountries = () => async dispatch => {
|
|||||||
export const getRegions = (country: string) => async dispatch => {
|
export const getRegions = (country: string) => async dispatch => {
|
||||||
if (country) {
|
if (country) {
|
||||||
try {
|
try {
|
||||||
dispatch(regionsRequest());
|
dispatch(regionsRequest(country));
|
||||||
|
const result = await axios.get(
|
||||||
|
`http://fk-server:8080/regions/${country}`
|
||||||
|
);
|
||||||
|
dispatch(regionsSuccess(result.data));
|
||||||
|
} catch (e) {
|
||||||
|
dispatch(regionsFailure());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dispatch(regionsFailure());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getRegions2 = (country: string) => async (
|
||||||
|
dispatch,
|
||||||
|
getState
|
||||||
|
) => {
|
||||||
|
if (country === getState().currentCountry) {
|
||||||
|
console.log("Hey! You are getting the same country as before!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (country) {
|
||||||
|
try {
|
||||||
|
dispatch(regionsRequest(country));
|
||||||
const result = await axios.get(
|
const result = await axios.get(
|
||||||
`http://fk-server:8080/regions/${country}`
|
`http://fk-server:8080/regions/${country}`
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,32 +1,57 @@
|
|||||||
/* @flow */
|
/* @flow */
|
||||||
|
|
||||||
|
// Countries actions
|
||||||
|
|
||||||
export const COUNTRIES_REQUEST = "countries:request";
|
export const COUNTRIES_REQUEST = "countries:request";
|
||||||
export const COUNTRIES_SUCCESS = "countries:success";
|
export const COUNTRIES_SUCCESS = "countries:success";
|
||||||
export const COUNTRIES_FAILURE = "countries:failure";
|
export const COUNTRIES_FAILURE = "countries:failure";
|
||||||
|
|
||||||
|
export type CountriesAction = {
|
||||||
|
type: string,
|
||||||
|
country?: string,
|
||||||
|
listOfCountries?: [object]
|
||||||
|
};
|
||||||
|
|
||||||
|
export const countriesRequest = () =>
|
||||||
|
({
|
||||||
|
type: COUNTRIES_REQUEST
|
||||||
|
}: CountriesActions);
|
||||||
|
|
||||||
|
export const countriesSuccess = (listOfCountries: []) =>
|
||||||
|
({
|
||||||
|
type: COUNTRIES_SUCCESS,
|
||||||
|
listOfCountries
|
||||||
|
}: CountriesActions);
|
||||||
|
|
||||||
|
export const countriesFailure = () =>
|
||||||
|
({
|
||||||
|
type: COUNTRIES_FAILURE
|
||||||
|
}: CountriesActions);
|
||||||
|
|
||||||
|
// Regions actions
|
||||||
|
|
||||||
export const REGIONS_REQUEST = "regions:request";
|
export const REGIONS_REQUEST = "regions:request";
|
||||||
export const REGIONS_SUCCESS = "regions:success";
|
export const REGIONS_SUCCESS = "regions:success";
|
||||||
export const REGIONS_FAILURE = "regions:failure";
|
export const REGIONS_FAILURE = "regions:failure";
|
||||||
|
|
||||||
export type worldAction = {
|
export type RegionsAction = {
|
||||||
type: string,
|
type: string,
|
||||||
value?: number
|
listOfRegions?: [object]
|
||||||
};
|
};
|
||||||
|
|
||||||
export const countriesRequest = () => ({ type: COUNTRIES_REQUEST });
|
export const regionsRequest = (country: string) =>
|
||||||
|
({
|
||||||
|
type: REGIONS_REQUEST,
|
||||||
|
country
|
||||||
|
}: RegionsActions);
|
||||||
|
|
||||||
export const countriesSuccess = (listOfCountries: []) => ({
|
export const regionsSuccess = (listOfRegions: [{}]) =>
|
||||||
type: COUNTRIES_SUCCESS,
|
({
|
||||||
listOfCountries
|
type: REGIONS_SUCCESS,
|
||||||
});
|
listOfRegions
|
||||||
|
}: RegionsActions);
|
||||||
|
|
||||||
export const countriesFailure = () => ({ type: COUNTRIES_FAILURE });
|
export const regionsFailure = () =>
|
||||||
|
({
|
||||||
export const regionsRequest = () => ({ type: REGIONS_REQUEST });
|
type: REGIONS_FAILURE
|
||||||
|
}: RegionsActions);
|
||||||
export const regionsSuccess = (listOfRegions: []) => ({
|
|
||||||
type: REGIONS_SUCCESS,
|
|
||||||
listOfRegions
|
|
||||||
});
|
|
||||||
|
|
||||||
export const regionsFailure = () => ({ type: REGIONS_FAILURE });
|
|
||||||
|
|||||||
@@ -9,17 +9,20 @@ import {
|
|||||||
REGIONS_FAILURE
|
REGIONS_FAILURE
|
||||||
} from "./world.actions";
|
} from "./world.actions";
|
||||||
|
|
||||||
|
import type { CountriesAction, RegionsAction } from "./world.actions";
|
||||||
|
|
||||||
// import type { CounterAction } from "./world.actions.js";
|
// import type { CounterAction } from "./world.actions.js";
|
||||||
|
|
||||||
export const reducer = (
|
export const reducer = (
|
||||||
state: object = {
|
state: object = {
|
||||||
// initial state
|
// initial state
|
||||||
loadingCountries: false,
|
loadingCountries: false,
|
||||||
|
currentCountry: "",
|
||||||
countries: [],
|
countries: [],
|
||||||
loadingRegions: false,
|
loadingRegions: false,
|
||||||
regions: []
|
regions: []
|
||||||
},
|
},
|
||||||
action: object
|
action: CountriesAction | RegionsAction
|
||||||
) => {
|
) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case COUNTRIES_REQUEST:
|
case COUNTRIES_REQUEST:
|
||||||
@@ -47,6 +50,7 @@ export const reducer = (
|
|||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
loadingRegions: true,
|
loadingRegions: true,
|
||||||
|
currentCountry: action.country,
|
||||||
regions: []
|
regions: []
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user