Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Fetch API before fetching reverse #329

Merged
merged 1 commit into from
Nov 10, 2019
Merged
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
15 changes: 9 additions & 6 deletions App/stores/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ export function ApiContextProvider({ children }: ApiContextProviderProps) {
const { setError } = useContext(ErrorContext);
const [api, setApi] = useState<Api | undefined>(undefined);

const { latitude, longitude } = currentLocation || {};

useEffect(() => {
setApi(undefined);
setError(undefined);

if (!currentLocation) {
if (!currentLocation || !latitude || !longitude) {
return;
}

Expand All @@ -59,17 +61,18 @@ export function ApiContextProvider({ children }: ApiContextProviderProps) {
)
),
TE.fold(
err => {
setError(err);
return T.of(undefined);
error => {
setError(error);

return T.of(void undefined);
},
newApi => {
setApi(newApi);
return T.of(undefined);
return T.of(void undefined);
}
)
)().catch(logFpError('ApiContextProvider'));
}, [currentLocation]);
}, [latitude, longitude]);

return (
<ApiContext.Provider
Expand Down
27 changes: 21 additions & 6 deletions App/stores/location.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as T from 'fp-ts/lib/Task';
import * as TE from 'fp-ts/lib/TaskEither';
import React, { createContext, useContext, useEffect, useState } from 'react';

import { logFpError } from '../util/fp';
import { logFpError, sideEffect } from '../util/fp';
import { noop } from '../util/noop';
import { ErrorContext } from './error';
import {
Expand Down Expand Up @@ -64,6 +64,21 @@ export function LocationContextProvider({
pipe(
fetchGpsPosition(),
TE.map(({ coords }) => coords),
TE.chain(
sideEffect(gps => {
// Set lat/lng for now, set the reverse location later
// @see https://github.com/amaurymartiny/shoot-i-smoke/issues/323
console.log(
`<LocationContext> - fetchGpsPosition - Got GPS ${JSON.stringify(
gps
)}`
);
setGpsLocation(gps);
setCurrentLocation(gps);

return TE.right(void undefined);
})
),
TE.chain(gps =>
TE.rightTask(
pipe(
Expand All @@ -79,14 +94,14 @@ export function LocationContextProvider({

return T.of(undefined);
},
gps => {
location => {
console.log(
`<LocationContext> - fetchGpsPosition - Got location ${JSON.stringify(
gps
`<LocationContext> - fetchGpsPosition - Got reverse location ${JSON.stringify(
location
)}`
);
setGpsLocation(gps);
setCurrentLocation(gps);
setGpsLocation(location);
setCurrentLocation(location);

return T.of(undefined);
}
Expand Down