summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorRaphael <mail@raphaelkabo.com>2024-02-26 15:06:01 +0000
committerGitHub <noreply@github.com>2024-02-26 15:06:01 +0000
commitde688444d167fdb80c6e88b8ba837405ba7651a6 (patch)
tree051fa37a2fe30052254bf79a60bd870667fcc0b7 /src/util
parentafd9fc4477fff90e5db917f350d99c3d01fba2bd (diff)
parent1275280a9e3a31f6080079d564a8fb9e1847db8b (diff)
Merge pull request #135 from lowercasename/rk/public-events
Events and groups optionally visible on front page
Diffstat (limited to 'src/util')
-rw-r--r--src/util/object.ts30
-rw-r--r--src/util/validation.ts31
2 files changed, 57 insertions, 4 deletions
diff --git a/src/util/object.ts b/src/util/object.ts
new file mode 100644
index 0000000..1ecc89b
--- /dev/null
+++ b/src/util/object.ts
@@ -0,0 +1,30 @@
+/**
+ * Simple object check.
+ */
+export function isObject(item: any) {
+ return item && typeof item === "object" && !Array.isArray(item);
+}
+
+/**
+ * Deep merge two objects.
+ */
+export function deepMerge<T>(
+ target: Record<any, any>,
+ ...sources: Record<any, any>[]
+): T {
+ if (!sources.length) return target;
+ const source = sources.shift();
+
+ if (isObject(target) && isObject(source)) {
+ for (const key in source) {
+ if (isObject(source[key])) {
+ if (!target[key]) Object.assign(target, { [key]: {} });
+ deepMerge(target[key], source[key]);
+ } else {
+ Object.assign(target, { [key]: source[key] });
+ }
+ }
+ }
+
+ return deepMerge(target, ...sources) as T;
+}
diff --git a/src/util/validation.ts b/src/util/validation.ts
index 732fbf3..b9a0c8a 100644
--- a/src/util/validation.ts
+++ b/src/util/validation.ts
@@ -5,11 +5,16 @@ type Error = {
field?: string;
};
-type ValidationResponse = {
+type EventValidationResponse = {
data?: ValidatedEventData;
errors?: Error[];
};
+type EventGroupValidationResponse = {
+ data?: ValidatedEventGroupData;
+ errors?: Error[];
+};
+
interface EventData {
eventName: string;
eventLocation: string;
@@ -21,6 +26,7 @@ interface EventData {
imagePath: string;
hostName: string;
creatorEmail: string;
+ publicCheckbox: string;
eventGroupCheckbox: string;
eventGroupID: string;
eventGroupEditToken: string;
@@ -33,11 +39,13 @@ interface EventData {
// EventData without the 'checkbox' fields
export type ValidatedEventData = Omit<
EventData,
+ | "publicCheckbox"
| "eventGroupCheckbox"
| "interactionCheckbox"
| "joinCheckbox"
| "maxAttendeesCheckbox"
> & {
+ publicBoolean: boolean;
eventGroupBoolean: boolean;
interactionBoolean: boolean;
joinBoolean: boolean;
@@ -50,8 +58,13 @@ interface EventGroupData {
eventGroupURL: string;
hostName: string;
creatorEmail: string;
+ publicCheckbox: string;
}
+export type ValidatedEventGroupData = Omit<EventGroupData, "publicCheckbox"> & {
+ publicBoolean: boolean;
+};
+
const validateEmail = (email: string) => {
if (!email || email.length === 0 || typeof email !== "string") {
return false;
@@ -89,9 +102,12 @@ export const validateEventTime = (start: Date, end: Date): Error | boolean => {
return true;
};
-export const validateEventData = (eventData: EventData): ValidationResponse => {
+export const validateEventData = (
+ eventData: EventData,
+): EventValidationResponse => {
const validatedData: ValidatedEventData = {
...eventData,
+ publicBoolean: eventData.publicCheckbox === "true",
eventGroupBoolean: eventData.eventGroupCheckbox === "true",
interactionBoolean: eventData.interactionCheckbox === "true",
joinBoolean: eventData.joinCheckbox === "true",
@@ -186,7 +202,9 @@ export const validateEventData = (eventData: EventData): ValidationResponse => {
};
};
-export const validateGroupData = (groupData: EventGroupData) => {
+export const validateGroupData = (
+ groupData: EventGroupData,
+): EventGroupValidationResponse => {
const errors: Error[] = [];
if (!groupData.eventGroupName) {
errors.push({
@@ -209,8 +227,13 @@ export const validateGroupData = (groupData: EventGroupData) => {
}
}
+ const validatedData: ValidatedEventGroupData = {
+ ...groupData,
+ publicBoolean: groupData.publicCheckbox === "true",
+ };
+
return {
- data: groupData,
+ data: validatedData,
errors: errors,
};
};