This fixes the creation and deletion of calendars to include a color and the proper chosen name.
37 lines
984 B
TypeScript
37 lines
984 B
TypeScript
import { Handlers } from 'fresh/server.ts';
|
|
|
|
import { FreshContextState } from '/lib/types.ts';
|
|
import { Calendar, CalendarModel } from '/lib/models/calendar.ts';
|
|
import { getColorAsHex } from '/lib/utils/calendar.ts';
|
|
|
|
interface Data {}
|
|
|
|
export interface RequestBody {
|
|
name: string;
|
|
}
|
|
|
|
export interface ResponseBody {
|
|
success: boolean;
|
|
newCalendars: Calendar[];
|
|
}
|
|
|
|
export const handler: Handlers<Data, FreshContextState> = {
|
|
async POST(request, context) {
|
|
if (!context.state.user) {
|
|
return new Response('Unauthorized', { status: 401 });
|
|
}
|
|
|
|
const requestBody = await request.clone().json() as RequestBody;
|
|
|
|
if (requestBody.name) {
|
|
await CalendarModel.create(context.state.user.id, requestBody.name, getColorAsHex('bg-gray-700'));
|
|
}
|
|
|
|
const newCalendars = await CalendarModel.list(context.state.user.id);
|
|
|
|
const responseBody: ResponseBody = { success: true, newCalendars };
|
|
|
|
return new Response(JSON.stringify(responseBody));
|
|
},
|
|
};
|