Skip to content

Commit

Permalink
Edit control codes
Browse files Browse the repository at this point in the history
Fixes #10
  • Loading branch information
perliedman committed Aug 22, 2022
1 parent d09f76f commit b7fd9e6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 29 deletions.
13 changes: 12 additions & 1 deletion src/ControlDescriptionSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function ControlDescriptionSheet({
onChangeEventName,
onChangeCourseName,
onChangeDescription,
onChangeControlCode,
}) {
const containerRef = useRef();
const [descriptionSelector, setDescriptionSelector] = useState();
Expand Down Expand Up @@ -59,7 +60,17 @@ export default function ControlDescriptionSheet({
<DescriptionSymbol symbol="start" />
)}
</td>
<td>{c.code}</td>
<td>
{" "}
<input
type="text"
value={c.code}
className="border-0 p-0 w-full text-center focus:outline-none"
onChange={(e) =>
onChangeControlCode(c.id, Number(e.target.value))
}
/>
</td>
{["C", "D", "E", "F", "G", "H"].map((column, colIndex) => (
<td
key={column}
Expand Down
7 changes: 6 additions & 1 deletion src/Courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default function Courses() {
setPrintScale,
setPrintArea,
setControlDescription,
setControlCode,
makeNewEvent,
newCourse,
} = useEvent(getCourses, shallow);
Expand Down Expand Up @@ -64,6 +65,9 @@ export default function Courses() {
onChangeDescription={(controlId, description) =>
setControlDescription(controlId, description)
}
onChangeControlCode={(controlId, code) =>
setControlCode(controlId, code)
}
/>
<Section title="Options" level={2}>
<CourseOptions
Expand Down Expand Up @@ -166,7 +170,7 @@ function getCourses({
setPrintScale,
setPrintArea,
},
control: { setDescription: setControlDescription },
control: { setDescription: setControlDescription, setCode: setControlCode },
},
}) {
return {
Expand All @@ -179,6 +183,7 @@ function getCourses({
setEventName,
setCourseName,
setControlDescription,
setControlCode,
makeNewEvent,
newCourse,
setPrintScale,
Expand Down
15 changes: 15 additions & 0 deletions src/models/event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,19 @@ describe("event", () => {
expect(loadedEvent.idGenerator.next()).toBe(3);
expect(loadedEvent.controlCodeGenerator.next()).toBe(31);
});

test("Modifying a control's code updates the code generator", () => {
const event = Event.create("Test");
const control = Control.create({
code: event.controlCodeGenerator.next(),
kind: "normal",
coordinates: [0, 0],
});
Event.addCourse(event, Course.create(null, "Test course", [control]));

Event.updateControl(event, control.id, (control) => {
control.code = 37;
});
expect(event.controlCodeGenerator.next()).toBe(38);
});
});
63 changes: 36 additions & 27 deletions src/models/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const ALL_CONTROLS_ID = -1;

interface Sequence {
next: () => number;
seed: (values: (number | string | null | undefined)[]) => void;
}

export interface Event {
Expand Down Expand Up @@ -53,32 +54,18 @@ export function create(name: string): Event {
}

export function load(data: Event): Event {
const maxId = toFinite(
Math.max(
0,
...[
...Object.keys(data.controls),
...data.courses.map((course) => course.id),
...data.specialObjects.map((object) => object.id),
]
.map(Number)
.filter((x) => !isNaN(x))
),
0
);
const maxControlCode = toFinite(
Math.max(
...[...Object.keys(data.controls).map((id) => data.controls[id].code)]
.map(Number)
.filter((x) => !isNaN(x))
),
29
);

const event = {
...data,
idGenerator: sequence(maxId + 1),
controlCodeGenerator: sequence(maxControlCode + 1),
idGenerator: sequence([
...Object.keys(data.controls),
...data.courses.map((course) => course.id),
...data.specialObjects.map((object) => object.id),
0,
]),
controlCodeGenerator: sequence([
...Object.keys(data.controls).map((id) => data.controls[id].code),
29,
]),
};
updateAllControls(event);

Expand Down Expand Up @@ -157,6 +144,11 @@ export function updateControl(
}
});
updateAllControls(event);
event.controlCodeGenerator.seed(
Object.values(event.controls)
.map((c) => c.code)
.filter((code: number | undefined): code is number => code != null)
);
}

export function updateAllControls(event: Event): void {
Expand Down Expand Up @@ -208,13 +200,30 @@ export function addSpecialObject(
return specialObject;
}

const sequence = (start: number) =>
/**
*
* @param seed if a number, this is the first value that will be generated by the sequence;
* if an array, the sequence will return the first successor to that is not in the array.
* @returns the newly created and seeded sequence
*/
const sequence = (
seed: number | (number | string | null | undefined)[]
): Sequence =>
(() => {
let s = start - 1;
return {
let s = 0;
const sequence = {
next: () => ++s,
current: () => s,
seed: (values: (number | string | null | undefined)[]) => {
s = toFinite(
Math.max(...values.map(Number).filter((x) => !isNaN(x))),
0
);
},
};
sequence.seed(Array.isArray(seed) ? seed : [seed - 1]);

return sequence;
})();

function toFinite(x: number, fallback: number): number {
Expand Down
9 changes: 9 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ interface Actions {
controlId: number,
description: Control.Description
) => void;
setCode: (controlId: number, code: number) => void;
};
setMode: (mode: Mode) => void;
};
Expand Down Expand Up @@ -447,6 +448,14 @@ const useEvent = create<StateWithActions>(
});
})
),
setCode: (controlId, code) =>
set(
undoable((draft: StateWithActions) => {
Event.updateControl(draft, controlId, (control) => {
control.code = code;
});
})
),
},
setMode: (mode) => set(() => ({ mode })),
},
Expand Down

0 comments on commit b7fd9e6

Please sign in to comment.