-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8b5d2e
commit 453c755
Showing
4 changed files
with
347 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,55 @@ | ||
export default () => { | ||
console.log('TODO') | ||
import { AnyAction, Reducer, Store } from 'redux' | ||
import { createUseLocation } from './use-location' | ||
|
||
export const UPDATE_LOCATION = '@router/updateLocation' | ||
|
||
export type WouterLocation = { | ||
path: string | ||
search: string | ||
} | ||
|
||
export type WouterAction = { | ||
type: typeof UPDATE_LOCATION | ||
payload: WouterLocation | ||
} | ||
|
||
export type WouterState = { | ||
location: WouterLocation | ||
history: WouterLocation[] | ||
} | ||
|
||
export const createWouterHook = <S extends Store>(store: S) => { | ||
const dispatchLocationUpdate = (location: WouterLocation) => { | ||
store.dispatch({ | ||
type: UPDATE_LOCATION, | ||
payload: location, | ||
}) | ||
} | ||
return createUseLocation(dispatchLocationUpdate) | ||
} | ||
|
||
const initialState: WouterState = { | ||
location: { path: '', search: null }, | ||
history: [], | ||
} | ||
|
||
export const reducer: Reducer = ( | ||
state: WouterState = initialState, | ||
action: WouterAction & AnyAction, | ||
) => { | ||
switch (action.type) { | ||
case UPDATE_LOCATION: | ||
if ( | ||
state.location.path !== action.payload.path || | ||
state.location.search !== action.payload.search | ||
) { | ||
const location = action.payload | ||
const history = [location, ...state.history] | ||
if (history.length > 20) { | ||
history.splice(20) | ||
} | ||
return { location, history } | ||
} | ||
} | ||
return state | ||
} |
Oops, something went wrong.