mirror of
https://github.com/bendtherules/buildit-weather-app.git
synced 2026-08-18 13:42:14 +00:00
Move all common data interfaces to utils
This commit is contained in:
@@ -1,20 +1,16 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import '../../../node_modules/open-weather-icons/dist/css/open-weather-icons.css';
|
import '../../../node_modules/open-weather-icons/dist/css/open-weather-icons.css';
|
||||||
import './Component.css';
|
import './Component.css';
|
||||||
import { Forecast5Days, ForecaseAtInstance } from '../WeatherApp';
|
|
||||||
import { SingleDayForecast } from '../SingleDayForecast';
|
import { SingleDayForecast } from '../SingleDayForecast';
|
||||||
import { Utils } from '../../Utils';
|
import { Utils, Forecast5DaysMixed, Forecast5DaysSeperated } from '../../Utils';
|
||||||
|
|
||||||
export interface MultiDayForecastProps {
|
export interface MultiDayForecastProps {
|
||||||
forecast5Days: Forecast5Days;
|
forecast5DaysMixed: Forecast5DaysMixed;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MultiDayForecastState {
|
export interface MultiDayForecastState {
|
||||||
forecastDay1: Array<ForecaseAtInstance>;
|
forecast5DaysSeperated: Forecast5DaysSeperated;
|
||||||
forecastDay2: Array<ForecaseAtInstance>;
|
|
||||||
forecastDay3: Array<ForecaseAtInstance>;
|
|
||||||
forecastDay4: Array<ForecaseAtInstance>;
|
|
||||||
forecastDay5: Array<ForecaseAtInstance>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class MultiDayForecast extends React.Component<MultiDayForecastProps, MultiDayForecastState> {
|
export class MultiDayForecast extends React.Component<MultiDayForecastProps, MultiDayForecastState> {
|
||||||
@@ -33,32 +29,46 @@ export class MultiDayForecast extends React.Component<MultiDayForecastProps, Mul
|
|||||||
|
|
||||||
extractStateFromProps(props: MultiDayForecastProps): MultiDayForecastState {
|
extractStateFromProps(props: MultiDayForecastProps): MultiDayForecastState {
|
||||||
const tempState: MultiDayForecastState = {
|
const tempState: MultiDayForecastState = {
|
||||||
forecastDay1: [],
|
forecast5DaysSeperated: {
|
||||||
forecastDay2: [],
|
forecastDay1: [],
|
||||||
forecastDay3: [],
|
forecastDay2: [],
|
||||||
forecastDay4: [],
|
forecastDay3: [],
|
||||||
forecastDay5: [],
|
forecastDay4: [],
|
||||||
|
forecastDay5: [],
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const forecast5DaysSeperated = tempState.forecast5DaysSeperated;
|
||||||
|
|
||||||
const day1 = Utils.createTodayDateInIndiaTZ(),
|
const day1 = Utils.createTodayDateInIndiaTZ(),
|
||||||
day2 = day1.clone().add(1, 'd'),
|
day2 = day1.clone().add(1, 'd'),
|
||||||
day3 = day1.clone().add(2, 'd'),
|
day3 = day1.clone().add(2, 'd'),
|
||||||
day4 = day1.clone().add(3, 'd'),
|
day4 = day1.clone().add(3, 'd'),
|
||||||
day5 = day1.clone().add(4, 'd');
|
day5 = day1.clone().add(4, 'd');
|
||||||
|
|
||||||
props.forecast5Days.list.forEach((forecastAtInstanceValue) => {
|
props.forecast5DaysMixed.list.forEach((forecastAtInstanceValue) => {
|
||||||
const forecastDay = Utils.createDateFromEpochInIndiaTZ(forecastAtInstanceValue.dt);
|
const forecastDay = Utils.createDateFromEpochInIndiaTZ(forecastAtInstanceValue.dt);
|
||||||
|
|
||||||
if (Utils.checkSameDayInSameTZ(forecastDay, day1)) {
|
if (Utils.checkSameDayInSameTZ(forecastDay, day1)) {
|
||||||
tempState.forecastDay1.push(forecastAtInstanceValue);
|
|
||||||
|
forecast5DaysSeperated.forecastDay1.push(forecastAtInstanceValue);
|
||||||
|
|
||||||
} else if (Utils.checkSameDayInSameTZ(forecastDay, day2)) {
|
} else if (Utils.checkSameDayInSameTZ(forecastDay, day2)) {
|
||||||
tempState.forecastDay2.push(forecastAtInstanceValue);
|
|
||||||
|
forecast5DaysSeperated.forecastDay2.push(forecastAtInstanceValue);
|
||||||
|
|
||||||
} else if (Utils.checkSameDayInSameTZ(forecastDay, day3)) {
|
} else if (Utils.checkSameDayInSameTZ(forecastDay, day3)) {
|
||||||
tempState.forecastDay3.push(forecastAtInstanceValue);
|
|
||||||
|
forecast5DaysSeperated.forecastDay3.push(forecastAtInstanceValue);
|
||||||
|
|
||||||
} else if (Utils.checkSameDayInSameTZ(forecastDay, day4)) {
|
} else if (Utils.checkSameDayInSameTZ(forecastDay, day4)) {
|
||||||
tempState.forecastDay4.push(forecastAtInstanceValue);
|
|
||||||
|
forecast5DaysSeperated.forecastDay4.push(forecastAtInstanceValue);
|
||||||
|
|
||||||
} else if (Utils.checkSameDayInSameTZ(forecastDay, day5)) {
|
} else if (Utils.checkSameDayInSameTZ(forecastDay, day5)) {
|
||||||
tempState.forecastDay5.push(forecastAtInstanceValue);
|
|
||||||
|
forecast5DaysSeperated.forecastDay5.push(forecastAtInstanceValue);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Because it is rolling 5 days, last few times can fall into (today + 5) th day
|
// Because it is rolling 5 days, last few times can fall into (today + 5) th day
|
||||||
// Just skip those values
|
// Just skip those values
|
||||||
@@ -70,13 +80,30 @@ export class MultiDayForecast extends React.Component<MultiDayForecastProps, Mul
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
const forecast5DaysSeperated = this.state.forecast5DaysSeperated;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="MultiDayForecast">
|
<div className="MultiDayForecast">
|
||||||
<SingleDayForecast forecastDay={this.state.forecastDay1} />
|
<SingleDayForecast
|
||||||
<SingleDayForecast forecastDay={this.state.forecastDay2} />
|
forecastCurrentDay={forecast5DaysSeperated.forecastDay1}
|
||||||
<SingleDayForecast forecastDay={this.state.forecastDay3} />
|
forecast5DaysSeperated={forecast5DaysSeperated}
|
||||||
<SingleDayForecast forecastDay={this.state.forecastDay4} />
|
/>
|
||||||
<SingleDayForecast forecastDay={this.state.forecastDay5} />
|
<SingleDayForecast
|
||||||
|
forecastCurrentDay={forecast5DaysSeperated.forecastDay2}
|
||||||
|
forecast5DaysSeperated={forecast5DaysSeperated}
|
||||||
|
/>
|
||||||
|
<SingleDayForecast
|
||||||
|
forecastCurrentDay={forecast5DaysSeperated.forecastDay3}
|
||||||
|
forecast5DaysSeperated={forecast5DaysSeperated}
|
||||||
|
/>
|
||||||
|
<SingleDayForecast
|
||||||
|
forecastCurrentDay={forecast5DaysSeperated.forecastDay4}
|
||||||
|
forecast5DaysSeperated={forecast5DaysSeperated}
|
||||||
|
/>
|
||||||
|
<SingleDayForecast
|
||||||
|
forecastCurrentDay={forecast5DaysSeperated.forecastDay5}
|
||||||
|
forecast5DaysSeperated={forecast5DaysSeperated}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import './Component.css';
|
import './Component.css';
|
||||||
import { ForecaseAtInstance } from '../WeatherApp';
|
import { Forecast5DaysSeperated, ForecaseAtInstance } from '../../Utils';
|
||||||
|
|
||||||
export interface SingleDayForecastProps {
|
export interface SingleDayForecastProps {
|
||||||
forecastDay: Array<ForecaseAtInstance>;
|
forecastCurrentDay: Array<ForecaseAtInstance>;
|
||||||
|
forecast5DaysSeperated: Forecast5DaysSeperated;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SingleDayForecast extends React.Component<SingleDayForecastProps, {}> {
|
export class SingleDayForecast extends React.Component<SingleDayForecastProps, {}> {
|
||||||
@@ -23,7 +24,12 @@ export class SingleDayForecast extends React.Component<SingleDayForecastProps, {
|
|||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className="SingleDayForecast">
|
<div className="SingleDayForecast">
|
||||||
{JSON.stringify(this.props)};
|
<div className="forecast-summary">
|
||||||
|
{JSON.stringify(this.calcSummary())};
|
||||||
|
</div>
|
||||||
|
<div className="forecast-details">
|
||||||
|
BLANK
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,47 +2,11 @@ import * as React from 'react';
|
|||||||
// import * as moment from 'moment';
|
// import * as moment from 'moment';
|
||||||
import '../../../node_modules/open-weather-icons/dist/css/open-weather-icons.css';
|
import '../../../node_modules/open-weather-icons/dist/css/open-weather-icons.css';
|
||||||
import './App.css';
|
import './App.css';
|
||||||
|
import { Forecast5DaysMixed } from '../../Utils';
|
||||||
import { MultiDayForecast } from '../MultiDayForecast';
|
import { MultiDayForecast } from '../MultiDayForecast';
|
||||||
|
|
||||||
export interface ForecaseAtInstance {
|
|
||||||
dt: number;
|
|
||||||
main: {
|
|
||||||
temp: number;
|
|
||||||
temp_min: number;
|
|
||||||
temp_max: number;
|
|
||||||
pressure: number;
|
|
||||||
sea_level: number;
|
|
||||||
grnd_level: number;
|
|
||||||
humidity: number;
|
|
||||||
temp_kf: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
weather: [{
|
|
||||||
id: number;
|
|
||||||
main: string; // Make enum
|
|
||||||
description: string;
|
|
||||||
icon: string;
|
|
||||||
}];
|
|
||||||
|
|
||||||
clouds: {
|
|
||||||
all: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
wind: {
|
|
||||||
speed: number;
|
|
||||||
deg: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
dt_txt: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Forecast5Days {
|
|
||||||
cnt: number;
|
|
||||||
list: Array<ForecaseAtInstance>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface WeatherDataState {
|
export interface WeatherDataState {
|
||||||
data: Forecast5Days | undefined;
|
data: Forecast5DaysMixed | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class WeatherApp extends React.Component<{}, WeatherDataState> {
|
export class WeatherApp extends React.Component<{}, WeatherDataState> {
|
||||||
@@ -59,7 +23,7 @@ export class WeatherApp extends React.Component<{}, WeatherDataState> {
|
|||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
.then((responseObject) => {
|
.then((responseObject) => {
|
||||||
this.setState({ data: responseObject as Forecast5Days });
|
this.setState({ data: responseObject as Forecast5DaysMixed });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,8 +31,8 @@ export class WeatherApp extends React.Component<{}, WeatherDataState> {
|
|||||||
return 'Still Loading';
|
return 'Still Loading';
|
||||||
}
|
}
|
||||||
|
|
||||||
renderDates(forecast5Days: Forecast5Days): JSX.Element | string {
|
renderDates(forecast5DaysMixed: Forecast5DaysMixed): JSX.Element | string {
|
||||||
return <MultiDayForecast forecast5Days={forecast5Days} />;
|
return <MultiDayForecast forecast5DaysMixed={forecast5DaysMixed} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
export { WeatherApp, ForecaseAtInstance, Forecast5Days } from './App';
|
export { WeatherApp } from './App';
|
||||||
+1
-1
@@ -1 +1 @@
|
|||||||
export { Utils, IndiaTZName } from './utils';
|
export { Utils, IndiaTZName, ForecaseAtInstance, Forecast5DaysMixed, Forecast5DaysSeperated } from './utils';
|
||||||
@@ -20,4 +20,49 @@ export class Utils {
|
|||||||
// tslint:disable-next-line:triple-equals
|
// tslint:disable-next-line:triple-equals
|
||||||
return (moment1.clone().startOf('day').format() == moment2.clone().startOf('day').format());
|
return (moment1.clone().startOf('day').format() == moment2.clone().startOf('day').format());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ForecaseAtInstance {
|
||||||
|
dt: number;
|
||||||
|
main: {
|
||||||
|
temp: number;
|
||||||
|
temp_min: number;
|
||||||
|
temp_max: number;
|
||||||
|
pressure: number;
|
||||||
|
sea_level: number;
|
||||||
|
grnd_level: number;
|
||||||
|
humidity: number;
|
||||||
|
temp_kf: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
weather: [{
|
||||||
|
id: number;
|
||||||
|
main: string; // Make enum
|
||||||
|
description: string;
|
||||||
|
icon: string;
|
||||||
|
}];
|
||||||
|
|
||||||
|
clouds: {
|
||||||
|
all: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
wind: {
|
||||||
|
speed: number;
|
||||||
|
deg: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
dt_txt: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Forecast5DaysMixed {
|
||||||
|
cnt: number;
|
||||||
|
list: Array<ForecaseAtInstance>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Forecast5DaysSeperated {
|
||||||
|
forecastDay1: Array<ForecaseAtInstance>;
|
||||||
|
forecastDay2: Array<ForecaseAtInstance>;
|
||||||
|
forecastDay3: Array<ForecaseAtInstance>;
|
||||||
|
forecastDay4: Array<ForecaseAtInstance>;
|
||||||
|
forecastDay5: Array<ForecaseAtInstance>;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user