Skip to content

Commit

Permalink
fix: Show isToofar warning on monthly and weekly (#426)
Browse files Browse the repository at this point in the history
* fix: Show isToofar warning on monthly and weekly

* Fix memoization

* Is too far should come before

* Remove createHistoryItem on api load

* Better warning messages

* Always you smoke
  • Loading branch information
amaury1093 authored Feb 3, 2020
1 parent 9ba0625 commit 8f4569a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 44 deletions.
6 changes: 1 addition & 5 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
module.exports = {
semi: true,
singleQuote: true,
tabWidth: 2
};
module.exports = require('@amaurymartiny/eslintrc/prettierrc');
18 changes: 9 additions & 9 deletions App/Screens/Home/AdditionalInfo/AdditionalInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ export function AdditionalInfo(

const isTooFar = isStationTooFar(currentLocation, api);

// Render a "station too far" warning
if (isTooFar) {
return (
<View style={[theme.withPadding, style]} {...rest}>
<Text style={theme.text}>{i18n.t('home_station_too_far_message')}</Text>
</View>
);
}

// Render a "beta" tag
if (frequency !== 'daily' && !exactCount) {
return (
Expand All @@ -103,14 +112,5 @@ export function AdditionalInfo(
);
}

// Render a "station too far" warning
if (frequency === 'daily' && isTooFar) {
return (
<View style={[theme.withPadding, style]} {...rest}>
<Text style={theme.text}>{i18n.t('home_station_too_far_message')}</Text>
</View>
);
}

return null;
}
24 changes: 17 additions & 7 deletions App/Screens/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ const memoHistoricalCigarettes = pMemoize(
});
},
{
cacheKey: args => JSON.stringify(args)
cacheKey: (frequency: Frequency, { latitude, longitude }: LatLng) => {
// We cache this function with the following cache key
return `${frequency}${latitude}${longitude}`;
}
}
);

Expand Down Expand Up @@ -128,7 +131,7 @@ export function Home(props: HomeProps): React.ReactElement {
? TE.right(results)
: TE.left(
new Error(
`Data for ${frequency} measurements has ${results.length} items`
`Data for ${frequency} measurements on [${currentLocation.latitude},${currentLocation.longitude}] has no items`
)
)
),
Expand All @@ -138,8 +141,16 @@ export function Home(props: HomeProps): React.ReactElement {
value
}));
}),
TE.map(data => sumInDays(data, frequency)),
TE.map(pm25ToCigarettes),
TE.map(data => ({
// Convert the PM2.5 sum to a cigarettes sum
count: pm25ToCigarettes(sumInDays(data, frequency)),
exact:
// We consider that the calculated sums are "exact", if there's at
// least 60 data points (for monthly sum) or 14 data points (for
// weekly sums), These 2 numbers are highly arbirtrary, I'm sure
// there's a more scientific way to find them.
frequency === 'monthly' ? data.length >= 60 : data.length >= 14
})),
TE.fold(
error => {
console.log(`<Home> - ${error.message}`);
Expand All @@ -154,10 +165,9 @@ export function Home(props: HomeProps): React.ReactElement {

return T.of(void undefined);
},
totalCigarettes => {
data => {
setCigarettes({
count: totalCigarettes,
exact: true,
...data,
frequency
});

Expand Down
14 changes: 2 additions & 12 deletions App/components/CigaretteBlock/CigaretteBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,7 @@ function getSwearWord(cigaretteCount: number): string {
}

export function CigaretteBlock(props: CigaretteBlockProps): React.ReactElement {
const {
cigarettes,
frequency,
isGps,
style,
displayFrequency,
...rest
} = props;
const { cigarettes, frequency, style, displayFrequency, ...rest } = props;

// Decide on a swear word. The effect says that the swear word only changes
// when the cigarettes count changes.
Expand All @@ -70,10 +63,7 @@ export function CigaretteBlock(props: CigaretteBlockProps): React.ReactElement {

const text = i18n.t('home_smoked_cigarette_title', {
swearWord,
presentPast:
isGps && frequency === 'daily'
? i18n.t('home_common_you_smoke')
: i18n.t('home_common_you_smoked'),
presentPast: i18n.t('home_common_you_smoke'),
singularPlural:
cigarettesRounded === 1
? i18n.t('home_common_cigarette').toLowerCase()
Expand Down
18 changes: 7 additions & 11 deletions App/stores/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ import * as TE from 'fp-ts/lib/TaskEither';
import promiseAny from 'p-any';
import React, { createContext, useContext, useEffect, useState } from 'react';

import { logFpError, promiseToTE, sideEffect } from '../util/fp';
import { logFpError, promiseToTE } from '../util/fp';
import { noop } from '../util/noop';
import { pm25ToCigarettes } from '../util/secretSauce';
import { ErrorContext } from './error';
import { CurrentLocationContext } from './location';
import { createHistoryItem } from './util';

// FIXME Import from @shootismoke/convert
type OpenAQFormat = Normalized[0];
Expand Down Expand Up @@ -68,7 +67,9 @@ function filterPm25(normalized: Normalized): Api {
}
};
} else {
throw new Error('PM2.5 has not been measured by this station right now');
throw new Error(
`PM2.5 has not been measured by station ${normalized[0].location} right now`
);
}
}

Expand All @@ -78,7 +79,7 @@ function filterPm25(normalized: Normalized): Api {
*
* @param gps - The GPS coordinates to fetch data for
*/
function race(gps: LatLng): TE.TaskEither<Error, Api> {
function raceApi(gps: LatLng): TE.TaskEither<Error, Api> {
// Helper function to fetch & normalize data for 1 provider
async function fetchForProvider<DataByGps, DataByStation, Options>(
provider: ProviderPromise<DataByGps, DataByStation, Options>,
Expand Down Expand Up @@ -122,7 +123,7 @@ interface ApiContextProviderProps {
export function ApiContextProvider({
children
}: ApiContextProviderProps): React.ReactElement {
const { currentLocation, isGps, setCurrentLocation } = useContext(
const { currentLocation, setCurrentLocation } = useContext(
CurrentLocationContext
);
const { setError } = useContext(ErrorContext);
Expand All @@ -139,12 +140,7 @@ export function ApiContextProvider({
}

pipe(
race(currentLocation),
TE.chain(
sideEffect(api =>
isGps ? createHistoryItem(api) : TE.right(void undefined)
)
),
raceApi(currentLocation),
TE.fold(
error => {
setError(error);
Expand Down

0 comments on commit 8f4569a

Please sign in to comment.