builds page weapon query param

This commit is contained in:
Kalle (Sendou)
2020-11-26 13:05:47 +02:00
parent b903d23dd7
commit f59c569de4
2 changed files with 26 additions and 3 deletions

View File

@@ -1,6 +1,8 @@
import { Ability } from "@prisma/client";
import { abilities, isMainAbility } from "lib/lists/abilities";
import { weaponToCode } from "lib/lists/weaponCodes";
import { weapons } from "lib/lists/weapons";
import { useRouter } from "next/router";
import { GetBuildsByWeaponData } from "prisma/queries/getBuildsByWeapon";
import { Dispatch, useReducer } from "react";
import useSWR from "swr";
@@ -49,13 +51,17 @@ type Action =
export type UseBuildsByWeaponDispatch = Dispatch<Action>;
// TODO: weapon in the URL
export function useBuildsByWeapon() {
const router = useRouter();
const [state, dispatch] = useReducer(
(oldState: UseBuildsByWeaponState, action: Action) => {
switch (action.type) {
case "SET_WEAPON":
router.replace({
pathname: "/builds",
query: { weapon: action.weapon },
});
return { ...oldState, weapon: action.weapon };
case "EXPAND_USER":
return {
@@ -109,9 +115,24 @@ export function useBuildsByWeapon() {
return oldState;
}
},
{ weapon: "", filters: [], expandedUsers: new Set() as Set<number> }
{
weapon: getInitialWeapon(),
filters: [],
expandedUsers: new Set() as Set<number>,
}
);
function getInitialWeapon() {
if (
typeof router.query.weapon !== "string" ||
!weapons.includes(router.query.weapon as any)
) {
return "";
}
return router.query.weapon;
}
const { data } = useSWR<GetBuildsByWeaponData>(() => {
if (!state.weapon) return null;

View File

@@ -10,6 +10,8 @@ import WeaponSelector from "components/common/WeaponSelector";
import { useBuildsByWeapon } from "hooks/builds";
import { useMyTheme } from "lib/useMyTheme";
// TODO: button to add new build
const BuildsPage = () => {
const {
data,