navigation utility

This commit is contained in:
Jeff Colombo 2019-02-19 19:29:32 -05:00
parent 353c57a715
commit f2f32cc93b
3 changed files with 32 additions and 8 deletions

View File

@ -19,6 +19,8 @@ import { Header } from './components/Header';
import { PokemonExplorer } from './components/PokemonExplorer/PokemonExplorer'; import { PokemonExplorer } from './components/PokemonExplorer/PokemonExplorer';
import { PokemonSelectList } from './components/PokemonSelectList/PokemonSelectList'; import { PokemonSelectList } from './components/PokemonSelectList/PokemonSelectList';
import { appendQueryString, getCurrentQueryStringVlaues } from 'app/utils/navigation';
import * as styles from './styles/PokemonApp.scss'; import * as styles from './styles/PokemonApp.scss';
type PokemonAppProps = ReturnType<typeof appReducers>; type PokemonAppProps = ReturnType<typeof appReducers>;
@ -45,11 +47,13 @@ class PokemonApp extends React.Component<IConnectedPokemonAppProps> {
]); ]);
dispatch(ActionsPokemonSelectList.setIsLoading(false)); dispatch(ActionsPokemonSelectList.setIsLoading(false));
const search = new URLSearchParams(location.search); const [
pokemonIdValue,
pokemonFormValue,
activeLeagueValue,
] = getCurrentQueryStringVlaues(location, 'id', 'form', 'league');
const pokemonIdValue = search.get('id');
const pokemonId = pokemonIdValue !== null ? parseInt(pokemonIdValue, 10) : null; const pokemonId = pokemonIdValue !== null ? parseInt(pokemonIdValue, 10) : null;
const pokemonFormValue = search.get('form');
const pokemonForm = pokemonFormValue ? parseInt(pokemonFormValue, 10) : null; const pokemonForm = pokemonFormValue ? parseInt(pokemonFormValue, 10) : null;
if (pokemonId !== null && typeof POGOProtos.Enums.PokemonId[pokemonId] !== 'undefined' && if (pokemonId !== null && typeof POGOProtos.Enums.PokemonId[pokemonId] !== 'undefined' &&
pokemonForm !== null && typeof POGOProtos.Enums.Form[pokemonForm] !== 'undefined' pokemonForm !== null && typeof POGOProtos.Enums.Form[pokemonForm] !== 'undefined'
@ -57,7 +61,6 @@ class PokemonApp extends React.Component<IConnectedPokemonAppProps> {
this.handleActivatePokemon(pokemonId, pokemonForm); this.handleActivatePokemon(pokemonId, pokemonForm);
} }
const activeLeagueValue = search.get('league');
const activeLeague = activeLeagueValue ? parseInt(activeLeagueValue, 10) : null; const activeLeague = activeLeagueValue ? parseInt(activeLeagueValue, 10) : null;
if (activeLeague !== null && typeof League[activeLeague] !== 'undefined') { if (activeLeague !== null && typeof League[activeLeague] !== 'undefined') {
this.handleChangeLeague(activeLeague); this.handleChangeLeague(activeLeague);
@ -165,10 +168,8 @@ class PokemonApp extends React.Component<IConnectedPokemonAppProps> {
location, location,
} = this.props; } = this.props;
const search = new URLSearchParams(location.search);
search.set('league', league.toString());
history.push({ history.push({
search: search.toString() search: appendQueryString(location, { league: league.toString() })
}); });
this.handleChangeLeague(league); this.handleChangeLeague(league);

View File

@ -8,6 +8,7 @@ import { VariableSizeList } from 'react-window';
import classNames from 'classnames'; import classNames from 'classnames';
import { formatDexNumber, formatForm } from 'app/utils/formatter'; import { formatDexNumber, formatForm } from 'app/utils/formatter';
import { appendQueryString } from 'app/utils/navigation';
import { DEFAULT_POKEMON_NAME, IPokemon } from 'app/models/Pokemon'; import { DEFAULT_POKEMON_NAME, IPokemon } from 'app/models/Pokemon';
@ -168,7 +169,10 @@ export class PokemonSelectList extends React.Component<IPokemonSelectListProps,
const onClick = () => this.props.handleActivatePokemon(pokemon.id, pokemon.form); const onClick = () => this.props.handleActivatePokemon(pokemon.id, pokemon.form);
const linkTo = { const linkTo = {
// pathname: '/courses', // pathname: '/courses',
search: `?id=${pokemon.id}&form=${pokemon.form}`, search: appendQueryString(location, {
id: pokemon.id.toString(),
form: pokemon.form.toString(),
}),
// hash: '#the-hash', // hash: '#the-hash',
// state: { fromDashboard: true } // state: { fromDashboard: true }
}; };

View File

@ -0,0 +1,19 @@
import { RouteComponentProps } from 'react-router-dom';
export const getCurrentQueryString = (location : RouteComponentProps['location'] | Window['location']) => {
const search = new URLSearchParams(location.search);
return '?' + search.toString();
};
export const getCurrentQueryStringVlaues = (location : RouteComponentProps['location'] | Window['location'], ...keys : Array<string>) => {
const search = new URLSearchParams(location.search);
return keys.map((key) => search.get(key));
};
export const appendQueryString = (location : RouteComponentProps['location'] | Window['location'], parameters : Record<string, string>) => {
const search = new URLSearchParams(location.search);
Object.entries(parameters).forEach(([ key, value ]) => {
search.set(key, value);
});
return '?' + search.toString();
};