Files
awx/awx/ui_next/src/screens/Job/useWsJob.js
Jake McDermott 72d700e125 Use the type-specific job models
Add the expected launch and run methods to the different job type
models. Include a new helper function to look up the right model
given a job type and use it in place of switch statements or
passing the type in to build a url.
2021-03-23 15:02:27 -04:00

50 lines
1.1 KiB
JavaScript

import { useState, useEffect } from 'react';
import useWebsocket from '../../util/useWebsocket';
import { getJobModel } from '../../util/jobs';
export default function useWsJob(initialJob) {
const [job, setJob] = useState(initialJob);
const lastMessage = useWebsocket({
jobs: ['status_changed'],
control: ['limit_reached_1'],
});
useEffect(() => {
setJob(initialJob);
}, [initialJob]);
useEffect(
function parseWsMessage() {
async function fetchJob() {
const { data } = await getJobModel(job.type).readDetail(job.id);
setJob(data);
}
if (!job || lastMessage?.unified_job_id !== job.id) {
return;
}
if (
['successful', 'failed', 'error', 'cancelled'].includes(
lastMessage.status
)
) {
fetchJob();
} else {
setJob(updateJob(job, lastMessage));
}
},
[lastMessage] // eslint-disable-line react-hooks/exhaustive-deps
);
return job;
}
function updateJob(job, message) {
return {
...job,
finished: message.finished,
status: message.status,
};
}