Skip to content

Commit

Permalink
Refactoring new rename changes
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Chong <[email protected]>
  • Loading branch information
aaronchongth committed Jun 3, 2024
1 parent ebd5f19 commit 01139aa
Show file tree
Hide file tree
Showing 13 changed files with 376 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ export const getScheduledTaskTitle = (
let remappedTaskName: string | undefined = undefined;
if (supportedTasks) {
for (const s of supportedTasks) {
if (s.task_definition_id === task.task_request.category) {
remappedTaskName = s.task_display_name;
if (s.taskDefinitionId === task.task_request.category) {
remappedTaskName = s.taskDisplayName;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/dashboard/src/managers/resource-manager-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class TaskResourceManager {
this.supportedTasks = {};
if (supportedTasks) {
for (const t of supportedTasks) {
this.supportedTasks[t.task_definition_id] = t;
this.supportedTasks[t.taskDefinitionId] = t;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/dashboard/src/managers/resource-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { LogoResource, LogoResourceManager } from './resource-manager-logos';
import { RobotResource, RobotResourceManager } from './resource-manager-robots';
import {
DefaultPatrolTaskDefinition,
DefaultSimpleDeliveryTaskDefinition,
DefaultCleanTaskDefinition,
DefaultDeliveryTaskDefinition,
DefaultComposeCleanTaskDefinition,
TaskDefinition,
DefaultCustomComposeTaskDefinition,
} from 'react-components';
Expand Down Expand Up @@ -82,8 +82,8 @@ export default class ResourceManager {
this.loggedInDisplayLevel = resources.loggedInDisplayLevel;
this.supportedTasks = resources.supportedTasks || [
DefaultPatrolTaskDefinition,
DefaultSimpleDeliveryTaskDefinition,
DefaultCleanTaskDefinition,
DefaultDeliveryTaskDefinition,
DefaultComposeCleanTaskDefinition,
DefaultCustomComposeTaskDefinition,
];
}
Expand Down
153 changes: 85 additions & 68 deletions packages/react-components/lib/tasks/create-task.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,37 @@ import React from 'react';
import { Loading } from '..';
import { ConfirmationDialog, ConfirmationDialogProps } from '../confirmation-dialog';
import {
CleanTaskDescription,
CleanTaskForm,
DefaultCleanTaskDefinition,
makeCleanTaskBookingLabel,
makeDefaultCleanTaskDescription,
} from './types/clean';
ComposeCleanTaskDescription,
ComposeCleanTaskForm,
DefaultComposeCleanTaskDefinition,
makeComposeCleanTaskBookingLabel,
makeDefaultComposeCleanTaskDescription,
} from './types/compose-clean';
import {
DefaultCustomComposeTaskDefinition,
CustomComposeTaskDescription,
CustomComposeTaskForm,
makeCustomComposeTaskBookingLabel,
} from './types/custom-compose';
import {
DefaultSimpleDeliveryTaskDefinition,
SimpleDeliveryTaskDescription,
SimpleDeliveryTaskForm,
makeDefaultSimpleDeliveryTaskDescription,
makeSimpleDeliveryTaskBookingLabel,
} from './types/delivery-simple';
DefaultDeliveryTaskDefinition,
DeliveryTaskDescription,
DeliveryTaskForm,
makeDefaultDeliveryTaskDescription,
makeDeliveryTaskBookingLabel,
} from './types/delivery';
import {
DefaultDeliveryPickupTaskDefinition,
DefaultDeliveryAreaPickupTaskDefinition,
DefaultDeliverySequentialLotPickupTaskDefinition,
DeliveryCustomTaskForm,
DeliveryCustomTaskDescription,
DeliveryTaskDescription,
DeliveryTaskForm,
DeliveryPickupTaskDescription,
DeliveryPickupTaskForm,
makeDefaultDeliveryCustomTaskDescription,
makeDefaultDeliveryTaskDescription,
makeDefaultDeliveryPickupTaskDescription,
makeDeliveryCustomTaskBookingLabel,
makeDeliveryTaskBookingLabel,
makeDeliveryPickupTaskBookingLabel,
} from './types/delivery-custom';
import {
makeDefaultPatrolTask,
Expand All @@ -79,22 +79,26 @@ import {
PatrolTaskDescription,
PatrolTaskForm,
} from './types/patrol';
import { serializeTaskBookingLabel } from './task-booking-label-utils';
import {
getTaskBookingLabelFromTaskRequest,
serializeTaskBookingLabel,
} from './task-booking-label-utils';

export interface TaskDefinition {
task_definition_id: string;
task_display_name: string;
taskDefinitionId: string;
taskDisplayName: string;
requestCategory: string;
}

// If no task definition id is found in a past task (scheduled or favorite)
const DefaultTaskDefinitionId = DefaultCustomComposeTaskDefinition.task_definition_id;
const DefaultTaskDefinitionId = DefaultCustomComposeTaskDefinition.taskDefinitionId;

type TaskDescription =
| DeliveryTaskDescription
| DeliveryPickupTaskDescription
| DeliveryCustomTaskDescription
| PatrolTaskDescription
| SimpleDeliveryTaskDescription
| CleanTaskDescription;
| DeliveryTaskDescription
| ComposeCleanTaskDescription;

const classes = {
title: 'dialogue-info-value',
Expand Down Expand Up @@ -193,43 +197,45 @@ function FavoriteTask({
);
}

function defaultTaskDescription(taskName: string): TaskDescription | undefined {
switch (taskName) {
case 'delivery_pickup':
return makeDefaultDeliveryTaskDescription();
case 'delivery_sequential_lot_pickup':
case 'delivery_area_pickup':
return makeDefaultDeliveryCustomTaskDescription(taskName);
case 'patrol':
function defaultTaskDescription(taskDefinitionId: string): TaskDescription | undefined {
switch (taskDefinitionId) {
case DefaultDeliveryPickupTaskDefinition.taskDefinitionId:
return makeDefaultDeliveryPickupTaskDescription();
case DefaultDeliverySequentialLotPickupTaskDefinition.taskDefinitionId:
case DefaultDeliveryAreaPickupTaskDefinition.taskDefinitionId:
return makeDefaultDeliveryCustomTaskDescription(taskDefinitionId);
case DefaultPatrolTaskDefinition.taskDefinitionId:
return makeDefaultPatrolTask();
case 'delivery':
return makeDefaultSimpleDeliveryTaskDescription();
case 'clean':
return makeDefaultCleanTaskDescription();
case DefaultDeliveryTaskDefinition.taskDefinitionId:
return makeDefaultDeliveryTaskDescription();
case DefaultComposeCleanTaskDefinition.taskDefinitionId:
return makeDefaultComposeCleanTaskDescription();
default:
return undefined;
}
}

function taskRequestCategory(task_definition_id: string): string | undefined {
switch (task_definition_id) {
case DefaultPatrolTaskDefinition.task_definition_id:
case DefaultSimpleDeliveryTaskDefinition.task_definition_id:
return task_definition_id;
case DefaultDeliveryPickupTaskDefinition.task_definition_id:
case DefaultDeliverySequentialLotPickupTaskDefinition.task_definition_id:
case DefaultDeliveryAreaPickupTaskDefinition.task_definition_id:
case DefaultCleanTaskDefinition.task_definition_id:
case DefaultCustomComposeTaskDefinition.task_definition_id:
// TODO: Move this into task defintion as well. To consider moving
// description.category into task definition too.
function taskRequestCategory(taskDefinitionId: string): string | undefined {
switch (taskDefinitionId) {
case DefaultPatrolTaskDefinition.taskDefinitionId:
case DefaultDeliveryTaskDefinition.taskDefinitionId:
return taskDefinitionId;
case DefaultDeliveryPickupTaskDefinition.taskDefinitionId:
case DefaultDeliverySequentialLotPickupTaskDefinition.taskDefinitionId:
case DefaultDeliveryAreaPickupTaskDefinition.taskDefinitionId:
case DefaultComposeCleanTaskDefinition.taskDefinitionId:
case DefaultCustomComposeTaskDefinition.taskDefinitionId:
return 'compose';
default:
return undefined;
}
}

function defaultTaskRequest(task_definition_id: string): TaskRequest {
const category = taskRequestCategory(task_definition_id);
const description = defaultTaskDescription(task_definition_id);
function defaultTaskRequest(taskDefinitionId: string): TaskRequest {
const category = taskRequestCategory(taskDefinitionId);
const description = defaultTaskDescription(taskDefinitionId);

return {
category: category ?? 'compose',
Expand Down Expand Up @@ -351,8 +357,8 @@ export function CreateTaskForm({
user,
supportedTasks = [
DefaultPatrolTaskDefinition,
DefaultSimpleDeliveryTaskDefinition,
DefaultCleanTaskDefinition,
DefaultDeliveryTaskDefinition,
DefaultComposeCleanTaskDefinition,
DefaultCustomComposeTaskDefinition,
],
/* eslint-disable @typescript-eslint/no-unused-vars */
Expand Down Expand Up @@ -394,16 +400,21 @@ export function CreateTaskForm({
const [favoriteTaskTitleError, setFavoriteTaskTitleError] = React.useState(false);
const [savingFavoriteTask, setSavingFavoriteTask] = React.useState(false);

const [taskDefinitionId, setTaskDefinitionId] = React.useState<string>(
supportedTasks.length > 0 ? supportedTasks[0].task_definition_id : DefaultTaskDefinitionId,
);
const [taskRequest, setTaskRequest] = React.useState<TaskRequest>(
() =>
requestTask ??
defaultTaskRequest(
supportedTasks.length > 0 ? supportedTasks[0].task_definition_id : DefaultTaskDefinitionId,
supportedTasks.length > 0 ? supportedTasks[0].taskDefinitionId : DefaultTaskDefinitionId,
),
);
const initialBookingLabel = requestTask ? getTaskBookingLabelFromTaskRequest(requestTask) : null;
const [taskDefinitionId, setTaskDefinitionId] = React.useState<string>(
initialBookingLabel
? initialBookingLabel.description.task_definition_id
: supportedTasks.length > 0
? supportedTasks[0].taskDefinitionId
: DefaultTaskDefinitionId,
);

const [submitting, setSubmitting] = React.useState(false);
const [formFullyFilled, setFormFullyFilled] = React.useState(requestTask !== undefined || false);
Expand Down Expand Up @@ -482,8 +493,8 @@ export function CreateTaskForm({
);
case 'delivery':
return (
<SimpleDeliveryTaskForm
taskDesc={taskRequest.description as SimpleDeliveryTaskDescription}
<DeliveryTaskForm
taskDesc={taskRequest.description as DeliveryTaskDescription}
pickupPoints={pickupPoints}
dropoffPoints={dropoffPoints}
onChange={(desc) => handleTaskDescriptionChange('delivery', desc)}
Expand All @@ -505,10 +516,10 @@ export function CreateTaskForm({
switch (taskRequest.description.category) {
case 'clean':
return (
<CleanTaskForm
taskDesc={taskRequest.description as CleanTaskDescription}
<ComposeCleanTaskForm
taskDesc={taskRequest.description as ComposeCleanTaskDescription}
cleaningZones={cleaningZones}
onChange={(desc: CleanTaskDescription) => {
onChange={(desc: ComposeCleanTaskDescription) => {
desc.category = taskRequest.description.category;
handleTaskDescriptionChange('compose', desc);
}}
Expand All @@ -517,12 +528,12 @@ export function CreateTaskForm({
);
case 'delivery_pickup':
return (
<DeliveryTaskForm
taskDesc={taskRequest.description as DeliveryTaskDescription}
<DeliveryPickupTaskForm
taskDesc={taskRequest.description as DeliveryPickupTaskDescription}
pickupPoints={pickupPoints}
cartIds={cartIds}
dropoffPoints={dropoffPoints}
onChange={(desc: DeliveryTaskDescription) => {
onChange={(desc: DeliveryPickupTaskDescription) => {
desc.category = taskRequest.description.category;
desc.phases[0].activity.description.activities[1].description.category =
taskRequest.description.category;
Expand Down Expand Up @@ -558,6 +569,7 @@ export function CreateTaskForm({
};
const handleTaskTypeChange = (ev: React.ChangeEvent<HTMLInputElement>) => {
const newType = ev.target.value;
console.log(`handleTaskTypeChange ${newType}`);
setTaskDefinitionId(newType);

if (newType === 'custom_compose') {
Expand Down Expand Up @@ -612,7 +624,7 @@ export function CreateTaskForm({
let requestBookingLabel: TaskBookingLabel | null = null;
switch (taskDefinitionId) {
case 'delivery_pickup':
requestBookingLabel = makeDeliveryTaskBookingLabel(request.description);
requestBookingLabel = makeDeliveryPickupTaskBookingLabel(request.description);
break;
case 'delivery_sequential_lot_pickup':
case 'delivery_area_pickup':
Expand All @@ -622,9 +634,11 @@ export function CreateTaskForm({
requestBookingLabel = makePatrolTaskBookingLabel(request.description);
break;
case 'delivery':
requestBookingLabel = makeSimpleDeliveryTaskBookingLabel(request.description);
requestBookingLabel = makeDeliveryTaskBookingLabel(request.description);
break;
case 'clean':
requestBookingLabel = makeCleanTaskBookingLabel(request.description);
requestBookingLabel = makeComposeCleanTaskBookingLabel(request.description);
break;
case 'custom_compose':
requestBookingLabel = makeCustomComposeTaskBookingLabel();
break;
Expand Down Expand Up @@ -726,7 +740,7 @@ export function CreateTaskForm({

setTaskRequest(
supportedTasks && supportedTasks.length > 0
? defaultTaskRequest(supportedTasks[0].task_definition_id)
? defaultTaskRequest(supportedTasks[0].taskDefinitionId)
: defaultTaskRequest('patrol'),
);
setOpenFavoriteDialog(false);
Expand Down Expand Up @@ -819,8 +833,11 @@ export function CreateTaskForm({
>
{supportedTasks.map((taskDefinition) => {
return (
<MenuItem value={taskDefinition.task_definition_id}>
{taskDefinition.task_display_name}
<MenuItem
value={taskDefinition.taskDefinitionId}
key={taskDefinition.taskDefinitionId}
>
{taskDefinition.taskDisplayName}
</MenuItem>
);
})}
Expand Down
24 changes: 23 additions & 1 deletion packages/react-components/lib/tasks/task-booking-label-utils.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ajv } from '../utils/schema-utils';
import schema from 'api-client/dist/schema';
import type { TaskBookingLabel, TaskState } from 'api-client';
import type { TaskBookingLabel, TaskRequest, TaskState } from 'api-client';

const validateTaskBookingLabel = ajv.compile(schema.components.schemas.TaskBookingLabel);

Expand Down Expand Up @@ -44,3 +44,25 @@ export function getTaskBookingLabelFromTaskState(taskState: TaskState): TaskBook
}
return requestLabel;
}

export function getTaskBookingLabelFromTaskRequest(
taskRequest: TaskRequest,
): TaskBookingLabel | null {
if (!taskRequest.labels) {
return null;
}

let requestLabel: TaskBookingLabel | null = null;
for (const label of taskRequest.labels) {
try {
const parsedLabel = getTaskBookingLabelFromJsonString(label);
if (parsedLabel) {
requestLabel = parsedLabel;
break;
}
} catch (e) {
continue;
}
}
return requestLabel;
}
Loading

0 comments on commit 01139aa

Please sign in to comment.