Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add song tags #10

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ module.exports = {
"newlines-between": "always",
},
],
"@typescript-eslint/no-unused-vars": [
"error",
{ ignoreRestSiblings: true },
],
},
},

Expand Down
25 changes: 23 additions & 2 deletions app/models/song.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export function getSong({
wallCounts: true,
startingWeightFoot: true,

tags: {
select: {
tag: true,
},
},

...(userId
? {
createdAt: true,
Expand Down Expand Up @@ -59,11 +65,26 @@ type RequiredSongField = Pick<Song, "title" | "artist">;
export function editSong(
songId: Song["id"],
userId: User["id"],
songData: NullableSongFields & RequiredSongField,
songData: NullableSongFields & RequiredSongField & { tags?: string[] },
) {
let tags = {};
if (songData.tags) {
// avoid deleting and reinserting tags all the time, only if they changed.
// also the nested ternary freaks out prisma TS for some reason
tags = {
deleteMany: {},
create: songData.tags?.map((tag) => ({
tag: { connect: { id: parseInt(tag) } },
})),
};
}
return prisma.song.update({
where: { id: songId },
data: { ...songData, updatedById: userId },
data: {
...songData,
updatedById: userId,
tags,
},
});
}

Expand Down
9 changes: 9 additions & 0 deletions app/models/tags.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { prisma } from "~/db.server";

export function getTags() {
return prisma.tag.findMany({
orderBy: {
name: "asc",
},
});
}
91 changes: 88 additions & 3 deletions app/routes/songs.$songId_.edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import invariant from "tiny-invariant";
import { z } from "zod";

import { deleteSong, editSong, getSong } from "~/models/song.server";
import { getTags } from "~/models/tags.server";
import { requireAdmin } from "~/session.server";

const schema = z.object({
Expand All @@ -26,28 +27,48 @@ const schema = z.object({
danceCounts: z.coerce.number().optional(),
wallCounts: z.coerce.number().optional(),
startingWeightFoot: z.string().optional(),
tags: z.array(z.string()).optional(),
});

export const action = async ({ request, params }: LoaderFunctionArgs) => {
invariant(params.songId, "songId not found");
const user = await requireAdmin(request);
const formData = await request.formData();

const { _action, ...payload } = Object.fromEntries(formData);
const { _action, tag, originalTags, ...payload } =
Object.fromEntries(formData);

if (_action === "delete") {
await deleteSong({ id: parseInt(params.songId) });
return redirect("/songs");
}

const tags = formData.getAll("tag");

payload["tags"] = tags as unknown as FormDataEntryValue;

const result = schema.safeParse(payload);

if (typeof originalTags !== "string") {
return json({
payload,
error: { originalTags: "originalTags must be a string" },
});
}

const originalTagsArr = originalTags.split(", ");

if (!result.success) {
return json({
payload,
error: result.error.flatten().fieldErrors,
});
}

if (originalTagsArr.sort().join(",") === tags.sort().join(",")) {
delete result.data.tags;
}

const song = await editSong(parseInt(params.songId), user.id, result.data);
return redirect("/songs/" + song.id);
};
Expand All @@ -62,7 +83,16 @@ export const loader = async ({ params, request }: LoaderFunctionArgs) => {
if (!song) {
throw new Response("Not Found", { status: 404 });
}
return json({ song });
const unfilteredTags = await getTags();
const tags = unfilteredTags.filter((tag) =>
[
"Wednesday Lessons",
"Friday Lessons",
"Early Night",
"Late Night",
].includes(tag.name),
);
return json({ song, tags });
};

export default function SongEditPage() {
Expand All @@ -71,7 +101,7 @@ export default function SongEditPage() {
const [searchParams] = useSearchParams();
const navigate = useNavigate();

const { song } = data;
const { song, tags } = data;
return (
<div className="max-w-[800px] mx-auto">
<Form method="post">
Expand Down Expand Up @@ -210,6 +240,61 @@ export default function SongEditPage() {
defaultValue={song.startingWeightFoot || ""}
/>
</div>
<div className="flex flex-col justify-around">
<label className="cursor-pointer py-2 md:py-0">
<input
type="checkbox"
name="tag"
value={tags.find((tag) => tag.name === "Wednesday Lessons")?.id}
defaultChecked={song.tags?.some(
(tag) => tag.tag.name === "Wednesday Lessons",
)}
/>{" "}
Wednesday Lessons
</label>
<label className="cursor-pointer py-2 md:py-0">
<input
type="checkbox"
name="tag"
value={tags.find((tag) => tag.name === "Friday Lessons")?.id}
defaultChecked={song.tags?.some(
(tag) => tag.tag.name === "Friday Lessons",
)}
/>{" "}
Friday Lessons
</label>
</div>
<div className="flex gap-8">
<label className="cursor-pointer py-2 md:py-0">
<input
type="checkbox"
name="tag"
value={tags.find((tag) => tag.name === "Early Night")?.id}
defaultChecked={song.tags?.some(
(tag) => tag.tag.name === "Early Night",
)}
/>{" "}
Early Night
</label>
<label className="cursor-pointer py-2 md:py-0">
<input
type="checkbox"
name="tag"
value={tags.find((tag) => tag.name === "Late Night")?.id}
defaultChecked={song.tags?.some(
(tag) => tag.tag.name === "Late Night",
)}
/>{" "}
Late Night
</label>
<input
type="text"
name="originalTags"
defaultValue={song.tags?.map((tag) => tag.tag.id).join(", ")}
readOnly
className="hidden"
/>
</div>
</div>
<div className="flex gap-4 mt-8 justify-end">
<button
Expand Down
21 changes: 21 additions & 0 deletions prisma/migrations/20231213033920_add_song_tags/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- CreateTable
CREATE TABLE "SongTag" (
"songId" INTEGER NOT NULL,
"tagId" INTEGER NOT NULL,

PRIMARY KEY ("songId", "tagId"),
CONSTRAINT "SongTag_songId_fkey" FOREIGN KEY ("songId") REFERENCES "Song" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "SongTag_tagId_fkey" FOREIGN KEY ("tagId") REFERENCES "Tag" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Tag" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "Tag_name_key" ON "Tag"("name");

-- InsertData
INSERT INTO "Tag" ("name") VALUES ("Wednesday Lessons"), ("Friday Lessons"), ("Early Night"), ("Late Night")
18 changes: 18 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ model Song {
updatedById String?
AnalyticsSongView AnalyticsSongView[]
PlaybackEvent PlaybackEvent[]
tags SongTag[]
}

model SongTag {
song Song @relation(fields: [songId], references: [id])
songId Int

tag Tag @relation(fields: [tagId], references: [id])
tagId Int

@@id([songId, tagId])
}

model Tag {
id Int @id @default(autoincrement())
name String @unique

songs SongTag[]
}

model PlaybackEvent {
Expand Down