Skip to content

Commit

Permalink
EPMRPP-96935 || Code review fixes - 2
Browse files Browse the repository at this point in the history
  • Loading branch information
iso9000t committed Nov 26, 2024
1 parent b393133 commit 61dd5c8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 61 deletions.
1 change: 1 addition & 0 deletions app/src/common/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,5 @@ export const URLS = {
clusterByLaunchId: (activeProject, launchId, query) =>
`${urlBase}${activeProject}/launch/cluster/${launchId}${getQueryParams(query)}`,
onboarding: (page = 'GENERAL') => `${urlBase}onboarding?page=${page}`,
ssoSettings: () => `${urlBase}settings`,
};
9 changes: 5 additions & 4 deletions app/src/components/inputs/inputUserSearch/inputUserSearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const InputUserSearch = ({
error,
touched,
placeholder,
ssoUsersOnly,
creatable,
}) => (
<AsyncAutocomplete
getURI={getURI(isAdmin, projectId)}
Expand All @@ -80,7 +80,7 @@ export const InputUserSearch = ({
renderOption={renderOption}
placeholder={placeholder}
isOptionUnique={isOptionUnique}
creatable={!ssoUsersOnly}
creatable={creatable}
showDynamicSearchPrompt
/>
);
Expand All @@ -93,8 +93,9 @@ InputUserSearch.propTypes = {
value: PropTypes.object,
error: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
touched: PropTypes.bool,
ssoUsersOnly: PropTypes.bool,
creatable: PropTypes.bool,
};

InputUserSearch.defaultProps = {
isAdmin: false,
projectId: '',
Expand All @@ -103,5 +104,5 @@ InputUserSearch.defaultProps = {
value: null,
error: false,
touched: false,
ssoUsersOnly: false,
creatable: true,
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@
*/

import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { defineMessages, useIntl } from 'react-intl';
import classNames from 'classnames/bind';
import { connect } from 'react-redux';
import { useSelector, useDispatch } from 'react-redux';
import { InputBigSwitcher } from 'components/inputs/inputBigSwitcher';
import { SectionHeader } from 'components/main/sectionHeader';
import { ADMIN_SERVER_SETTINGS_PAGE_EVENTS } from 'components/main/analytics/events';
import { ssoUsersOnlySelector, fetchAppInfoAction } from 'controllers/appInfo';
import { showSuccessNotification, showErrorNotification } from 'controllers/notification';
import formStyles from 'pages/admin/serverSettingsPage/common/formController/formController.scss';
import { fetch } from 'common/utils/fetch';
import { tokenSelector } from 'controllers/auth';
import { URLS } from 'common/urls';
import styles from './ssoUsersForm.scss';

const formCx = classNames.bind(formStyles);
Expand Down Expand Up @@ -60,20 +59,16 @@ const messages = defineMessages({
},
});

const SsoUsersFormComponent = ({
enabled: enabledFromStore,
fetchAppInfo,
token,
dispatchShowSuccessNotification,
dispatchShowErrorNotification,
}) => {
export const SsoUsersForm = () => {
const { formatMessage } = useIntl();
const dispatch = useDispatch();
const enabledFromStore = useSelector(ssoUsersOnlySelector);
const [enabled, setEnabled] = useState(enabledFromStore);
const inputId = 'ssoUsersToggle';

useEffect(() => {
fetchAppInfo();
}, [fetchAppInfo]);
dispatch(fetchAppInfoAction());
}, [dispatch]);

useEffect(() => {
setEnabled(enabledFromStore);
Expand All @@ -83,25 +78,22 @@ const SsoUsersFormComponent = ({
formatMessage(enabled ? messages.ssoOnlyDescription : messages.manualInvitesDescription);

const handleToggle = async (value) => {
setEnabled(value);

try {
await fetch('/api/v1/settings', {
await fetch(URLS.ssoSettings(), {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: token,
},
data: {
key: 'server.users.sso',
value: value.toString(),
},
});

await fetchAppInfo();
setEnabled(value);
dispatchShowSuccessNotification(formatMessage(messages.successNotification));
await dispatch(fetchAppInfoAction());
dispatch(showSuccessNotification({ message: formatMessage(messages.successNotification) }));
} catch (error) {
dispatchShowErrorNotification(formatMessage(messages.errorNotification));
setEnabled(!value);
dispatch(showErrorNotification({ message: formatMessage(messages.errorNotification) }));
}
};

Expand Down Expand Up @@ -134,28 +126,3 @@ const SsoUsersFormComponent = ({
</div>
);
};

SsoUsersFormComponent.propTypes = {
enabled: PropTypes.bool,
fetchAppInfo: PropTypes.func.isRequired,
token: PropTypes.string.isRequired,
dispatchShowSuccessNotification: PropTypes.func.isRequired,
dispatchShowErrorNotification: PropTypes.func.isRequired,
};

SsoUsersFormComponent.defaultProps = {
enabled: false,
};

const mapStateToProps = (state) => ({
enabled: ssoUsersOnlySelector(state),
token: tokenSelector(state),
});

const mapDispatchToProps = (dispatch) => ({
fetchAppInfo: () => dispatch(fetchAppInfoAction()),
dispatchShowSuccessNotification: (message) => dispatch(showSuccessNotification({ message })),
dispatchShowErrorNotification: (message) => dispatch(showErrorNotification({ message })),
});

export const SsoUsersForm = connect(mapStateToProps, mapDispatchToProps)(SsoUsersFormComponent);
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export class InviteUserModal extends Component {
} = this.props;
const data = {};

if (userData.user.externalUser && !this.props.ssoUsersOnly) {
if (userData.user.externalUser) {
data.defaultProject = selectedProject;
data.email = userData.user.userLogin;
data.role = userData.role;
Expand Down Expand Up @@ -237,7 +237,6 @@ export class InviteUserModal extends Component {
selectedProject,
areUserSuggestionsAllowed,
data: { isProjectSelector },
ssoUsersOnly,
} = this.props;
const userData = {
...data,
Expand All @@ -248,19 +247,14 @@ export class InviteUserModal extends Component {
const foundUser = foundUsers?.content.find(({ email }) => email === data.email);
if (foundUser) {
userData.user = makeOptions(data.project, false)({ content: [foundUser] })[0];
} else if (!ssoUsersOnly) {
userData.user = {
userLogin: data.email,
externalUser: true,
};
}
}

const res = await this.inviteUser(userData);
if (res?.errorOccurred) {
return;
}
if (userData.user.externalUser && !ssoUsersOnly) {
if (userData.user.externalUser) {
this.props.showModalAction({
id: 'externalUserInvitationModal',
data: { email: res.email, link: res.backLink },
Expand Down Expand Up @@ -296,7 +290,6 @@ export class InviteUserModal extends Component {
};

const cancelButton = {
// Add this definition
text: intl.formatMessage(COMMON_LOCALE_KEYS.CANCEL),
eventInfo: MEMBERS_PAGE_EVENTS.CANCEL_BTN_INVITE_USER_MODAL,
};
Expand All @@ -315,7 +308,7 @@ export class InviteUserModal extends Component {
{intl.formatMessage(ssoUsersOnly ? messages.descriptionAssign : messages.description)}
</p>
<form className={cx('invite-form')}>
{isProjectSelector || areUserSuggestionsAllowed || ssoUsersOnly ? (
{isProjectSelector || areUserSuggestionsAllowed ? (
<ModalField
label={intl.formatMessage(messages.loginOrEmailLabel)}
labelWidth={LABEL_WIDTH}
Expand All @@ -326,7 +319,7 @@ export class InviteUserModal extends Component {
projectId={selectedProject}
isAdmin={isAdmin}
placeholder={intl.formatMessage(messages.inputPlaceholder)}
ssoUsersOnly={ssoUsersOnly}
creatable={!ssoUsersOnly}
/>
</FieldErrorHint>
</FieldProvider>
Expand Down

0 comments on commit 61dd5c8

Please sign in to comment.