Skip to content

Commit

Permalink
feat: optimistic type filter button
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorxfs committed Oct 28, 2023
1 parent e518ffe commit dacd065
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/components/PokemonFilters/Types/Types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface PokemonTypesFilterProps { }

export const PokemonTypesFilter: React.FC<PokemonTypesFilterProps> = ({}) => {
const [selectedTypes, setSelectedTypes] = useState<string[]>([]);
const [optimistic, setOptimistic] = useState<{ type?: string, selected?: boolean }>({});

const router = useRouter();
const searchParams = useSearchParams();
Expand All @@ -62,8 +63,10 @@ export const PokemonTypesFilter: React.FC<PokemonTypesFilterProps> = ({}) => {
const isSelected = index !== -1;

if (isSelected) {
setOptimistic({ type, selected: false });
updatedSelected = removeByIndex(selectedTypes, index);
} else {
setOptimistic({ type, selected: true });
updatedSelected = [...selectedTypes, type];
}

Expand All @@ -86,7 +89,9 @@ export const PokemonTypesFilter: React.FC<PokemonTypesFilterProps> = ({}) => {
<PokemonFilterCard title="types">
<div className="flex flex-wrap gap-4 justify-center">
{items.map((item: PokemonType) => {
const isSelected = selectedTypes.includes(item);
const optimisticSelected = optimistic.selected;
const isSelected = optimistic.type === item ? optimisticSelected : selectedTypes.includes(item);

return (
<button
key={item}
Expand Down

0 comments on commit dacd065

Please sign in to comment.