Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: changed multi select custom component in MF #2519

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 0 additions & 120 deletions forms-flow-web/src/components/CustomComponents/MultiSelect.js

This file was deleted.

8 changes: 5 additions & 3 deletions forms-flow-web/src/components/Form/EditForm/FormEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ import NewVersionModal from "../../Modals/NewVersionModal";
import { currentFormReducer } from "../../../modules/formReducer.js";
import { toast } from "react-toastify";
import userRoles from "../../../constants/permissions.js";
import { generateUniqueId, isFormComponentsChanged, addTenantkey, textTruncate } from "../../../helper/helper.js";
import { generateUniqueId, isFormComponentsChanged, addTenantkey, textTruncate,
convertMultiSelectOptionToValue } from "../../../helper/helper.js";
import { useMutation } from "react-query";
import NavigateBlocker from "../../CustomComponents/NavigateBlocker";
import { setProcessData, setFormPreviosData, setFormProcessesData } from "../../../actions/processActions.js";
Expand Down Expand Up @@ -538,12 +539,13 @@ const handleSaveLayout = () => {


/* ----------- save settings function to be used in settings modal ---------- */

const filterAuthorizationData = (authorizationData) => {
if(authorizationData.selectedOption === "submitter"){
return {roles: [], userName:null, resourceDetails:{submitter:true}};
}
if (authorizationData.selectedOption === "specifiedRoles") {
return { roles: authorizationData.selectedRoles, userName: "" };
return { roles: convertMultiSelectOptionToValue(authorizationData.selectedRoles, "role"), userName: "" };
}
return { roles: [], userName: preferred_username };
};
Expand Down Expand Up @@ -582,7 +584,7 @@ const handleSaveLayout = () => {
resourceDetails: {},
roles:
rolesState.FORM.selectedOption === "specifiedRoles"
? rolesState.FORM.selectedRoles
? convertMultiSelectOptionToValue(rolesState.FORM.selectedRoles, "role")
: [],
},
};
Expand Down
77 changes: 44 additions & 33 deletions forms-flow-web/src/components/Form/EditForm/FormSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
CustomRadioButton,
FormInput,
FormTextArea,
MultipleSelect
} from "@formsflow/components";

