From b96fcce340bfd4e1981329ca4881659a95586ca3 Mon Sep 17 00:00:00 2001 From: "Kalle (Sendou)" <38327916+Sendouc@users.noreply.github.com> Date: Sat, 19 Dec 2020 15:28:57 +0200 Subject: [PATCH] sr hook initial --- hooks/sr.ts | 16 ++++++++++++++++ pages/sr/leaderboards/index.tsx | 21 +++++++++++++++++++-- pages/sr/leaderboards/new.tsx | 7 +++++-- 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 hooks/sr.ts diff --git a/hooks/sr.ts b/hooks/sr.ts new file mode 100644 index 000000000..dfdf5b676 --- /dev/null +++ b/hooks/sr.ts @@ -0,0 +1,16 @@ +import { GetAllSalmonRunRecordsData } from "prisma/queries/getAllSalmonRunRecords"; +import useSWR from "swr"; + +export function useSalmonRunRecords() { + const { data = [] } = useSWR("/api/sr/records"); + + console.log({ data }); + + return { + data: data.filter((record) => record.approved), + pendingCount: data.reduce( + (acc, record) => (!record.approved ? ++acc : acc), + 0 + ), + }; +} diff --git a/pages/sr/leaderboards/index.tsx b/pages/sr/leaderboards/index.tsx index 9dcc2b05d..2405ce356 100644 --- a/pages/sr/leaderboards/index.tsx +++ b/pages/sr/leaderboards/index.tsx @@ -1,13 +1,27 @@ -import { Button, Link } from "@chakra-ui/react"; -import { t, Trans } from "@lingui/macro"; +import { Alert, AlertIcon, Box, Button } from "@chakra-ui/react"; +import { Plural, t, Trans } from "@lingui/macro"; import Breadcrumbs from "components/common/Breadcrumbs"; +import { useSalmonRunRecords } from "hooks/sr"; +import Link from "next/link"; const SalmonRunLeaderboardsPage = ({}) => { + const { data, pendingCount } = useSalmonRunRecords(); + console.log({ data, pendingCount }); return ( <> + {pendingCount && ( + + + You have one record waiting for approval} + other={You have # records waiting for approval} + /> + + )} + + {JSON.stringify(data)} + ); }; diff --git a/pages/sr/leaderboards/new.tsx b/pages/sr/leaderboards/new.tsx index 5e4d71c87..ba4513c6d 100644 --- a/pages/sr/leaderboards/new.tsx +++ b/pages/sr/leaderboards/new.tsx @@ -24,8 +24,10 @@ import { sendData } from "lib/postData"; import useUser from "lib/useUser"; import { salmonRunRecordSchema } from "lib/validators/salmonRunRecord"; import Image from "next/image"; +import { useRouter } from "next/router"; import { useState } from "react"; import { Controller, useForm } from "react-hook-form"; +import { mutate } from "swr"; import * as z from "zod"; const salmonRunCategoryToNatural = { @@ -54,6 +56,7 @@ type FormData = z.infer; const AddRecordModal = () => { const { i18n } = useLingui(); + const router = useRouter(); const [sending, setSending] = useState(false); const [loggedInUser] = useUser(); const { handleSubmit, errors, register, control, watch } = useForm({ @@ -74,9 +77,9 @@ const AddRecordModal = () => { setSending(false); if (!success) return; - //mutate(`/api/users/${loggedInUser.id}/builds`); + mutate("/api/sr/records"); - //redirect() + router.push("/sr/leaderboards"); }; return (