Skip to content

Commit

Permalink
Fix component render sequence issues
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Chong <aaronchongth@gmail.com>
  • Loading branch information
aaronchongth committed Aug 30, 2024
1 parent ce848a2 commit ca9ecf9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 36 deletions.
3 changes: 2 additions & 1 deletion packages/dashboard/src/components/appbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const AppBar = React.memo(({ extraToolbarItems }: AppBarProps): React.Rea
FireAlarmTriggerState | undefined
>(undefined);

const { waypointNames, pickupPoints, dropoffPoints, cleaningZoneNames } =
const { waypointNames, pickupPoints, dropoffPoints, cleaningZoneNames, fleets } =
useCreateTaskFormData(rmf);
const username = useGetUsername(rmf);

Expand Down Expand Up @@ -566,6 +566,7 @@ export const AppBar = React.memo(({ extraToolbarItems }: AppBarProps): React.Rea
{openCreateTaskForm && (
<CreateTaskForm
user={username ? username : 'unknown user'}
fleets={fleets}
tasksToDisplay={allowedTasks}
patrolWaypoints={waypointNames}
cleaningZones={cleaningZoneNames}
Expand Down
3 changes: 2 additions & 1 deletion packages/dashboard/src/components/tasks/task-schedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const TaskSchedule = () => {
const rmf = React.useContext(RmfAppContext);
const { showAlert } = React.useContext(AppControllerContext);

const { waypointNames, pickupPoints, dropoffPoints, cleaningZoneNames } =
const { waypointNames, pickupPoints, dropoffPoints, cleaningZoneNames, fleets } =
useCreateTaskFormData(rmf);
const username = useGetUsername(rmf);
const [eventScope, setEventScope] = React.useState<string>(EventScopes.CURRENT);
Expand Down Expand Up @@ -352,6 +352,7 @@ export const TaskSchedule = () => {
{openCreateTaskForm && (
<CreateTaskForm
user={username ? username : 'unknown user'}
fleets={fleets}
tasksToDisplay={allowedTasks}
patrolWaypoints={waypointNames}
cleaningZones={cleaningZoneNames}
Expand Down
15 changes: 14 additions & 1 deletion packages/dashboard/src/hooks/useCreateTaskForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const useCreateTaskFormData = (rmf: RmfIngress | undefined) => {
const [cleaningZoneNames, setCleaningZoneNames] = React.useState<string[]>([]);
const [pickupPoints, setPickupPoints] = React.useState<Record<string, string>>({});
const [dropoffPoints, setDropoffPoints] = React.useState<Record<string, string>>({});
const [fleets, setFleets] = React.useState<Record<string, string[]>>({});

React.useEffect(() => {
if (!rmf) {
Expand Down Expand Up @@ -42,9 +43,21 @@ export const useCreateTaskFormData = (rmf: RmfIngress | undefined) => {
setWaypointNames(waypointNames);
}),
);
subs.push(
rmf.fleetsObs.subscribe((fleetStates) => {
const result: Record<string, string[]> = {};
for (const fleet of fleetStates) {
if (!fleet.name || !fleet.robots) {
continue;
}
result[fleet.name] = Object.keys(fleet.robots);
}
setFleets(result);
}),
);

return () => subs.forEach((s) => s.unsubscribe());
}, [rmf]);

return { waypointNames, pickupPoints, dropoffPoints, cleaningZoneNames };
return { waypointNames, pickupPoints, dropoffPoints, cleaningZoneNames, fleets };
};
64 changes: 32 additions & 32 deletions packages/react-components/lib/tasks/create-task.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,7 @@ export interface CreateTaskFormProps

export function CreateTaskForm({
user,
fleets = {
f1: ['r11', 'r12'],
f2: ['r21', 'r22'],
},
fleets,
tasksToDisplay = [
PatrolTaskDefinition,
DeliveryTaskDefinition,
Expand Down Expand Up @@ -710,15 +707,10 @@ export function CreateTaskForm({
setSubmitting(true);
if (scheduling) {
await scheduleTask(request, schedule);
} else {
let robotDispatchTarget: RobotDispatchTarget | null = null;
if (dispatchType === DispatchType.Robot) {
robotDispatchTarget = {
fleet: '',
robot: '',
};
}
} else if (dispatchType === DispatchType.Robot) {
await dispatchTask(request, robotDispatchTarget);
} else {
await dispatchTask(request, null);
}
setSubmitting(false);

Expand Down Expand Up @@ -844,7 +836,6 @@ export function CreateTaskForm({
};

const handleDispatchFleetTargetChange = (ev: React.ChangeEvent<HTMLInputElement>) => {
console.log('handleDispatchFleetTargetChange called');
setTaskRequest((prev) => {
return {
...prev,
Expand All @@ -854,6 +845,10 @@ export function CreateTaskForm({
};

const handleDispatchRobotTargetChange = (ev: React.ChangeEvent<HTMLInputElement>) => {
if (!fleets) {
setRobotDispatchTarget(null);
return;
}
let robotFleet: string | null = null;
for (const fleetName of Object.keys(fleets)) {
if (fleets[fleetName].includes(ev.target.value)) {
Expand Down Expand Up @@ -1022,16 +1017,17 @@ export function CreateTaskForm({
fullWidth
margin="normal"
value={taskRequest.fleet_name ?? ''}
defaultValue={Object.keys(fleets)[0]}
defaultValue=""
onChange={handleDispatchFleetTargetChange}
>
{Object.keys(fleets).map((fleetName) => {
return (
<MenuItem value={fleetName} key={fleetName}>
{fleetName}
</MenuItem>
);
})}
{fleets &&
Object.keys(fleets).map((fleetName) => {
return (
<MenuItem value={fleetName} key={fleetName}>
{fleetName}
</MenuItem>
);
})}
</TextField>
) : dispatchType === DispatchType.Robot ? (
<TextField
Expand All @@ -1041,25 +1037,29 @@ export function CreateTaskForm({
variant="outlined"
fullWidth
margin="normal"
value={robotDispatchTarget?.robot}
value={robotDispatchTarget?.robot ?? ''}
defaultValue=""
onChange={handleDispatchRobotTargetChange}
>
{Object.keys(fleets).map((fleetName) => {
return (
<>
<ListSubheader>{fleetName}</ListSubheader>
{fleets[fleetName].map((robotName) => (
{fleets &&
Object.keys(fleets).flatMap((fleetName) => {
const fleetRobots = [
<Divider key={`${fleetName}_divider`} />,
<MenuItem value={fleetName} key={fleetName} disabled>
{fleetName}
</MenuItem>,
];
return fleetRobots.concat(
fleets[fleetName].map((robotName) => (
<MenuItem value={robotName} key={robotName}>
{robotName}
</MenuItem>
))}
</>
);
})}
)),
);
})}
</TextField>
) : (
<TextField
select
disabled
id="dispatch-automatic-type"
variant="outlined"
Expand Down
1 change: 0 additions & 1 deletion packages/react-components/lib/tasks/types/patrol.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ export function PatrolTaskForm({
onValidate(isPatrolTaskDescriptionValid(desc));
onChange(desc);
};
onValidate(isPatrolTaskDescriptionValid(taskDesc));

return (
<Grid container spacing={theme.spacing(2)} justifyContent="center" alignItems="center">
Expand Down

0 comments on commit ca9ecf9

Please sign in to comment.