import MultiSelectComponent from "../../CustomComponents/MultiSelect";
import { MULTITENANCY_ENABLED } from "../../../constants/constants";
import { addTenantkeyAsSuffix } from "../../../helper/helper";
import { addTenantkeyAsSuffix, convertSelectedValueToMultiSelectOption } from "../../../helper/helper";
import { useDispatch, useSelector } from "react-redux";
import { getUserRoles } from "../../../apiManager/services/authorizationService";
import { useTranslation } from "react-i18next";
Expand Down Expand Up @@ -64,22 +64,26 @@ const FormSettings = forwardRef((props, ref) => {

const publicUrlPath = `${window.location.origin}/public/form/`;
const [urlPath,setUrlPath] = useState(publicUrlPath);
const setSelectedOption = (roles, option)=> roles.length ? "specifiedRoles" : option;
const setSelectedOption = (option, roles = [])=> roles.length ? "specifiedRoles" : option;
const multiSelectOptionKey = "role";
/* ------------------------- authorization variables ------------------------ */
const [rolesState, setRolesState] = useState({
DESIGN: {
selectedRoles: formAuthorization.DESIGNER?.roles,
selectedOption: setSelectedOption(formAuthorization.DESIGNER?.roles,"onlyYou"),
selectedRoles: convertSelectedValueToMultiSelectOption(formAuthorization.DESIGNER?.roles,
multiSelectOptionKey),
selectedOption: setSelectedOption("onlyYou", formAuthorization.DESIGNER?.roles),
},
FORM: {
roleInput: "",
selectedRoles: formAuthorization.FORM?.roles,
selectedOption: setSelectedOption(formAuthorization.FORM?.roles,"registeredUsers"),
selectedRoles: convertSelectedValueToMultiSelectOption(formAuthorization.FORM?.roles,
multiSelectOptionKey),
selectedOption: setSelectedOption("registeredUsers", formAuthorization.FORM?.roles),
},
APPLICATION: {
roleInput: "",
selectedRoles: formAuthorization.APPLICATION?.roles,
selectedOption: setSelectedOption(formAuthorization.APPLICATION?.roles, "submitter"),
selectedRoles: convertSelectedValueToMultiSelectOption(formAuthorization.APPLICATION?.roles,
multiSelectOptionKey),
selectedOption: setSelectedOption("submitter", formAuthorization.APPLICATION?.roles),
/* The 'submitter' key is stored in 'resourceDetails'. If the roles array is not empty
we assume that the submitter is true. */
}
Expand Down Expand Up @@ -154,15 +158,15 @@ const FormSettings = forwardRef((props, ref) => {
.then((res) => {
if (res) {
const { data = [] } = res;
setUserRoles(data.map((role) => role.name));
setUserRoles(data.map((role,index) => ({[multiSelectOptionKey]:role.name, id: index})));
}
})
.catch((error) => console.error("error", error));
}, [dispatch]);



const handleRoleStateChange = (section, key, value) => {
const handleRoleStateChange = (section, key, value = []) => {
setRolesState((prevState) => ({
...prevState,
[section]: {
Expand Down Expand Up @@ -210,6 +214,10 @@ const FormSettings = forwardRef((props, ref) => {
props.setIsSaveButtonDisabled(shouldDisableSaveButton);
}, [rolesState, errors, formDetails]);

const handleRoleSelectForDesign = (roles) => handleRoleStateChange(DESIGN, "selectedRoles", roles);
const handleRoleSelectForForm = (roles) => handleRoleStateChange(FORM, "selectedRoles", roles);
const handleRoleSelectForApplication = (roles) =>
handleRoleStateChange(APPLICATION, "selectedRoles", roles);

return (
<>
Expand Down Expand Up @@ -289,14 +297,15 @@ const FormSettings = forwardRef((props, ref) => {
<FormInput disabled={true} />
)}
{rolesState.DESIGN.selectedOption === "specifiedRoles" && (
<MultiSelectComponent
openByDefault
allRoles={userRoles}
selectedRoles={rolesState.DESIGN.selectedRoles}
setSelectedRoles={(roles) =>
handleRoleStateChange(DESIGN, "selectedRoles", roles)
}
<MultipleSelect
options={userRoles}
selectedValues={rolesState.DESIGN.selectedRoles}
onSelect={handleRoleSelectForDesign}
onRemove={handleRoleSelectForDesign}
displayValue={multiSelectOptionKey}
avoidHighlightFirstOption={true}
/>

)}

<Form.Label className="field-label mt-3">
Expand Down Expand Up @@ -338,14 +347,15 @@ const FormSettings = forwardRef((props, ref) => {
<FormInput disabled={true} />
)}
{rolesState.FORM.selectedOption === "specifiedRoles" && (
<MultiSelectComponent
openByDefault
allRoles={userRoles}
selectedRoles={rolesState.FORM.selectedRoles}
setSelectedRoles={(roles) =>
handleRoleStateChange(FORM, "selectedRoles", roles)
}
/>
<MultipleSelect
options={userRoles} // Options to display in the dropdown
selectedValues={rolesState.FORM.selectedRoles} // Preselected value to persist in dropdown
onSelect={handleRoleSelectForForm} // Function will trigger on select event
onRemove={handleRoleSelectForForm} // Function will trigger on remove event
displayValue={multiSelectOptionKey} // Property name to display in the dropdown options
avoidHighlightFirstOption={true}
/>

)}

<Form.Label className="field-label mt-3">
Expand Down Expand Up @@ -378,14 +388,15 @@ const FormSettings = forwardRef((props, ref) => {
)}

{rolesState.APPLICATION.selectedOption === "specifiedRoles" && (
<MultiSelectComponent
openByDefault
allRoles={userRoles}
selectedRoles={rolesState.APPLICATION.selectedRoles}
setSelectedRoles={(roles) =>
handleRoleStateChange(APPLICATION, "selectedRoles", roles)
}
<MultipleSelect
options={userRoles} // Options to display in the dropdown
selectedValues={rolesState.APPLICATION.selectedRoles} // Preselected value to persist in dropdown
onSelect={handleRoleSelectForApplication} // Function will trigger on select event
onRemove={handleRoleSelectForApplication} // Function will trigger on remove event
displayValue={multiSelectOptionKey} // Property name to display in the dropdown options
avoidHighlightFirstOption={true}
/>

)}
</div>

Expand Down
21 changes: 19 additions & 2 deletions forms-flow-web/src/helper/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,22 @@ const addTenantkeyAsSuffix = (value, tenantkey) => {
}
};

export { generateUniqueId, replaceUrl, addTenantkey, removeTenantKey, textTruncate, renderPage,
filterSelectOptionByLabel, isFormComponentsChanged,addTenantkeyAsSuffix};
/* ----------------- convert data from and into multiselect ----------------- */
const convertMultiSelectOptionToValue = (selectedValues = [], key = null) =>
selectedValues.map(i=> i[key]);

const convertSelectedValueToMultiSelectOption = (values = [], key = null) =>
values.map((value)=>({[key]:value, id:_.uniqueId(value)}));
/* ----------------------------------- --- ---------------------------------- */
export { generateUniqueId,
replaceUrl,
addTenantkey,
removeTenantKey,
textTruncate,
renderPage,
filterSelectOptionByLabel,
isFormComponentsChanged,
addTenantkeyAsSuffix,
convertMultiSelectOptionToValue,
convertSelectedValueToMultiSelectOption
};
Loading