-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update route params based on search location
- Loading branch information
Showing
6 changed files
with
109 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
interface RouteParameters { | ||
distance: number; | ||
location: string; | ||
routeType: number; | ||
randomSeed: number; | ||
flipped: boolean; | ||
} | ||
|
||
interface UpdateRouteParameters { | ||
type: 'ROUTE_UPDATE_PARAMETERS'; | ||
payload: RouteParameters; | ||
} | ||
|
||
export function updateRouteParameters(payload: RouteParameters): UpdateRouteParameters { | ||
return { | ||
type: 'ROUTE_UPDATE_PARAMETERS', | ||
payload, | ||
}; | ||
} | ||
|
||
interface GenerateRun { | ||
type: 'ROUTE_GENERATE_RUN'; | ||
} | ||
|
||
export type routeActions = UpdateRouteParameters | GenerateRun; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { RouteState } from '../types'; | ||
import { routeActions } from './actions'; | ||
|
||
const defaultRouteState: RouteState = { | ||
route: { | ||
state: 'initial', | ||
}, | ||
}; | ||
|
||
function route(state: RouteState = defaultRouteState, action: routeActions): RouteState { | ||
switch (action.type) { | ||
case 'ROUTE_UPDATE_PARAMETERS': | ||
return { | ||
...state, | ||
...action.payload, | ||
}; | ||
default: | ||
return state; | ||
} | ||
} | ||
|
||
export default route; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { createSelector } from 'reselect'; | ||
|
||
import { Loadable, RouteState, StoreState } from '../types'; | ||
|
||
const routeState = (state: StoreState): RouteState => state.route; | ||
|
||
const routeValueState = createSelector(routeState, (state) => state.route); | ||
|
||
const isLoading = (item: Loadable): boolean => item.state === 'loading'; | ||
const isInitial = (item: Loadable): boolean => item.state === 'initial'; | ||
const hasError = (item: Loadable): boolean => item.state === 'error'; | ||
const getError = (item: Loadable): Error | undefined => (item.state === 'error' ? item.error : undefined); | ||
const hasValue = (item: Loadable): boolean => item.state === 'hasValue'; | ||
const getValue = <T>(item: Loadable<T>): T | undefined => (item.state === 'hasValue' ? item.data : undefined); | ||
|
||
const isRouteInitialSelector = createSelector(routeValueState, isLoading); | ||
const isRouteLoadingSelector = createSelector(routeValueState, isInitial); | ||
const hasRouteErrorSelector = createSelector(routeValueState, hasError); | ||
const routeErrorSelector = createSelector(routeValueState, getError); | ||
const hasRouteValueSelector = createSelector(routeValueState, hasValue); | ||
const getRouteValueSelector = createSelector(routeValueState, getValue); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,34 @@ | ||
import { RoutesResponse } from '../server/types'; | ||
|
||
export type Loadable<T = unknown> = { | ||
state: 'hasValue'; | ||
data: T; | ||
} | { | ||
state: 'initial'; | ||
} | { | ||
state: 'loading'; | ||
} | { | ||
state: 'error'; | ||
error: Error; | ||
} | ||
|
||
export interface AppState { | ||
drawerOpened: boolean; | ||
defaultDistance: number; | ||
minimumDistance: number; | ||
maximumDistance: number; | ||
} | ||
|
||
export type RouteState = { | ||
location?: string; | ||
distance?: number; | ||
routeType?: number; | ||
randomSeed?: number; | ||
flipped?: boolean; | ||
route: Loadable<RoutesResponse>; | ||
}; | ||
|
||
export interface StoreState { | ||
app: AppState; | ||
route: RouteState; | ||
} |