From 6b171a6f87a864fb7faab956fb7ea0b638e81dcf Mon Sep 17 00:00:00 2001 From: Bailey Pumfleet Date: Mon, 15 Nov 2021 12:25:49 +0000 Subject: [PATCH] Manually reorder event types (#1142) * Add event type reordering * Add migration for position field * hack on a hack * can edit * fix ordering * Remove console.log Co-authored-by: Alex Johansson Co-authored-by: KATT --- pages/[user].tsx | 8 ++ pages/event-types/index.tsx | 59 ++++++++- .../migration.sql | 2 + prisma/schema.prisma | 1 + public/static/locales/en/common.json | 1 + server/routers/viewer.tsx | 120 +++++++++++++++++- 6 files changed, 184 insertions(+), 7 deletions(-) create mode 100644 prisma/migrations/20211106121119_add_event_type_position/migration.sql diff --git a/pages/[user].tsx b/pages/[user].tsx index 1643db19..b1949c09 100644 --- a/pages/[user].tsx +++ b/pages/[user].tsx @@ -124,6 +124,14 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) => }, ], }, + orderBy: [ + { + position: "desc", + }, + { + id: "asc", + }, + ], select: { id: true, slug: true, diff --git a/pages/event-types/index.tsx b/pages/event-types/index.tsx index 0fa359a2..789ef292 100644 --- a/pages/event-types/index.tsx +++ b/pages/event-types/index.tsx @@ -1,13 +1,20 @@ // TODO: replace headlessui with radix-ui import { Menu, Transition } from "@headlessui/react"; -import { UsersIcon } from "@heroicons/react/solid"; -import { ChevronDownIcon, PlusIcon } from "@heroicons/react/solid"; -import { DotsHorizontalIcon, ExternalLinkIcon, LinkIcon } from "@heroicons/react/solid"; +import { + DotsHorizontalIcon, + ExternalLinkIcon, + LinkIcon, + ArrowDownIcon, + ChevronDownIcon, + PlusIcon, + ArrowUpIcon, + UsersIcon, +} from "@heroicons/react/solid"; import { SchedulingType } from "@prisma/client"; import Head from "next/head"; import Link from "next/link"; import { useRouter } from "next/router"; -import React, { Fragment, useRef } from "react"; +import React, { Fragment, useRef, useState, useEffect } from "react"; import { useMutation } from "react-query"; import { QueryCell } from "@lib/QueryCell"; @@ -72,10 +79,40 @@ interface EventTypeListProps { } const EventTypeList = ({ readOnly, types, profile }: EventTypeListProps): JSX.Element => { const { t } = useLocale(); + + const utils = trpc.useContext(); + const mutation = trpc.useMutation("viewer.eventTypeOrder", { + onError: (err) => { + console.error(err.message); + }, + async onSettled() { + await utils.cancelQuery(["viewer.eventTypes"]); + await utils.invalidateQueries(["viewer.eventTypes"]); + }, + }); + const [sortableTypes, setSortableTypes] = useState(types); + useEffect(() => { + setSortableTypes(types); + }, [types]); + function moveEventType(index: number, increment: 1 | -1) { + const newList = [...sortableTypes]; + + const type = sortableTypes[index]; + const tmp = sortableTypes[index + increment]; + if (tmp) { + newList[index] = tmp; + newList[index + increment] = type; + } + setSortableTypes(newList); + mutation.mutate({ + ids: newList.map((type) => type.id), + }); + } + return (