-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(gatsby): Convert redux/reducers/redirects to typescript (#22810)
* Convert redux/reducer/redirects to typescript * Fix Tests * Fix Test * Fix * address PR feedback * fix issues in test Co-authored-by: Blaine Kasten <[email protected]>
- Loading branch information
1 parent
d07c7b1
commit a10382d
Showing
5 changed files
with
73 additions
and
51 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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import _ from "lodash" | ||
import { IGatsbyState, IRedirect, ICreateRedirectAction } from "../types" | ||
|
||
const redirects = new Map<string, IRedirect[]>() | ||
|
||
function exists(newRedirect: IRedirect): boolean { | ||
const fromPathRedirects = redirects.get(newRedirect.fromPath) | ||
|
||
if (!fromPathRedirects) return false | ||
|
||
return fromPathRedirects.some(redirect => _.isEqual(redirect, newRedirect)) | ||
} | ||
|
||
function add(redirect: IRedirect): void { | ||
let samePathRedirects = redirects.get(redirect.fromPath) | ||
|
||
if (!samePathRedirects) { | ||
samePathRedirects = [] | ||
redirects.set(redirect.fromPath, samePathRedirects) | ||
} | ||
|
||
samePathRedirects.push(redirect) | ||
} | ||
|
||
export const redirectsReducer = ( | ||
state: IGatsbyState["redirects"] = [], | ||
action: ICreateRedirectAction | ||
): IGatsbyState["redirects"] => { | ||
switch (action.type) { | ||
case `CREATE_REDIRECT`: { | ||
const redirect = action.payload | ||
|
||
// Add redirect only if it wasn't yet added to prevent duplicates | ||
if (!exists(redirect)) { | ||
add(redirect) | ||
|
||
state.push(redirect) | ||
} | ||
|
||
return state | ||
} | ||
|
||
default: | ||
return state | ||
} | ||
} |
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