Skip to content

Commit

Permalink
js: use cancellable promises
Browse files Browse the repository at this point in the history
  • Loading branch information
carlinmack committed Jun 3, 2024
1 parent bee1708 commit 88239d3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import PropTypes from "prop-types";
import React, { Component } from "react";
import { http } from "react-invenio-forms";
import { RunButton } from "./RunButton";
import { withCancel } from "react-invenio-forms";

export class JobRunsHeaderComponent extends Component {
constructor(props) {
Expand All @@ -25,23 +26,25 @@ export class JobRunsHeaderComponent extends Component {

componentDidMount() {
const { jobId } = this.props;
http
.get("/api/jobs/" + jobId)
.then((response) => response.data)
.then((data) => {
this.setState({
loading: false,
...(data.title && { title: data.title }),
...(data.description && { description: data.description }),
...(data.default_args && { config: data.default_args }),
});
})
.catch((error) => {
this.onError(error);
this.setState({
loading: false,
});
});
withCancel(
http
.get("/api/jobs/" + jobId)
.then((response) => response.data)
.then((data) => {
this.setState({
loading: false,
...(data.title && { title: data.title }),
...(data.description && { description: data.description }),
...(data.default_args && { config: data.default_args }),
});
})
.catch((error) => {
this.onError(error);
this.setState({
loading: false,
});
})
);
}

static contextType = NotificationContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { i18next } from "@translations/invenio_app_rdm/i18next";
import PropTypes from "prop-types";
import React, { Component } from "react";
import { http } from "react-invenio-forms";
import { http, withCancel } from "react-invenio-forms";
import {
Button,
Dropdown,
Expand All @@ -34,6 +34,10 @@ export class RunButton extends Component {
handleConfigChange = (e, { name, value }) => this.setState({ config: value });
handleQueueChange = (e, { name, value }) => this.setState({ queue: value });

componentWillUnmount() {
this.cancellableAction && this.cancellableAction.cancel();
}

Check failure on line 40 in invenio_jobs/assets/semantic-ui/js/invenio_jobs/administration/RunButton.js

View workflow job for this annotation

GitHub Actions / Tests (3.9, postgresql14, opensearch2, 18.x)

Delete `··`

Check failure on line 40 in invenio_jobs/assets/semantic-ui/js/invenio_jobs/administration/RunButton.js

View workflow job for this annotation

GitHub Actions / Tests (3.9, postgresql14, opensearch2, 20.x)

Delete `··`

Check failure on line 40 in invenio_jobs/assets/semantic-ui/js/invenio_jobs/administration/RunButton.js

View workflow job for this annotation

GitHub Actions / Tests (3.12, postgresql14, opensearch2, 18.x)

Delete `··`

Check failure on line 40 in invenio_jobs/assets/semantic-ui/js/invenio_jobs/administration/RunButton.js

View workflow job for this annotation

GitHub Actions / Tests (3.12, postgresql14, opensearch2, 20.x)

Delete `··`
handleSubmit = async () => {
this.setState({ loading: true });

Expand All @@ -52,16 +56,20 @@ export class RunButton extends Component {
queue: queue,
};

Check failure on line 57 in invenio_jobs/assets/semantic-ui/js/invenio_jobs/administration/RunButton.js

View workflow job for this annotation

GitHub Actions / Tests (3.9, postgresql14, opensearch2, 18.x)

Delete `⏎`

Check failure on line 57 in invenio_jobs/assets/semantic-ui/js/invenio_jobs/administration/RunButton.js

View workflow job for this annotation

GitHub Actions / Tests (3.9, postgresql14, opensearch2, 20.x)

Delete `⏎`

Check failure on line 57 in invenio_jobs/assets/semantic-ui/js/invenio_jobs/administration/RunButton.js

View workflow job for this annotation

GitHub Actions / Tests (3.12, postgresql14, opensearch2, 18.x)

Delete `⏎`

Check failure on line 57 in invenio_jobs/assets/semantic-ui/js/invenio_jobs/administration/RunButton.js

View workflow job for this annotation

GitHub Actions / Tests (3.12, postgresql14, opensearch2, 20.x)

Delete `⏎`


try {
var response = await http.post("/api/jobs/" + jobId + "/runs", runData);
this.cancellableAction = await withCancel(
http.post("/api/jobs/" + jobId + "/runs", runData)
);
} catch (error) {
if (error.response) {
onError(error.response.data);
} else {
onError(error);
}
}
setRun(response.data.status, response.data.created);
const response = await this.cancellableAction.promise;
setRun(response.data?.status, response.data?.created);
this.setState({ loading: false });
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ import PropTypes from "prop-types";
import React, { useState } from "react";
import { http } from "react-invenio-forms";
import { Button, Icon } from "semantic-ui-react";
import { withCancel } from "react-invenio-forms";

export const StopButton = ({ stopURL, setStatus, onError }) => {
const [loading, setLoading] = useState(false);

const handleClick = async () => {
setLoading(true);
const response = await http.post(stopURL).catch((error) => {
if (error.response) {
onError(error.response.data);
} else {
onError(error);
}
});
const cancellableAction = await withCancel(
http.post(stopURL).catch((error) => {
if (error.response) {
onError(error.response.data);
} else {
onError(error);
}
})
);
const response = await cancellableAction.promise;
setStatus(response.data.status);
setLoading(false);
};
Expand Down

0 comments on commit 88239d3

Please sign in to comment.