Fix creating and deleting calendars
This fixes the creation and deletion of calendars to include a color and the proper chosen name.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
services:
|
services:
|
||||||
website:
|
website:
|
||||||
image: ghcr.io/bewcloud/bewcloud:v2.5.0
|
image: ghcr.io/bewcloud/bewcloud:v2.5.1
|
||||||
restart: always
|
restart: always
|
||||||
ports:
|
ports:
|
||||||
- 127.0.0.1:8000:8000
|
- 127.0.0.1:8000:8000
|
||||||
|
|||||||
@@ -37,6 +37,11 @@ export default function Calendars({ initialCalendars }: CalendarsProps) {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify(requestBody),
|
body: JSON.stringify(requestBody),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to add calendar! ${response.statusText} ${await response.text()}`);
|
||||||
|
}
|
||||||
|
|
||||||
const result = await response.json() as AddResponseBody;
|
const result = await response.json() as AddResponseBody;
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
@@ -65,6 +70,11 @@ export default function Calendars({ initialCalendars }: CalendarsProps) {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify(requestBody),
|
body: JSON.stringify(requestBody),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to delete calendar! ${response.statusText} ${await response.text()}`);
|
||||||
|
}
|
||||||
|
|
||||||
const result = await response.json() as DeleteResponseBody;
|
const result = await response.json() as DeleteResponseBody;
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
|
|||||||
@@ -120,6 +120,7 @@ export class CalendarModel {
|
|||||||
static async create(
|
static async create(
|
||||||
userId: string,
|
userId: string,
|
||||||
name: string,
|
name: string,
|
||||||
|
color: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const calendarId = crypto.randomUUID();
|
const calendarId = crypto.randomUUID();
|
||||||
const calendarUrl = `${calendarConfig.calDavUrl}/${userId}/${calendarId}/`;
|
const calendarUrl = `${calendarConfig.calDavUrl}/${userId}/${calendarId}/`;
|
||||||
@@ -129,7 +130,8 @@ export class CalendarModel {
|
|||||||
await client.makeCalendar({
|
await client.makeCalendar({
|
||||||
url: calendarUrl,
|
url: calendarUrl,
|
||||||
props: {
|
props: {
|
||||||
displayName: name,
|
displayname: name,
|
||||||
|
calendarColor: color,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -163,8 +165,10 @@ export class CalendarModel {
|
|||||||
|
|
||||||
static async delete(
|
static async delete(
|
||||||
userId: string,
|
userId: string,
|
||||||
calendarUrl: string,
|
calendarId: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
|
const calendarUrl = `${calendarConfig.calDavUrl}/${userId}/${calendarId}/`;
|
||||||
|
|
||||||
const client = await getClient(userId);
|
const client = await getClient(userId);
|
||||||
|
|
||||||
await client.deleteObject({
|
await client.deleteObject({
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ export const CALENDAR_COLOR_OPTIONS = [
|
|||||||
'bg-fuchsia-700',
|
'bg-fuchsia-700',
|
||||||
'bg-pink-800',
|
'bg-pink-800',
|
||||||
'bg-rose-700',
|
'bg-rose-700',
|
||||||
|
'bg-gray-700',
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
const CALENDAR_COLOR_OPTIONS_HEX = [
|
const CALENDAR_COLOR_OPTIONS_HEX = [
|
||||||
@@ -44,9 +45,12 @@ const CALENDAR_COLOR_OPTIONS_HEX = [
|
|||||||
'#9D21B1',
|
'#9D21B1',
|
||||||
'#9C174D',
|
'#9C174D',
|
||||||
'#BC133D',
|
'#BC133D',
|
||||||
|
'#384354',
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
export function getColorAsHex(calendarColor: string) {
|
export function getColorAsHex(
|
||||||
|
calendarColor: (typeof CALENDAR_COLOR_OPTIONS)[number],
|
||||||
|
): (typeof CALENDAR_COLOR_OPTIONS_HEX)[number] {
|
||||||
const colorIndex = CALENDAR_COLOR_OPTIONS.findIndex((color) => color === calendarColor);
|
const colorIndex = CALENDAR_COLOR_OPTIONS.findIndex((color) => color === calendarColor);
|
||||||
|
|
||||||
return CALENDAR_COLOR_OPTIONS_HEX[colorIndex] || '#384354';
|
return CALENDAR_COLOR_OPTIONS_HEX[colorIndex] || '#384354';
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { assertMatch } from 'std/assert/assert_match.ts';
|
|||||||
|
|
||||||
import { Calendar, CalendarEvent } from '/lib/models/calendar.ts';
|
import { Calendar, CalendarEvent } from '/lib/models/calendar.ts';
|
||||||
import {
|
import {
|
||||||
|
CALENDAR_COLOR_OPTIONS,
|
||||||
convertRRuleToWords,
|
convertRRuleToWords,
|
||||||
generateVCalendar,
|
generateVCalendar,
|
||||||
generateVEvent,
|
generateVEvent,
|
||||||
@@ -29,7 +30,7 @@ Deno.test('that getColorAsHex works', () => {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for (const test of tests) {
|
for (const test of tests) {
|
||||||
const output = getColorAsHex(test.input);
|
const output = getColorAsHex(test.input as typeof CALENDAR_COLOR_OPTIONS[number]);
|
||||||
assertEquals(output, test.expected);
|
assertEquals(output, test.expected);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { Handlers } from 'fresh/server.ts';
|
|||||||
|
|
||||||
import { FreshContextState } from '/lib/types.ts';
|
import { FreshContextState } from '/lib/types.ts';
|
||||||
import { Calendar, CalendarModel } from '/lib/models/calendar.ts';
|
import { Calendar, CalendarModel } from '/lib/models/calendar.ts';
|
||||||
|
import { getColorAsHex } from '/lib/utils/calendar.ts';
|
||||||
|
|
||||||
interface Data {}
|
interface Data {}
|
||||||
|
|
||||||
@@ -23,7 +24,7 @@ export const handler: Handlers<Data, FreshContextState> = {
|
|||||||
const requestBody = await request.clone().json() as RequestBody;
|
const requestBody = await request.clone().json() as RequestBody;
|
||||||
|
|
||||||
if (requestBody.name) {
|
if (requestBody.name) {
|
||||||
await CalendarModel.create(context.state.user.id, 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 newCalendars = await CalendarModel.list(context.state.user.id);
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { Handlers } from 'fresh/server.ts';
|
|||||||
import { FreshContextState } from '/lib/types.ts';
|
import { FreshContextState } from '/lib/types.ts';
|
||||||
import { Calendar, CalendarModel } from '/lib/models/calendar.ts';
|
import { Calendar, CalendarModel } from '/lib/models/calendar.ts';
|
||||||
import { UserModel } from '/lib/models/user.ts';
|
import { UserModel } from '/lib/models/user.ts';
|
||||||
import { getColorAsHex } from '/lib/utils/calendar.ts';
|
import { CALENDAR_COLOR_OPTIONS, getColorAsHex } from '/lib/utils/calendar.ts';
|
||||||
|
|
||||||
interface Data {}
|
interface Data {}
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ export const handler: Handlers<Data, FreshContextState> = {
|
|||||||
calendar.displayName = requestBody.name;
|
calendar.displayName = requestBody.name;
|
||||||
calendar.calendarColor = requestBody.color?.startsWith('#')
|
calendar.calendarColor = requestBody.color?.startsWith('#')
|
||||||
? requestBody.color
|
? requestBody.color
|
||||||
: getColorAsHex(requestBody.color || 'bg-gray-700');
|
: getColorAsHex((requestBody.color || 'bg-gray-700') as typeof CALENDAR_COLOR_OPTIONS[number]);
|
||||||
|
|
||||||
await CalendarModel.update(context.state.user.id, calendar.url, calendar.displayName, calendar.calendarColor);
|
await CalendarModel.update(context.state.user.id, calendar.url, calendar.displayName, calendar.calendarColor);
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { FreshContextState } from '/lib/types.ts';
|
|||||||
import { Calendar, CalendarEvent, CalendarEventModel, CalendarModel } from '/lib/models/calendar.ts';
|
import { Calendar, CalendarEvent, CalendarEventModel, CalendarModel } from '/lib/models/calendar.ts';
|
||||||
import CalendarWrapper from '/islands/calendar/CalendarWrapper.tsx';
|
import CalendarWrapper from '/islands/calendar/CalendarWrapper.tsx';
|
||||||
import { AppConfig } from '/lib/config.ts';
|
import { AppConfig } from '/lib/config.ts';
|
||||||
import { getDateRangeForCalendarView } from '/lib/utils/calendar.ts';
|
import { getColorAsHex, getDateRangeForCalendarView } from '/lib/utils/calendar.ts';
|
||||||
|
|
||||||
interface Data {
|
interface Data {
|
||||||
userCalendars: Calendar[];
|
userCalendars: Calendar[];
|
||||||
@@ -42,7 +42,7 @@ export const handler: Handlers<Data, FreshContextState> = {
|
|||||||
|
|
||||||
// Create default calendar if none exists
|
// Create default calendar if none exists
|
||||||
if (userCalendars.length === 0) {
|
if (userCalendars.length === 0) {
|
||||||
await CalendarModel.create(userId, 'Calendar');
|
await CalendarModel.create(userId, 'Calendar', getColorAsHex('bg-red-700'));
|
||||||
|
|
||||||
userCalendars = await CalendarModel.list(userId);
|
userCalendars = await CalendarModel.list(userId);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user