From d67312f825a5cd84aeb9f29ca92baaa9c3939752 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sun, 31 Jul 2022 13:14:30 +0300 Subject: [PATCH] Sort tags before adding to DB --- TODO.md | 7 +++++-- app/constants.ts | 6 ++++++ app/db/models/calendar.server.ts | 2 +- app/routes/calendar/new.tsx | 26 ++++++++++++++++++++------ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/TODO.md b/TODO.md index 2f16bbe9e..3ad7d9054 100644 --- a/TODO.md +++ b/TODO.md @@ -9,9 +9,11 @@ Calendar - [x] Implement tags - [x] Add initial tags.json - [x] Add tags to the seed -- [ ] Tags should be clickable and apply a filter (also many tags) - [ ] Page to add a new event - [ ] Edit page +- [ ] E2E test form controls (adding/removing date, adding/removing tags) etc. +- [ ] E2E test browsing events + event page +- [ ] E2E test adding and editing event - [ ] Add winners page - [ ] Winners on the event info page - [ ] There should be a banner on the list page if you have past tournaments to report @@ -20,9 +22,10 @@ Calendar ## Other +- [ ] Move "BADGE" tag resolving to DB? - [x] Tags selector can remove - [ ] Calendar new title -- [ ] Sort tags so that they are always in same order +- [x] Sort tags so that they are always in same order - [x] Maybe make bracket URL mandatory? - [ ] components render twice on state change? - [ ] If week has no events show some text + don't show time zone text diff --git a/app/constants.ts b/app/constants.ts index 7cfe351b7..79c500097 100644 --- a/app/constants.ts +++ b/app/constants.ts @@ -1,3 +1,6 @@ +import allTags from "~/routes/calendar/tags.json"; +import type { CalendarEventTag } from "./db/types"; + export const TWEET_LENGTH_MAX_LENGTH = 280; export const DISCORD_MESSAGE_MAX_LENGTH = 2000; @@ -12,6 +15,9 @@ export const CALENDAR_EVENT_DESCRIPTION_MAX_LENGTH = DISCORD_MESSAGE_MAX_LENGTH; export const CALENDAR_EVENT_DISCORD_INVITE_CODE_MAX_LENGTH = 50; export const CALENDAR_EVENT_BRACKET_URL_MAX_LENGTH = 100; export const CALENDAR_EVENT_MAX_AMOUNT_OF_DATES = 5; +export const CALENDAR_EVENT_TAGS = Object.keys( + allTags +) as Array; export const PLUS_TIERS = [1, 2, 3]; diff --git a/app/db/models/calendar.server.ts b/app/db/models/calendar.server.ts index db93bd539..da0266c80 100644 --- a/app/db/models/calendar.server.ts +++ b/app/db/models/calendar.server.ts @@ -167,7 +167,7 @@ function addTagArray< const { hasBadge, ...row } = arg; const tags = (row.tags ? row.tags.split(",") : []) as Array; - if (hasBadge) tags.push("BADGE"); + if (hasBadge) tags.unshift("BADGE"); return { ...row, tags }; } diff --git a/app/routes/calendar/new.tsx b/app/routes/calendar/new.tsx index 560d6e03a..2fd707cd2 100644 --- a/app/routes/calendar/new.tsx +++ b/app/routes/calendar/new.tsx @@ -4,6 +4,7 @@ import { Main } from "~/components/Main"; import * as React from "react"; import type { Badge as BadgeType, CalendarEventTag } from "~/db/types"; import { + CALENDAR_EVENT_TAGS, CALENDAR_EVENT_BRACKET_URL_MAX_LENGTH, CALENDAR_EVENT_DESCRIPTION_MAX_LENGTH, CALENDAR_EVENT_DISCORD_INVITE_CODE_MAX_LENGTH, @@ -26,7 +27,6 @@ import { TrashIcon } from "~/components/icons/Trash"; import { Input } from "~/components/Input"; import { FormMessage } from "~/components/FormMessage"; import { useIsMounted } from "~/hooks/useIsMounted"; -import allTags from "./tags.json"; import { Tags } from "./components/Tags"; import { db } from "~/db"; import { requireUser } from "~/modules/auth"; @@ -98,7 +98,13 @@ const newCalendarEventActionSchema = z.object({ tags: z.preprocess( processMany(safeJSONParse, removeDuplicates), z - .array(z.string().refine((val) => Object.keys(allTags).includes(val))) + .array( + z + .string() + .refine((val) => + CALENDAR_EVENT_TAGS.includes(val as CalendarEventTag) + ) + ) .nullable() ), badges: z.preprocess( @@ -120,7 +126,15 @@ export const action: ActionFunction = async ({ request }) => { startTimes: data.dates.map((date) => dateToDatabaseTimestamp(date)), bracketUrl: data.bracketUrl, discordInviteCode: data.discordInviteCode, - tags: data.tags ? data.tags.join(",") : data.tags, + tags: data.tags + ? data.tags + .sort( + (a, b) => + CALENDAR_EVENT_TAGS.indexOf(a as CalendarEventTag) - + CALENDAR_EVENT_TAGS.indexOf(b as CalendarEventTag) + ) + .join(",") + : data.tags, badges: data.badges ?? [], }; if (data.eventToEditId) { @@ -386,9 +400,9 @@ function TagsAdder() { ); const id = React.useId(); - const tagsForSelect = ( - Object.keys(allTags) as Array - ).filter((tag) => !tags.includes(tag) && tag !== "BADGE"); + const tagsForSelect = CALENDAR_EVENT_TAGS.filter( + (tag) => !tags.includes(tag) && tag !== "BADGE" + ); return (