Skip to content

Commit

Permalink
first impl
Browse files Browse the repository at this point in the history
  • Loading branch information
jackieli-tes committed Jul 1, 2021
1 parent d8b5d2e commit 453c755
Show file tree
Hide file tree
Showing 4 changed files with 347 additions and 11 deletions.
183 changes: 176 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wouter-redux",
"version": "0.0.1",
"version": "0.1.0",
"description": "Use redux state for wouter",
"license": "MIT",
"keywords": [
Expand Down Expand Up @@ -33,12 +33,15 @@
},
"devDependencies": {
"@types/jest": "^26.0.23",
"@types/react": "^17.0.11",
"jest": "^27.0.6",
"prettier": "^2.3.2",
"ts-jest": "^27.0.3",
"typescript": "^4.3.4"
},
"peerDependencies": {
"wouter": "^2.7.4"
"react": ">=16.8.0",
"redux": ">=4.0.0",
"wouter": ">=2.0.0"
}
}
56 changes: 54 additions & 2 deletions src/index.ts
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
}
Loading

0 comments on commit 453c755

Please sign in to comment.