fa public

This commit is contained in:
Kalle (Sendou) 2021-01-06 21:08:37 +02:00
parent 41f6509ac8
commit 52c8133e28
9 changed files with 162 additions and 10 deletions

View File

@ -2,6 +2,8 @@ import {
Button,
Checkbox,
CheckboxGroup,
FormControl,
FormHelperText,
FormLabel,
HStack,
Modal,
@ -30,6 +32,7 @@ import {
import { GetAllFreeAgentPostsData } from "prisma/queries/getAllFreeAgentPosts";
import { useState } from "react";
import { Controller, useForm } from "react-hook-form";
import { FiTrash } from "react-icons/fi";
import { mutate } from "swr";
import * as z from "zod";
@ -42,6 +45,7 @@ type FormData = z.infer<typeof freeAgentPostSchema>;
const FAModal: React.FC<Props> = ({ onClose, post }) => {
const [sending, setSending] = useState(false);
const [deleting, setDeleting] = useState(false);
const { i18n } = useLingui();
const { handleSubmit, errors, register, watch, control } = useForm<FormData>({
@ -71,6 +75,19 @@ const FAModal: React.FC<Props> = ({ onClose, post }) => {
onClose();
};
const onDelete = async () => {
setDeleting(true);
const success = await sendData("DELETE", "/api/freeagents");
setDeleting(false);
if (!success) return;
mutate("/api/freeagents");
toast(getToastOptions(t`Free agent post deleted`, "success"));
onClose();
};
return (
<Modal isOpen onClose={onClose} size="xl" closeOnOverlayClick={false}>
<ModalOverlay>
@ -85,6 +102,30 @@ const FAModal: React.FC<Props> = ({ onClose, post }) => {
<ModalCloseButton borderRadius="50%" />
<form onSubmit={handleSubmit(onSubmit)}>
<ModalBody pb={6}>
{post && (
<>
<Button
leftIcon={<FiTrash />}
variant="outline"
color="red.500"
isLoading={deleting}
onClick={async () => {
if (window.confirm(t`Delete the free agent post?`))
await onDelete();
}}
>
<Trans>Delete free agent post</Trans>
</Button>
<FormControl>
<FormHelperText mb={6}>
<Trans>
Please note deleting your free agent post also deletes
all the likes you have given and received.
</Trans>
</FormHelperText>
</FormControl>
</>
)}
<FormLabel htmlFor="playstyles">
<Trans>Roles</Trans>
</FormLabel>

View File

@ -14,7 +14,13 @@ const Banner = () => {
return (
<a href="https://www.twitch.tv/endgametv1">
<Box bg={event.bg} p={2} fontWeight="bold" textAlign="center">
<Box
bg={event.bg}
color="white"
p={2}
fontWeight="bold"
textAlign="center"
>
<MyContainer>
<Image
w={10}

View File

@ -0,0 +1,43 @@
import { getMySession } from "lib/getMySession";
import { NextApiRequest, NextApiResponse } from "next";
import prisma from "prisma/client";
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.status(200).json({ message: "not implemented" });
switch (req.method) {
case "PUT":
await putHandler(req, res);
break;
case "DELETE":
await deleteHandler(req, res);
break;
default:
res.status(405).end();
}
};
async function putHandler(req: NextApiRequest, res: NextApiResponse) {
const user = await getMySession(req);
if (!user) return res.status(401).end();
if (typeof req.body.likedId !== "number") {
return res.status(400).end();
}
await prisma.freeAgentPost.update({
where: { userId: user.id },
data: { likedPosts: { connect: { id: req.body.likedId } } },
});
}
async function deleteHandler(req: NextApiRequest, res: NextApiResponse) {
const user = await getMySession(req);
if (!user) return res.status(401).end();
res.status(200).end();
}
export default handler;

View File

@ -1,4 +1,4 @@
import { Box, Button, Divider, Flex } from "@chakra-ui/react";
import { Box, Button, Divider, Flex, IconButton } from "@chakra-ui/react";
import { t, Trans } from "@lingui/macro";
import Breadcrumbs from "components/common/Breadcrumbs";
import Markdown from "components/common/Markdown";
@ -11,10 +11,13 @@ import WeaponImage from "components/common/WeaponImage";
import FAModal from "components/freeagents/FAModal";
import { countries, getEmojiFlag } from "countries-list";
import { useFreeAgents } from "hooks/freeagents";
import { sendData } from "lib/postData";
import { Unpacked } from "lib/types";
import { useMyTheme } from "lib/useMyTheme";
import useUser from "lib/useUser";
import { GetAllFreeAgentPostsData } from "prisma/queries/getAllFreeAgentPosts";
import { useState } from "react";
import { FaHeart, FaRegHeart } from "react-icons/fa";
import {
RiAnchorLine,
RiMicFill,
@ -39,8 +42,8 @@ const FreeAgentsPage = () => {
<Trans>New free agent post</Trans>
)}
</Button>
{[...data, ...data, ...data].map((post) => (
<FreeAgentCard post={post} />
{data.map((post) => (
<FreeAgentCard key={post.id} post={post} isLiked={false} />
))}
</MyContainer>
);
@ -54,10 +57,18 @@ const playstyleToEmoji = {
const FreeAgentCard = ({
post,
isLiked,
}: {
post: Unpacked<GetAllFreeAgentPostsData>;
isLiked: boolean;
}) => {
const { themeColorShade } = useMyTheme();
const [user] = useUser();
const handleClick = async () => {
await sendData("PUT", "/api/freeagents/like", { likedId: post.id });
};
return (
<>
<Box as="section" my={8}>
@ -84,7 +95,7 @@ const FreeAgentCard = ({
{post.user.profile?.weaponPool.length && (
<Box my={2}>
{post.user.profile.weaponPool.map((wpn) => (
<WeaponImage name={wpn} size={32} />
<WeaponImage key={wpn} name={wpn} size={32} />
))}
</Box>
)}
@ -92,6 +103,7 @@ const FreeAgentCard = ({
<Flex mt={4} mb={2}>
{post.playstyles.map((style) => (
<Box
key={style}
w={6}
h={6}
mx={1}
@ -134,6 +146,17 @@ const FreeAgentCard = ({
<Markdown value={post.user.profile.bio} smallHeaders />
</SubTextCollapse>
)}
{user && post.user.discordId !== user.discordId && false && (
<IconButton
color="red.500"
aria-label="Like"
size="lg"
isRound
variant="ghost"
icon={isLiked ? <FaHeart /> : <FaRegHeart />}
onClick={handleClick}
/>
)}
</Box>
<Divider />
</>

View File

@ -4,6 +4,7 @@ import { PrismaClient } from "@prisma/client";
let prisma: PrismaClient;
if (!global.prisma) {
//global.prisma = new PrismaClient({ log: ["query", "info", "warn"] });
global.prisma = new PrismaClient();
}

View File

@ -29,4 +29,7 @@ export const getAllFreeAgentPosts = async () =>
},
},
},
orderBy: {
updatedAt: "desc",
},
});

View File

@ -1,9 +1,10 @@
import { Prisma } from "@prisma/client";
const getFreeAgentPosts = (
testUserId: number
testUserId: number,
otherUserIds: number[]
): Prisma.FreeAgentPostCreateArgs["data"][] => {
return [
const result: Prisma.FreeAgentPostCreateArgs["data"][] = [
{
canVC: "YES",
content:
@ -16,6 +17,21 @@ const getFreeAgentPosts = (
playstyles: ["FRONTLINE", "MIDLINE"],
},
];
otherUserIds
.map((id) => ({
canVC: "MAYBE" as const,
content: `User with id ${id} ready to be recruited`,
playstyles: ["BACKLINE"] as "BACKLINE"[],
user: {
connect: {
id,
},
},
}))
.forEach((fa) => result.push(fa));
return result;
};
export default getFreeAgentPosts;

View File

@ -9,7 +9,7 @@ const main = async () => {
throwIfNotLocalhost();
await deleteExistingRecords();
const [testUser] = await Promise.all(
const [testUser, ...otherUsers] = await Promise.all(
getUsers().map((data) =>
prisma.user.create({
data,
@ -35,8 +35,10 @@ const main = async () => {
console.log("Builds created");
const otherUserIds = otherUsers.map((user) => user.id);
await Promise.all(
getFreeAgentPosts(testUser.id).map((data) =>
getFreeAgentPosts(testUser.id, otherUserIds).map((data) =>
prisma.freeAgentPost.create({ data })
)
);
@ -68,6 +70,7 @@ function throwIfNotLocalhost() {
}
async function deleteExistingRecords() {
await prisma.leagueSquadMember.deleteMany();
await prisma.freeAgentPost.deleteMany({});
await prisma.salmonRunRecord.deleteMany({});
await prisma.salmonRunRotation.deleteMany({});

View File

@ -1,7 +1,7 @@
import { Prisma } from "@prisma/client";
const getUsers = (): Prisma.UserCreateArgs["data"][] => {
const result = [
const result: Prisma.UserCreateArgs["data"][] = [
{
username: "Sendou",
discriminator: "4059",
@ -28,7 +28,23 @@ const getUsers = (): Prisma.UserCreateArgs["data"][] => {
},
];
new Array(10)
.fill(null)
.map((_, i) => ({
discordId: "" + i,
discriminator: padWithZero("" + i, 4),
username: `User ${i}`,
}))
.forEach((value) => result.push(value));
return result;
};
function padWithZero(value: string, targetLength: number) {
return new Array(targetLength - value.length)
.fill(0)
.concat(value.split(""))
.join("");
}
export default getUsers;