mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-20 14:39:06 +00:00
refactor: simplify custom field shape
This commit is contained in:
committed by
Carlos Valente
parent
cf14b0b427
commit
c4136a0ec7
@@ -155,7 +155,7 @@ export const useEventAction = () => {
|
|||||||
|
|
||||||
const updateCustomField = useCallback(
|
const updateCustomField = useCallback(
|
||||||
async (eventId: string, field: string, value: string) => {
|
async (eventId: string, field: string, value: string) => {
|
||||||
updateEvent({ id: eventId, custom: { [field]: { value } } });
|
updateEvent({ id: eventId, custom: { [field]: value } });
|
||||||
},
|
},
|
||||||
[updateEvent],
|
[updateEvent],
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ describe('cloneEvent()', () => {
|
|||||||
timeWarning: 120000,
|
timeWarning: 120000,
|
||||||
timeDanger: 60000,
|
timeDanger: 60000,
|
||||||
custom: {
|
custom: {
|
||||||
lighting: { value: '3' },
|
lighting: '3',
|
||||||
} as EventCustomFields,
|
} as EventCustomFields,
|
||||||
} as OntimeEvent;
|
} as OntimeEvent;
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -101,7 +101,7 @@ export default function PreviewRundown(props: PreviewRundownProps) {
|
|||||||
fieldHeaders.map((field) => {
|
fieldHeaders.map((field) => {
|
||||||
let value = '';
|
let value = '';
|
||||||
if (field in event.custom) {
|
if (field in event.custom) {
|
||||||
value = event.custom[field].value;
|
value = event.custom[field];
|
||||||
}
|
}
|
||||||
return <td key={field}>{value}</td>;
|
return <td key={field}>{value}</td>;
|
||||||
})}
|
})}
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ export default function CuesheetWrapper() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const previousValue = event.custom[accessor]?.value;
|
const previousValue = event.custom[accessor];
|
||||||
|
|
||||||
if (previousValue === payload) {
|
if (previousValue === payload) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ function MakeCustomField({ row, column, table }: CellContext<OntimeRundownEntry,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// events dont necessarily contain all custom fields
|
// events dont necessarily contain all custom fields
|
||||||
const initialValue = event.custom[column.id]?.value ?? '';
|
const initialValue = event.custom[column.id] ?? '';
|
||||||
|
|
||||||
return <EditableCell value={initialValue} handleUpdate={update} />;
|
return <EditableCell value={initialValue} handleUpdate={update} />;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ export const makeTable = (headerData: ProjectData, rundown: OntimeRundown, custo
|
|||||||
// for custom fields, we need to extract the value from the custom object
|
// for custom fields, we need to extract the value from the custom object
|
||||||
if (field.startsWith('custom-')) {
|
if (field.startsWith('custom-')) {
|
||||||
const fieldLabel = field.split('custom-')[1];
|
const fieldLabel = field.split('custom-')[1];
|
||||||
const value = entry.custom[fieldLabel]?.value;
|
const value = entry.custom[fieldLabel];
|
||||||
row.push(parseField(fieldLabel, value));
|
row.push(parseField(fieldLabel, value));
|
||||||
} else {
|
} else {
|
||||||
// @ts-expect-error -- it is ok, we will just not have the data for other fields
|
// @ts-expect-error -- it is ok, we will just not have the data for other fields
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ export default function Operator() {
|
|||||||
|
|
||||||
const mainField = main ? entry?.[main] || entry.title : entry.title;
|
const mainField = main ? entry?.[main] || entry.title : entry.title;
|
||||||
const secondaryField = getPropertyValue(entry, secondary) ?? '';
|
const secondaryField = getPropertyValue(entry, secondary) ?? '';
|
||||||
const subscribedData = entry.custom[subscribe]?.value;
|
const subscribedData = entry.custom[subscribe];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<OperatorEvent
|
<OperatorEvent
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ export default function EventEditor() {
|
|||||||
(field: EditorUpdateFields, value: string) => {
|
(field: EditorUpdateFields, value: string) => {
|
||||||
if (field.startsWith('custom-')) {
|
if (field.startsWith('custom-')) {
|
||||||
const fieldLabel = field.split('custom-')[1];
|
const fieldLabel = field.split('custom-')[1];
|
||||||
updateEvent({ id: event?.id, custom: { [fieldLabel]: { value } } });
|
updateEvent({ id: event?.id, custom: { [fieldLabel]: value } });
|
||||||
} else {
|
} else {
|
||||||
updateEvent({ id: event?.id, [field]: value });
|
updateEvent({ id: event?.id, [field]: value });
|
||||||
}
|
}
|
||||||
@@ -111,7 +111,7 @@ export default function EventEditor() {
|
|||||||
{Object.keys(customFields).map((label) => {
|
{Object.keys(customFields).map((label) => {
|
||||||
const key = `${event.id}-${label}`;
|
const key = `${event.id}-${label}`;
|
||||||
const fieldName = `custom-${label}`;
|
const fieldName = `custom-${label}`;
|
||||||
const initialValue = event.custom[label]?.value ?? '';
|
const initialValue = event.custom[label] ?? '';
|
||||||
const { backgroundColor, color } = getAccessibleColour(customFields[label].colour);
|
const { backgroundColor, color } = getAccessibleColour(customFields[label].colour);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ export function getPropertyValue(event: OntimeEvent | null, property: MaybeStrin
|
|||||||
|
|
||||||
if (property.startsWith('custom-')) {
|
if (property.startsWith('custom-')) {
|
||||||
const field = property.split('custom-')[1];
|
const field = property.split('custom-')[1];
|
||||||
return event.custom?.[field]?.value;
|
return event.custom?.[field];
|
||||||
}
|
}
|
||||||
|
|
||||||
return event[property as keyof OntimeEvent] as string;
|
return event[property as keyof OntimeEvent] as string;
|
||||||
|
|||||||
@@ -304,15 +304,15 @@ describe('generate()', () => {
|
|||||||
type: SupportedEvent.Event,
|
type: SupportedEvent.Event,
|
||||||
id: '1',
|
id: '1',
|
||||||
custom: {
|
custom: {
|
||||||
lighting: { value: 'event 1 lx' },
|
lighting: 'event 1 lx',
|
||||||
} as EventCustomFields,
|
} as EventCustomFields,
|
||||||
} as OntimeEvent,
|
} as OntimeEvent,
|
||||||
{
|
{
|
||||||
type: SupportedEvent.Event,
|
type: SupportedEvent.Event,
|
||||||
id: '2',
|
id: '2',
|
||||||
custom: {
|
custom: {
|
||||||
lighting: { value: 'event 2 lx' },
|
lighting: 'event 2 lx',
|
||||||
sound: { value: 'event 2 sound' },
|
sound: 'event 2 sound',
|
||||||
} as EventCustomFields,
|
} as EventCustomFields,
|
||||||
} as OntimeEvent,
|
} as OntimeEvent,
|
||||||
];
|
];
|
||||||
@@ -322,10 +322,10 @@ describe('generate()', () => {
|
|||||||
lighting: ['1', '2'],
|
lighting: ['1', '2'],
|
||||||
sound: ['2'],
|
sound: ['2'],
|
||||||
});
|
});
|
||||||
expect((initResult.rundown['1'] as OntimeEvent).custom).toMatchObject({ lighting: { value: 'event 1 lx' } });
|
expect((initResult.rundown['1'] as OntimeEvent).custom).toMatchObject({ lighting: 'event 1 lx' });
|
||||||
expect((initResult.rundown['2'] as OntimeEvent).custom).toMatchObject({
|
expect((initResult.rundown['2'] as OntimeEvent).custom).toMatchObject({
|
||||||
lighting: { value: 'event 2 lx' },
|
lighting: 'event 2 lx',
|
||||||
sound: { value: 'event 2 sound' },
|
sound: 'event 2 sound',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ describe('handleCustomField()', () => {
|
|||||||
timeStart: 0,
|
timeStart: 0,
|
||||||
linkStart: '1',
|
linkStart: '1',
|
||||||
custom: {
|
custom: {
|
||||||
lighting: { value: 'on' },
|
lighting: 'on',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const assignedCustomFields = {};
|
const assignedCustomFields = {};
|
||||||
@@ -114,7 +114,7 @@ describe('handleCustomField()', () => {
|
|||||||
expect(result).toBeUndefined();
|
expect(result).toBeUndefined();
|
||||||
expect(assignedCustomFields).toStrictEqual({ lighting: ['2'] });
|
expect(assignedCustomFields).toStrictEqual({ lighting: ['2'] });
|
||||||
expect(event.custom).toStrictEqual({
|
expect(event.custom).toStrictEqual({
|
||||||
lighting: { value: 'on' },
|
lighting: 'on',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -143,7 +143,7 @@ describe('handleCustomField()', () => {
|
|||||||
timeStart: 0,
|
timeStart: 0,
|
||||||
linkStart: '1',
|
linkStart: '1',
|
||||||
custom: {
|
custom: {
|
||||||
sound: { value: 'on' },
|
sound: 'on',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const assignedCustomFields = {};
|
const assignedCustomFields = {};
|
||||||
@@ -152,7 +152,7 @@ describe('handleCustomField()', () => {
|
|||||||
expect(result).toBeUndefined();
|
expect(result).toBeUndefined();
|
||||||
expect(assignedCustomFields).toStrictEqual({ video: ['2'] });
|
expect(assignedCustomFields).toStrictEqual({ video: ['2'] });
|
||||||
expect(event.custom).toStrictEqual({
|
expect(event.custom).toStrictEqual({
|
||||||
video: { value: 'on' },
|
video: 'on',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -179,8 +179,8 @@ describe('handleCustomField()', () => {
|
|||||||
type: SupportedEvent.Event,
|
type: SupportedEvent.Event,
|
||||||
id: 'event1',
|
id: 'event1',
|
||||||
custom: {
|
custom: {
|
||||||
field1: { value: 'value1' },
|
field1: 'value1',
|
||||||
field2: { value: 'value2' },
|
field2: 'value2',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -189,11 +189,11 @@ describe('handleCustomField()', () => {
|
|||||||
handleCustomField(customFields, customFieldChangelog, mutableEvent, assignedCustomFields);
|
handleCustomField(customFields, customFieldChangelog, mutableEvent, assignedCustomFields);
|
||||||
|
|
||||||
// Check that field1 has been renamed to newField1 and the value reassigned
|
// Check that field1 has been renamed to newField1 and the value reassigned
|
||||||
expect(mutableEvent.custom['newField1']).toStrictEqual({ value: 'value1' });
|
expect(mutableEvent.custom['newField1']).toStrictEqual('value1');
|
||||||
expect(mutableEvent.custom['field1']).toBeUndefined();
|
expect(mutableEvent.custom['field1']).toBeUndefined();
|
||||||
|
|
||||||
// Check that field2 has been processed
|
// Check that field2 has been processed
|
||||||
expect(mutableEvent.custom['field2']).toStrictEqual({ value: 'value2' });
|
expect(mutableEvent.custom['field2']).toStrictEqual('value2');
|
||||||
|
|
||||||
// Check that assignedCustomFields has been updated correctly
|
// Check that assignedCustomFields has been updated correctly
|
||||||
expect(assignedCustomFields).toStrictEqual({
|
expect(assignedCustomFields).toStrictEqual({
|
||||||
@@ -232,7 +232,7 @@ describe('isDataStale()', () => {
|
|||||||
timeWarning: 1,
|
timeWarning: 1,
|
||||||
timeDanger: 2,
|
timeDanger: 2,
|
||||||
custom: {
|
custom: {
|
||||||
lighting: { value: '3' },
|
lighting: '3',
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
).toBe(false);
|
).toBe(false);
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ export function handleCustomField(
|
|||||||
const oldData = mutableEvent.custom[field];
|
const oldData = mutableEvent.custom[field];
|
||||||
const newLabel = customFieldChangelog[field];
|
const newLabel = customFieldChangelog[field];
|
||||||
|
|
||||||
mutableEvent.custom[newLabel] = { ...oldData };
|
mutableEvent.custom[newLabel] = oldData;
|
||||||
delete mutableEvent.custom[field];
|
delete mutableEvent.custom[field];
|
||||||
addToCustomAssignment(newLabel, mutableEvent.id, assignedCustomFields);
|
addToCustomAssignment(newLabel, mutableEvent.id, assignedCustomFields);
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -883,16 +883,16 @@ describe('parseExcel()', () => {
|
|||||||
skip: false,
|
skip: false,
|
||||||
note: 'Ballyhoo',
|
note: 'Ballyhoo',
|
||||||
custom: {
|
custom: {
|
||||||
user0: { value: 'a0' },
|
user0: 'a0',
|
||||||
user1: { value: 'a1' },
|
user1: 'a1',
|
||||||
user2: { value: 'a2' },
|
user2: 'a2',
|
||||||
user3: { value: 'a3' },
|
user3: 'a3',
|
||||||
user4: { value: 'a4' },
|
user4: 'a4',
|
||||||
user5: { value: 'a5' },
|
user5: 'a5',
|
||||||
user6: { value: 'a6' },
|
user6: 'a6',
|
||||||
user7: { value: 'a7' },
|
user7: 'a7',
|
||||||
user8: { value: 'a8' },
|
user8: 'a8',
|
||||||
user9: { value: 'a9' },
|
user9: 'a9',
|
||||||
},
|
},
|
||||||
colour: 'red',
|
colour: 'red',
|
||||||
type: 'event',
|
type: 'event',
|
||||||
@@ -908,8 +908,8 @@ describe('parseExcel()', () => {
|
|||||||
skip: true,
|
skip: true,
|
||||||
note: 'Rainbow chase',
|
note: 'Rainbow chase',
|
||||||
custom: {
|
custom: {
|
||||||
user0: { value: 'b0' },
|
user0: 'b0',
|
||||||
user5: { value: 'b5' },
|
user5: 'b5',
|
||||||
},
|
},
|
||||||
colour: '#F00',
|
colour: '#F00',
|
||||||
type: 'event',
|
type: 'event',
|
||||||
|
|||||||
@@ -214,7 +214,7 @@ export const parseExcel = (excelData: unknown[][], options?: Partial<ImportMap>)
|
|||||||
} else if (j in customFieldIndexes) {
|
} else if (j in customFieldIndexes) {
|
||||||
const importKey = customFieldIndexes[j];
|
const importKey = customFieldIndexes[j];
|
||||||
const ontimeKey = customFieldImportKeys[importKey];
|
const ontimeKey = customFieldImportKeys[importKey];
|
||||||
eventCustomFields[ontimeKey] = { value: makeString(column, '') };
|
eventCustomFields[ontimeKey] = makeString(column, '');
|
||||||
} else {
|
} else {
|
||||||
// 2. if there is no flag, lets see if we know the field type
|
// 2. if there is no flag, lets see if we know the field type
|
||||||
if (typeof column === 'string') {
|
if (typeof column === 'string') {
|
||||||
|
|||||||
@@ -7,4 +7,4 @@ export type CustomField = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type CustomFields = Record<CustomFieldLabel, CustomField>;
|
export type CustomFields = Record<CustomFieldLabel, CustomField>;
|
||||||
export type EventCustomFields = Record<CustomFieldLabel, { value: string }>;
|
export type EventCustomFields = Record<CustomFieldLabel, string>;
|
||||||
|
|||||||
Reference in New Issue
Block a user