Skip to content

Commit ea9aceb

Browse files
rekt-hardmax-moser
authored andcommitted
deposit: check permission and set disable tooltip for publish button
1 parent 6d226ce commit ea9aceb

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/controls/PublishButton/PublishButton.js

+50-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This file is part of Invenio-RDM-Records
22
// Copyright (C) 2020-2023 CERN.
33
// Copyright (C) 2020-2022 Northwestern University.
4+
// Copyright (C) 2024 Graz University of Technology.
45
//
56
// Invenio-RDM-Records is free software; you can redistribute it and/or modify it
67
// under the terms of the MIT License; see LICENSE file for more details.
@@ -12,7 +13,7 @@ import _omit from "lodash/omit";
1213
import PropTypes from "prop-types";
1314
import React, { Component } from "react";
1415
import { connect } from "react-redux";
15-
import { Button, Icon, Message, Modal } from "semantic-ui-react";
16+
import { Button, Icon, Message, Modal, Popup } from "semantic-ui-react";
1617
import {
1718
DepositFormSubmitActions,
1819
DepositFormSubmitContext,
@@ -40,8 +41,8 @@ class PublishButtonComponent extends Component {
4041
this.closeConfirmModal();
4142
};
4243

43-
isDisabled = (values, isSubmitting, filesState) => {
44-
if (isSubmitting) {
44+
isDisabled = (values, isSubmitting, filesState, hasPublishPermission) => {
45+
if (isSubmitting || !hasPublishPermission) {
4546
return true;
4647
}
4748

@@ -59,6 +60,19 @@ class PublishButtonComponent extends Component {
5960
return !allCompleted;
6061
};
6162

63+
hasPermission = (permissions) => {
64+
return permissions.can_publish;
65+
};
66+
67+
getDisabledButtonPopupText = (hasPublishPermission) => {
68+
let text = i18next.t("Required fields are missing");
69+
70+
if (!hasPublishPermission) {
71+
text = i18next.t("You don't have permission to publish");
72+
}
73+
return text;
74+
};
75+
6276
render() {
6377
const {
6478
actionState,
@@ -67,26 +81,46 @@ class PublishButtonComponent extends Component {
6781
publishWithoutCommunity,
6882
formik,
6983
publishModalExtraContent,
84+
permissions,
7085
...ui
7186
} = this.props;
7287
const { isConfirmModalOpen } = this.state;
7388
const { values, isSubmitting, handleSubmit } = formik;
7489

7590
const uiProps = _omit(ui, ["dispatch"]);
7691

92+
const hasPublishPermission = this.hasPermission(permissions);
93+
const publishDisabled = this.isDisabled(
94+
values,
95+
isSubmitting,
96+
filesState,
97+
hasPublishPermission
98+
);
99+
100+
// only used when button is disabled
101+
const popupText = this.getDisabledButtonPopupText(hasPublishPermission);
102+
77103
return (
78104
<>
79-
<Button
80-
disabled={this.isDisabled(values, isSubmitting, filesState)}
81-
name="publish"
82-
onClick={this.openConfirmModal}
83-
positive
84-
icon="upload"
85-
loading={isSubmitting && actionState === DRAFT_PUBLISH_STARTED}
86-
labelPosition="left"
87-
content={buttonLabel}
88-
{...uiProps}
89-
type="button" // needed so the formik form doesn't handle it as submit button i.e enable HTML validation on required input fields
105+
<Popup
106+
disabled={!publishDisabled}
107+
content={popupText}
108+
trigger={
109+
<span>
110+
<Button
111+
disabled={publishDisabled}
112+
name="publish"
113+
onClick={this.openConfirmModal}
114+
positive
115+
icon="upload"
116+
loading={isSubmitting && actionState === DRAFT_PUBLISH_STARTED}
117+
labelPosition="left"
118+
content={buttonLabel}
119+
{...uiProps}
120+
type="button" // needed so the formik form doesn't handle it as submit button i.e enable HTML validation on required input fields
121+
/>
122+
</span>
123+
}
90124
/>
91125
{isConfirmModalOpen && (
92126
<Modal
@@ -139,6 +173,7 @@ PublishButtonComponent.propTypes = {
139173
formik: PropTypes.object.isRequired,
140174
publishModalExtraContent: PropTypes.string,
141175
filesState: PropTypes.object,
176+
permissions: PropTypes.object.isRequired,
142177
};
143178

144179
PublishButtonComponent.defaultProps = {
@@ -153,6 +188,7 @@ const mapStateToProps = (state) => ({
153188
actionState: state.deposit.actionState,
154189
publishModalExtraContent: state.deposit.config.publish_modal_extra,
155190
filesState: state.files,
191+
permissions: state.deposit.permissions,
156192
});
157193

158194
export const PublishButton = connect(

invenio_rdm_records/assets/semantic-ui/js/invenio_rdm_records/src/deposit/controls/PublishButton/SubmitReviewOrPublishButton.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class SubmitReviewOrPublishComponent extends Component {
2929
showDirectPublishButton,
3030
showSubmitForReviewButton,
3131
record,
32+
permissions,
3233
...ui
3334
} = this.props;
3435
const { modalOpen } = this.state;
@@ -61,13 +62,14 @@ class SubmitReviewOrPublishComponent extends Component {
6162
/>
6263
<PublishButton
6364
buttonLabel={i18next.t("Publish without community")}
65+
permissions={permissions}
6466
publishWithoutCommunity
6567
{...ui}
6668
/>
6769
</>
6870
);
6971
} else {
70-
result = <PublishButton {...ui} />;
72+
result = <PublishButton permissions={permissions} {...ui} />;
7173
}
7274
return result;
7375
}
@@ -80,6 +82,7 @@ SubmitReviewOrPublishComponent.propTypes = {
8082
showDirectPublishButton: PropTypes.bool.isRequired,
8183
showSubmitForReviewButton: PropTypes.bool.isRequired,
8284
record: PropTypes.object.isRequired,
85+
permissions: PropTypes.object.isRequired,
8386
};
8487

8588
SubmitReviewOrPublishComponent.defaultProps = {

0 commit comments

Comments
 (0)