Skip to content

Commit

Permalink
generalize field handling; fields may habe multiple entries now
Browse files Browse the repository at this point in the history
  • Loading branch information
aleneum committed Aug 15, 2023
1 parent 54ffe64 commit a42cbf9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ export class Entry {
const editData: Record<string, FieldValueType> = {};
for (const field of this.fields.values()) {
if (field.edited) {
editData[field.element.fieldName] = field.value;
for (const data of field.getData()) {
editData[data.field] = data.value;
}
field.edited = false;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/fields/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export abstract class FieldBase<T> {
public get value(): T {
return this.entry[this.element.fieldName] as T;
}

public getData(): Array<{ field: string; value: T | null }> {
return [{ field: this.element.fieldName, value: this.value }];
}
}

export abstract class ValueField<
Expand Down
33 changes: 32 additions & 1 deletion src/fields/date.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
import { ValueField } from "./base";

export default class DateField extends ValueField<string> {}
export default class DateField extends ValueField<string> {
private get endDateFieldName(): string {
return `${this.element.uuid}_endDate`;
}

public get date(): string {
return this.value;
}

public get startDate(): string {
return this.value;
}

public get endDate(): string | null {
const val = this.entry[this.endDateFieldName];
if (val === null) {
return null;
}
return val as string;
}

public setEndDate(dateString: string | null): void {
this.entry[this.endDateFieldName] = dateString;
this.edited = true;
}

public getData(): Array<{ field: string; value: string | null }> {
const res = super.getData();
res.push({ field: this.endDateFieldName, value: this.endDate });
return res;
}
}

0 comments on commit a42cbf9

Please sign in to comment.