Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 0 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-redux": "^7.1.3",
"react-scripts": "3.3.0",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0"
"react-scripts": "3.3.0"
},
"scripts": {
"start": "react-scripts start",
Expand Down
32 changes: 0 additions & 32 deletions src/actions/index.js

This file was deleted.

46 changes: 6 additions & 40 deletions src/components/ISSLocation.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import Map from 'pigeon-maps';
import Marker from 'pigeon-marker';

import { fetchISSLocation } from '../actions';
import ISSContext from '../context/ISSContext';

class ISSLocation extends Component {
componentDidMount() {
const { getCurrentISSLocation } = this.props;
const { getCurrentISSLocation } = this.context;

this.timer = setInterval(
getCurrentISSLocation,
Expand All @@ -26,7 +24,7 @@ class ISSLocation extends Component {
isFetching,
latitude,
longitude,
} = this.props;
} = this.context;
const isLocationPresent = latitude && longitude;

return (
Expand All @@ -37,7 +35,7 @@ class ISSLocation extends Component {
defaultWidth={700}
height={450}
minZoom={1}
maxZoom={8}
maxZoom={20}
zoom={1}
>
{!isFetching && isLocationPresent && <Marker anchor={[latitude, longitude]} />}
Expand All @@ -51,38 +49,6 @@ class ISSLocation extends Component {
}
}

const mapStateToProps = ({
issLocation: {
error,
isFetching,
latitude,
longitude,
},
}) => (
{
error,
isFetching,
latitude,
longitude,
}
);

const mapDispatchToProps = (dispatch) => ({
getCurrentISSLocation: () => dispatch(fetchISSLocation()),
});

ISSLocation.propTypes = {
error: PropTypes.string,
getCurrentISSLocation: PropTypes.func.isRequired,
isFetching: PropTypes.bool.isRequired,
latitude: PropTypes.number,
longitude: PropTypes.number,
};

ISSLocation.defaultProps = {
error: null,
latitude: null,
longitude: null,
};
ISSLocation.contextType = ISSContext;

export default connect(mapStateToProps, mapDispatchToProps)(ISSLocation);
export default ISSLocation;
5 changes: 5 additions & 0 deletions src/context/ISSContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createContext } from 'react';

const ISSContext = createContext();

export default ISSContext;
69 changes: 69 additions & 0 deletions src/context/Provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';

import ISSContext from './ISSContext';
import { getCurrentISSLocation } from '../services/issAPI';

class Provider extends Component {
constructor(props) {
super(props);
this.state = {
error: null,
isFetching: false,
latitude: null,
longitude: null,
};

this.fetchISSLocation = this.fetchISSLocation.bind(this);
this.handleISSLocationSuccess = this.handleISSLocationSuccess.bind(this);
this.handleISSLocationFailure = this.handleISSLocationFailure.bind(this);
}

fetchISSLocation() {
const { isFetching } = this.state;

if (isFetching) return;

this.setState({ isFetching: true });

getCurrentISSLocation()
.then(this.handleISSLocationSuccess, this.handleISSLocationFailure);
}

handleISSLocationSuccess(response) {
const { iss_position: { latitude, longitude } } = response;

this.setState({
isFetching: false,
latitude: parseFloat(latitude),
longitude: parseFloat(longitude),
});
}

handleISSLocationFailure(error) {
this.setState({
isFetching: false,
error: error.message,
});
}

render() {
const contextValue = {
...this.state,
getCurrentISSLocation: this.fetchISSLocation,
};
const { children } = this.props;

return (
<ISSContext.Provider value={contextValue}>
{children}
</ISSContext.Provider>
);
}
}

Provider.propTypes = {
children: PropTypes.node.isRequired,
};

export default Provider;
5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import Provider from './context/Provider';

import './index.css';
import App from './App';
import store from './store';

ReactDOM.render(
<Provider store={store}>
<Provider>
<App />
</Provider>, document.getElementById('root'),
);
8 changes: 0 additions & 8 deletions src/reducers/index.js

This file was deleted.

38 changes: 0 additions & 38 deletions src/reducers/issLocation.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/store/index.js

This file was deleted.