Skip to content

Commit

Permalink
Move function from utils to task/utils since it is related to just tasks
Browse files Browse the repository at this point in the history
Signed-off-by: angatupyry <fierrofenix@gmail.com>
  • Loading branch information
Angatupyry committed Sep 18, 2023
1 parent 7978bba commit 707eb5e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/dashboard/src/components/appbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import { parseTasksFile } from './tasks/utils';
import { Subscription } from 'rxjs';
import { formatDistance } from 'date-fns';
import { useCreateTaskFormData } from '../hooks/useCreateTaskForm';
import { toApiSchedule } from './utils';
import { toApiSchedule } from './tasks/utils';
import useGetUsername from '../hooks/useFetchUser';

export type TabValue = 'infrastructure' | 'robots' | 'tasks' | 'custom1' | 'custom2' | 'admin';
Expand Down
2 changes: 1 addition & 1 deletion packages/dashboard/src/components/tasks/task-schedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import useGetUsername from '../../hooks/useFetchUser';
import { AppControllerContext } from '../app-contexts';
import { AppEvents } from '../app-events';
import { RmfAppContext } from '../rmf-app';
import { toApiSchedule } from '../utils';
import { toApiSchedule } from './utils';
import {
apiScheduleToSchedule,
getScheduledTaskTitle,
Expand Down
28 changes: 27 additions & 1 deletion packages/dashboard/src/components/tasks/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TaskRequest, TaskState } from 'api-client';
import { PostScheduledTaskRequest, TaskRequest, TaskState } from 'api-client';
import { Schedule } from 'react-components';
import schema from 'api-client/dist/schema';
import { ajv } from '../utils';

Expand Down Expand Up @@ -93,3 +94,28 @@ export function downloadCsvMinimal(timestamp: Date, allTasks: TaskState[]) {
URL.revokeObjectURL(url);
});
}

export const toApiSchedule = (
taskRequest: TaskRequest,
schedule: Schedule,
): PostScheduledTaskRequest => {
const start = schedule.startOn;
const apiSchedules: PostScheduledTaskRequest['schedules'] = [];
const date = new Date(start);
const start_from = start.toISOString();
const until = schedule.until?.toISOString();
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
const at = `${hours}:${minutes}`;
schedule.days[0] && apiSchedules.push({ period: 'monday', start_from, at, until });
schedule.days[1] && apiSchedules.push({ period: 'tuesday', start_from, at, until });
schedule.days[2] && apiSchedules.push({ period: 'wednesday', start_from, at, until });
schedule.days[3] && apiSchedules.push({ period: 'thursday', start_from, at, until });
schedule.days[4] && apiSchedules.push({ period: 'friday', start_from, at, until });
schedule.days[5] && apiSchedules.push({ period: 'saturday', start_from, at, until });
schedule.days[6] && apiSchedules.push({ period: 'sunday', start_from, at, until });
return {
task_request: taskRequest,
schedules: apiSchedules,
};
};
27 changes: 0 additions & 27 deletions packages/dashboard/src/components/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import Ajv from 'ajv';
import { PostScheduledTaskRequest, TaskRequest } from 'api-client';
import schema from 'api-client/schema';
import { AxiosError } from 'axios';
import { Schedule } from 'react-components';

export function getApiErrorMessage(error: unknown): string {
return (error as AxiosError).response?.data.detail || '';
Expand All @@ -13,28 +11,3 @@ export const ajv = new Ajv();
Object.entries(schema.components.schemas).forEach(([k, v]) => {
ajv.addSchema(v, `#/components/schemas/${k}`);
});

export const toApiSchedule = (
taskRequest: TaskRequest,
schedule: Schedule,
): PostScheduledTaskRequest => {
const start = schedule.startOn;
const apiSchedules: PostScheduledTaskRequest['schedules'] = [];
const date = new Date(start);
const start_from = start.toISOString();
const until = schedule.until?.toISOString();
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
const at = `${hours}:${minutes}`;
schedule.days[0] && apiSchedules.push({ period: 'monday', start_from, at, until });
schedule.days[1] && apiSchedules.push({ period: 'tuesday', start_from, at, until });
schedule.days[2] && apiSchedules.push({ period: 'wednesday', start_from, at, until });
schedule.days[3] && apiSchedules.push({ period: 'thursday', start_from, at, until });
schedule.days[4] && apiSchedules.push({ period: 'friday', start_from, at, until });
schedule.days[5] && apiSchedules.push({ period: 'saturday', start_from, at, until });
schedule.days[6] && apiSchedules.push({ period: 'sunday', start_from, at, until });
return {
task_request: taskRequest,
schedules: apiSchedules,
};
};

0 comments on commit 707eb5e

Please sign in to comment.