-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: turn JobRunsHeader into a component
- Loading branch information
1 parent
9d063f1
commit fbfdbaa
Showing
2 changed files
with
82 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 72 additions & 23 deletions
95
invenio_jobs/assets/semantic-ui/js/invenio_jobs/administration/search/JobRunsHeader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,75 @@ | ||
// This file is part of Invenio | ||
// Copyright (C) 2024 CERN. | ||
// | ||
// Invenio RDM is free software; you can redistribute it and/or modify it | ||
// under the terms of the MIT License; see LICENSE file for more details. | ||
|
||
import { RunButton } from "./RunButton"; | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
|
||
export async function JobRunsHeader(pidValue) { | ||
fetch("/api/jobs/" + pidValue) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
if (data.title) { | ||
const titleElem = document.getElementById("title"); | ||
if (titleElem) { | ||
titleElem.innerText = data.title; | ||
const descriptionElem = document.getElementById("description"); | ||
if (descriptionElem && data.description) { | ||
descriptionElem.innerText = data.description; | ||
} | ||
} | ||
} | ||
|
||
const actions = document.getElementById("actions"); | ||
ReactDOM.render( | ||
<RunButton jobId={data.id} config={data.default_args ?? {}} />, | ||
actions | ||
); | ||
import React, { Component } from "react"; | ||
import { NotificationContext } from "@js/invenio_administration"; | ||
import { i18next } from "@translations/invenio_app_rdm/i18next"; | ||
import PropTypes from "prop-types"; | ||
import { http } from "react-invenio-forms"; | ||
|
||
export class JobRunsHeaderComponent extends Component { | ||
constructor(props) { | ||
super(); | ||
|
||
this.state = { | ||
jobId: props.jobId, | ||
title: "Job Details", | ||
description: "", | ||
config: {}, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
const { jobId } = this.state; | ||
http | ||
.get("/api/jobs/" + jobId) | ||
.then((response) => response.data) | ||
.then((data) => { | ||
this.setState({ | ||
...(data.title && { title: data.title }), | ||
...(data.description && { description: data.description }), | ||
...(data.default_args && { config: data.default_args }), | ||
}); | ||
}); | ||
} | ||
|
||
static contextType = NotificationContext; | ||
|
||
onError = (e) => { | ||
const { addNotification } = this.context; | ||
addNotification({ | ||
title: i18next.t("Error"), | ||
content: e.message, | ||
type: "error", | ||
}); | ||
console.error(e); | ||
}; | ||
|
||
render() { | ||
const { jobId } = this.props; | ||
const { title, description, config } = this.state; | ||
return ( | ||
<> | ||
<div className="column six wide"> | ||
<h1 className="ui header m-0">{title}</h1> | ||
<p className="ui grey header">{description}</p> | ||
</div> | ||
<div className="column ten wide right aligned"> | ||
<RunButton | ||
jobId={jobId} | ||
config={config ?? {}} | ||
onError={this.onError} | ||
/> | ||
</div> | ||
</> | ||
); | ||
} | ||
} | ||
|
||
JobRunsHeaderComponent.propTypes = { | ||
jobId: PropTypes.string.isRequired, | ||
}; |