Skip to content

Commit

Permalink
create events
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoishin committed Jan 10, 2024
1 parent 1064609 commit 813911f
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 21 deletions.
12 changes: 9 additions & 3 deletions projects/admin/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Admin, ListGuesser, Resource } from "react-admin";
import { Admin, Resource } from "react-admin";
import { dataProvider } from "./data-provider/data-provider";
import { UserEdit, UserList } from "./users";
import { UserEdit, UserList } from "./resources/users";
import { LoginPage } from "./login";
import { authProvider } from "./auth-provider";
import { EventCreate, EventEdit, EventList } from "./resources/events";

export const App = () => {
return (
Expand All @@ -17,7 +18,12 @@ export const App = () => {
edit={UserEdit}
recordRepresentation="email"
/>
<Resource name="events" list={ListGuesser} />
<Resource
name="events"
create={EventCreate}
list={EventList}
edit={EventEdit}
/>
</Admin>
);
};
4 changes: 4 additions & 0 deletions projects/admin/src/auth-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export const authProvider: AuthProvider = {

logout: async () => {
try {
const signedIn = await trpc.validateSession.query();
if (!signedIn) {
return;
}
await trpc.authentication.signOut.mutate();
} catch (error) {
console.error(error);
Expand Down
5 changes: 3 additions & 2 deletions projects/admin/src/data-provider/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { z } from "zod";

const createEventSchema = z.object({
name: z.string(),
startsAt: z.string().datetime(),
endsAt: z.string().datetime(),
startsAt: z.date().transform((date) => date.toISOString()),
endsAt: z.date().transform((date) => date.toISOString()),
marathonTypes: z.array(z.enum(["ONLINE", "ONSITE"])),
});

export const create = async (
Expand Down
16 changes: 14 additions & 2 deletions projects/admin/src/data-provider/get-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,29 @@ export const getList = async (
resource: string,
params: GetListParams,
): Promise<GetListResult> => {
const query = {
const input = {
order: lowercase(params.sort.order),
orderBy: params.sort.field,
skip: (params.pagination.page - 1) * params.pagination.perPage,
take: params.pagination.perPage,
};
switch (resource) {
case "users": {
const res = await trpc.admin.users.list.query(query);
const res = await trpc.admin.users.list.query(input);
return { data: res.data, total: res.count };
}
case "events": {
const res = await trpc.admin.events.list.query(input);
return {
data: res.data.map((item) => ({
...item,
marathonTypes: item.eventMarathonTypes.map((type) => ({
name: type.marathonType,
})),
})),
total: res.count,
};
}
default:
throw new Error(`unknown resource ${resource}`);
}
Expand Down
11 changes: 11 additions & 0 deletions projects/admin/src/data-provider/get-one.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ export const getOne = async (
const data = await trpc.admin.users.get.query({ id });
return { data };
}
case "events": {
const data = await trpc.admin.events.get.query({ id });
return {
data: {
...data,
marathonTypes: data.eventMarathonTypes.map(
(type) => type.marathonType,
),
},
};
}
default:
throw new Error(`unknown resource ${resource}`);
}
Expand Down
71 changes: 71 additions & 0 deletions projects/admin/src/resources/events.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
Create,
DateTimeInput,
SimpleForm,
TextInput,
required,
BooleanField,
Datagrid,
DateField,
List,
TextField,
CheckboxGroupInput,
ArrayField,
SingleFieldList,
ChipField,
Edit,
BooleanInput,
} from "react-admin";

export const EventCreate = () => {
return (
<Create>
<SimpleForm>
<TextInput source="name" validate={[required()]} />
<DateTimeInput source="startsAt" validate={[required()]} />
<DateTimeInput source="endsAt" validate={[required()]} />
<CheckboxGroupInput
source="marathonTypes"
choices={[
{ id: "ONSITE", name: "Onsite" },
{ id: "ONLINE", name: "Online" },
]}
/>
</SimpleForm>
</Create>
);
};

export const EventList = () => (
<List>
<Datagrid rowClick="edit">
<TextField source="name" />
<DateField showTime source="startsAt" />
<DateField showTime source="endsAt" />
<ArrayField source="marathonTypes">
<SingleFieldList linkType={false}>
<ChipField source="name" />
</SingleFieldList>
</ArrayField>
<BooleanField source="published" />
</Datagrid>
</List>
);

export const EventEdit = () => (
<Edit>
<SimpleForm>
<TextInput source="name" validate={[required()]} />
<DateTimeInput source="startsAt" validate={[required()]} />
<DateTimeInput source="endsAt" validate={[required()]} />
<CheckboxGroupInput
source="marathonTypes"
choices={[
{ id: "ONSITE", name: "Onsite" },
{ id: "ONLINE", name: "Online" },
]}
/>
<BooleanInput source="published" />
</SimpleForm>
</Edit>
);
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ALTER COLUMN "username" DROP NOT NULL;
18 changes: 17 additions & 1 deletion projects/server/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrismaClient, Role } from "@prisma/client";
import { MarathonType, PrismaClient, Role } from "@prisma/client";

const prisma = new PrismaClient();

Expand All @@ -20,3 +20,19 @@ await prisma.user.create({
username: "dev-normal",
},
});

await prisma.event.create({
data: {
name: "RTA in Japan Sample 1995",
startsAt: new Date("1995-08-08T12:00:00+0900"),
endsAt: new Date("1995-08-15T18:00:00+0900"),
eventMarathonTypes: {
createMany: {
data: [
{ marathonType: MarathonType.ONLINE },
{ marathonType: MarathonType.ONSITE },
],
},
},
},
});
34 changes: 21 additions & 13 deletions projects/server/src/routes/admin/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import { adminProcedure, router } from "../../trpc.js";
import { listSchema } from "./utils.js";
import { MarathonType } from "@prisma/client";

const createEventSchema = z.object({
name: z.string(),
startsAt: z.string().datetime(),
endsAt: z.string().datetime(),
marathonTypes: z.array(z.enum([MarathonType.ONLINE, MarathonType.ONSITE])),
});

export const eventsRouter = router({
list: adminProcedure.input(listSchema).query(async ({ input }) => {
const data = await prisma.event.findMany({
Expand All @@ -15,6 +22,9 @@ export const eventsRouter = router({
endsAt: input.orderBy === "endsAt" ? input.order : undefined,
published: input.orderBy === "published" ? input.order : undefined,
},
include: {
eventMarathonTypes: true,
},
});
const count = await prisma.event.count();
return { data, count };
Expand All @@ -26,26 +36,17 @@ export const eventsRouter = router({
where: {
id: input.id,
},
include: {
eventMarathonTypes: true,
},
});
if (!event) {
throw new Error("event not found");
}
return event;
}),
create: adminProcedure
.input(
z.object({
name: z.string(),
startsAt: z
.string()
.datetime()
.transform((date) => new Date(date)),
endsAt: z
.string()
.datetime()
.transform((date) => new Date(date)),
}),
)
.input(createEventSchema)
.mutation(async ({ input }) => {
const event = await prisma.event.create({
data: {
Expand All @@ -54,6 +55,12 @@ export const eventsRouter = router({
endsAt: input.endsAt,
},
});
await prisma.eventMarathonType.createMany({
data: input.marathonTypes.map((type) => ({
eventId: event.id,
marathonType: type,
})),
});
return event;
}),
update: adminProcedure
Expand Down Expand Up @@ -83,6 +90,7 @@ export const eventsRouter = router({
published: input.data.published,
},
});
// TODO: update event types
return event;
}),
delete: adminProcedure
Expand Down

0 comments on commit 813911f

Please sign in to comment.