Skip to content

Commit

Permalink
editor: refactor set char count action
Browse files Browse the repository at this point in the history
* refactor action
* disable submit button when it's < 1 char
  • Loading branch information
Samk13 committed Jul 8, 2024
1 parent 9e848ef commit e23919f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// This file is part of InvenioRequests
// Copyright (C) 2022 CERN.
// Copyright (C) 2024 KTH Royal Institute of Technology.
//
// Invenio RDM Records is free software; you can redistribute it and/or modify it
// under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -24,7 +23,6 @@ export class Request extends Component {
request,
userAvatar,
permissions,
commentContentMaxLength,
updateRequestAfterAction,
} = this.props;

Expand All @@ -39,7 +37,6 @@ export class Request extends Component {
request={request}
userAvatar={userAvatar}
permissions={permissions}
commentContentMaxLength={commentContentMaxLength}
/>
</Loader>
</Overridable>
Expand All @@ -52,13 +49,11 @@ Request.propTypes = {
initRequest: PropTypes.func.isRequired,
updateRequestAfterAction: PropTypes.func.isRequired,
userAvatar: PropTypes.string,
commentContentMaxLength: PropTypes.number.isRequired,
permissions: PropTypes.object.isRequired,
};

Request.defaultProps = {
userAvatar: "",
commentContentMaxLength: 0,
};

export default Overridable.component("InvenioRequests.Request", Request);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const TimelineCommentEditor = ({
charCount,
commentContentMaxLength,
}) => {
const isSubmitDisabled = () => isLoading || (charCount === 0) || (charCount >= commentContentMaxLength)
return (
<div className="timeline-comment-editor-container">
{error && <Message negative>{error}</Message>}
Expand All @@ -46,7 +47,7 @@ const TimelineCommentEditor = ({
icon="send"
size="medium"
content={i18next.t("Comment")}
disabled={isLoading || charCount >= commentContentMaxLength}
disabled={isSubmitDisabled()}
loading={isLoading}
onClick={() => submitComment(commentContent, "html")}
/>
Expand All @@ -72,7 +73,7 @@ TimelineCommentEditor.defaultProps = {
error: "",
userAvatar: "",
charCount: 0,
commentContentMaxLength: 25000,
commentContentMaxLength: 0,
};

export default TimelineCommentEditor;
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "../../timeline/state/actions";
import _cloneDeep from "lodash/cloneDeep";
import { i18next } from "@translations/invenio_requests/i18next";

export const IS_LOADING = "eventEditor/IS_LOADING";
export const HAS_ERROR = "eventEditor/HAS_ERROR";
export const SUCCESS = "eventEditor/SUCCESS";
Expand All @@ -27,27 +28,22 @@ export const setEventContent = (content) => {
type: SETTING_CONTENT,
payload: content,
});
const commentContentMaxLength = config.commentContentMaxLength || 25000;

const commentContentMaxLength = config.commentContentMaxLength;
const charCount = content.length;
dispatch({
type: SET_CHAR_COUNT,
payload: charCount,
});
if (charCount >= commentContentMaxLength) {
dispatch({
type: HAS_ERROR,
payload: i18next.t("Character count exceeds the maximum allowed limit."),
});
} else {
dispatch({
type: HAS_ERROR,
payload: null,
});
}
const errorMessage = i18next.t("Character count exceeds the maximum allowed limit.");

const validateContentLength = (charCount, maxLength, errorMessage) => {
return charCount >= maxLength ? errorMessage : null;
};

const error = validateContentLength(charCount, commentContentMaxLength, errorMessage);

dispatch({ type: SET_CHAR_COUNT, payload: charCount });
dispatch({ type: HAS_ERROR, payload: error });
};
};


export const submitComment = (content, format) => {
return async (dispatch, getState, config) => {
const { timeline: timelineState } = getState();
Expand Down

0 comments on commit e23919f

Please sign in to comment.