Skip to content

Commit

Permalink
Fix hardcoded years for galleries
Browse files Browse the repository at this point in the history
  • Loading branch information
Allypost committed Sep 20, 2023
1 parent 242db6f commit d9f9407
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
22 changes: 12 additions & 10 deletions src/pages/multimedia/events/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ type Props = ServerSideProps<typeof getServerSideProps>;

const PageMultimediaEventsHome: NextPage<Props> = (props) => {
const forYear = props.year ?? new Date().getFullYear();
const [galleryEventYears] = api.gallery.getYearsWithEvents.useSuspenseQuery(
undefined,
{
cacheTime: 60 * 1000,
},
);

return (
<>
Expand All @@ -146,16 +152,12 @@ const PageMultimediaEventsHome: NextPage<Props> = (props) => {
label="Godina"
name="year"
queryKey={QUERY_KEY_FOR.year}
options={[
{
label: "2023",
value: "2023",
},
{
label: "2022",
value: "2022",
},
]}
options={galleryEventYears.map((year) => {
return {
label: String(year),
value: String(year),
};
})}
/>
</div>
<LoadingArea
Expand Down
23 changes: 17 additions & 6 deletions src/server/api/routers/gallery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { z } from "zod";

import { prisma } from "~/server/db";
import { utcDate } from "~/utils/date";
import { dbFieldsOfModel, dbModelFor } from "~/utils/prisma";

import { createTRPCRouter, publicProcedure } from "../trpc";

const GalleryAlbumModel = dbModelFor("GalleryAlbum");
const GalleryAlbumDbFields = dbFieldsOfModel(GalleryAlbumModel);

export const galleryRouter = createTRPCRouter({
getGalleries: publicProcedure
.input(
Expand Down Expand Up @@ -57,12 +61,7 @@ export const galleryRouter = createTRPCRouter({
.input(
z.object({
eventType: z.enum(["live", "foto"]),
forYear: z
.number()
.int()
.min(2000)
.max(2100)
.default(new Date().getFullYear()),
forYear: z.number().int().default(new Date().getFullYear()),
filter: z
.object({
titleOrPhotographerName: z.string().transform((v) => v.trim()),
Expand Down Expand Up @@ -169,4 +168,16 @@ export const galleryRouter = createTRPCRouter({
groupedByMonth: entries,
};
}),

getYearsWithEvents: publicProcedure.query(async () => {
const eventYears: { date: number }[] = await prisma.$queryRawUnsafe(
`select distinct date_part('year', ${
GalleryAlbumDbFields.dateOfEvent
}) as date from "${
GalleryAlbumModel.dbName ?? ""
}" order by date_part('year', ${GalleryAlbumDbFields.dateOfEvent}) desc`,
);

return eventYears.map((e) => e.date);
}),
});

0 comments on commit d9f9407

Please sign in to comment.