Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search to JobModal.tsx #16

Merged
merged 12 commits into from
Dec 16, 2023
49 changes: 26 additions & 23 deletions packages/client/src/app/jobs/executeJobModal.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
import React, { useState, useMemo } from 'react';
import { Button, Modal, Table, Text } from '@nextui-org/react';
import { MitmControlDTO } from '@rotom/connections';
import { Selection } from '@react-types/shared/src/selection';
import { toast } from 'react-toastify';
import { useCallback, useState } from 'react';

import { SearchInput } from '../status/search';

interface ExecuteJobModalProps {
closeModal: () => void;
devices?: MitmControlDTO[];
jobId: string;
}

export const ExecuteJobModal = ({ closeModal, devices, jobId }: ExecuteJobModalProps): JSX.Element => {
export const ExecuteJobModal: React.FC<ExecuteJobModalProps> = ({ closeModal, devices, jobId }) => {
const [selectedDevices, setSelectedDevices] = useState<Selection>();
const [search, setSearch] = useState('');

const executeJob = useCallback(
async ({ deviceIds }: { deviceIds: string[] | number[] }) => {
const promise = fetch(`/api/job/execute/${jobId}/${deviceIds.join()}`, { method: 'POST' }).then(
async (response) => {
if (response.status !== 200) {
throw new Error();
}
const filteredDevices = useMemo(() => {
Fabio1988 marked this conversation as resolved.
Show resolved Hide resolved
return devices?.filter((device) => device.deviceId?.includes(search) || device.origin?.includes(search)) || [];
}, [devices, search]);
Fabio1988 marked this conversation as resolved.
Show resolved Hide resolved

closeModal();
},
);
const executeJob = async ({ deviceIds }: { deviceIds: string[] | number[] }) => {
Fabio1988 marked this conversation as resolved.
Show resolved Hide resolved
const promise = fetch(`/api/job/execute/${jobId}/${deviceIds.join()}`, { method: 'POST' }).then(
async (response) => {
if (response.status !== 200) {
throw new Error();
}
closeModal();
},
);

toast.promise(promise, {
pending: `Running ${jobId}...`,
success: `${jobId} started succesfully`,
error: `Failed to start job ${jobId}`,
});
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[jobId],
);
toast.promise(promise, {
pending: `Running ${jobId}...`,
success: `${jobId} started successfully`,
error: `Failed to start job ${jobId}`,
});
};

return (
<>
<Modal.Header>
<Text h3>Execute {jobId}</Text>
</Modal.Header>
<Modal.Body>
{devices && (
<SearchInput value={search} onChange={setSearch} />
{filteredDevices && (
<Table
aria-label="Devices"
bordered
Expand All @@ -64,7 +67,7 @@ export const ExecuteJobModal = ({ closeModal, devices, jobId }: ExecuteJobModalP
<Table.Column>Origin</Table.Column>
</Table.Header>
<Table.Body>
{devices.map((device) => (
{filteredDevices.map((device) => (
<Table.Row key={`${device.deviceId}`}>
<Table.Cell>{device.deviceId}</Table.Cell>
<Table.Cell>{device.origin}</Table.Cell>
Expand Down