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

card order and link order #1443

Merged
merged 8 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { LandingPageCard, Loader } from "@egovernments/digit-ui-components";
import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import { EmployeeModuleCard } from "@egovernments/digit-ui-react-components";
import { orderConfig } from "../config/card-link-order";

const DIGIT_UI_CONTEXTS = ["digit-ui", "works-ui", "workbench-ui", "health-ui", "sanitation-ui", "core-ui", "mgramseva-web", "sandbox-ui"];

Expand All @@ -12,6 +13,7 @@ export const RoleBasedEmployeeHome = ({ modules, additionalComponent }) => {
const { t } = useTranslation();
const history = useHistory();
const tenantId = Digit.ULBService.getStateId();
let sortedConfigEmployeesSidebar= null;

const transformURL = (url = "") => {
if (url == "/") {
Expand Down Expand Up @@ -61,6 +63,7 @@ export const RoleBasedEmployeeHome = ({ modules, additionalComponent }) => {
// link: queryParamIndex === -1 ? linkUrl : linkUrl.substring(0, queryParamIndex),
queryParams: queryParamIndex === -1 ? null : linkUrl.substring(queryParamIndex),
label: t(Digit.Utils.locale.getTransformedLocale(`${module}_LINK_${item.displayName}`)),
displayName: item.displayName
});
return acc;
}, {});
Expand All @@ -73,8 +76,49 @@ export const RoleBasedEmployeeHome = ({ modules, additionalComponent }) => {
return "";
}

const children = Object.keys(configEmployeeSideBar)?.map((current, index) => {
const moduleData = configEmployeeSideBar?.[current];
const sortCardAndLink = (configEmployeeSideBar) => {
const sortedModules = Object.keys(configEmployeeSideBar)
.sort((a, b) => {
const cardOrderA = (orderConfig?.order?.cardorder?.[a]) || Number.MAX_SAFE_INTEGER;
const cardOrderB = (orderConfig?.order?.cardorder?.[b]) || Number.MAX_SAFE_INTEGER;
return cardOrderA - cardOrderB;
})
.reduce((acc, module) => {
const sortedLinks = configEmployeeSideBar?.[module]?.links?.sort((linkA, linkB) => {
const labelA = linkA?.displayName;
const labelB = linkB?.displayName;
// Add safety checks for undefined labels
if (!labelA || !labelB) {
console.warn(`Link labels are missing: linkA.label=${labelA}, linkB.label=${labelB}`);
return 0; // Keep the original order if labels are missing
}

const orderA = (orderConfig?.order?.linkorder?.[module]?.[labelA]) || null;
const orderB = (orderConfig?.order?.linkorder?.[module]?.[labelB]) || null;


return orderA - orderB;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Handle null values in link sorting to ensure consistent ordering.

When orderA or orderB are null, subtracting them can lead to unintended sorting behavior. Consider defaulting these values to Number.MAX_SAFE_INTEGER, similar to how it's handled in module sorting. This ensures that links without a specified order are placed at the end.

Apply this diff to adjust the handling of null values:

-          const orderA = (orderConfig?.order?.linkorder?.[module]?.[labelA]) || null;
-          const orderB = (orderConfig?.order?.linkorder?.[module]?.[labelB]) || null;
+          const orderA = (orderConfig?.order?.linkorder?.[module]?.[labelA]) || Number.MAX_SAFE_INTEGER;
+          const orderB = (orderConfig?.order?.linkorder?.[module]?.[labelB]) || Number.MAX_SAFE_INTEGER;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const orderA = (orderConfig?.order?.linkorder?.[module]?.[labelA]) || null;
const orderB = (orderConfig?.order?.linkorder?.[module]?.[labelB]) || null;
return orderA - orderB;
const orderA = (orderConfig?.order?.linkorder?.[module]?.[labelA]) || Number.MAX_SAFE_INTEGER;
const orderB = (orderConfig?.order?.linkorder?.[module]?.[labelB]) || Number.MAX_SAFE_INTEGER;
return orderA - orderB;

});

acc[module] = {
...configEmployeeSideBar[module],
links: sortedLinks,
};

return acc;
}, {});
return sortedModules
};

if(isMultiRootTenant){
sortedConfigEmployeesSidebar= sortCardAndLink(configEmployeeSideBar);
}
else{
sortedConfigEmployeesSidebar = configEmployeeSideBar;
}

const children = Object.keys(sortedConfigEmployeesSidebar)?.map((current, index) => {
const moduleData = sortedConfigEmployeesSidebar?.[current];
const propsForModuleCard = {
// Icon: moduleData?.icon,
icon: "SupervisorAccount",
nabeelmd-eGov marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const orderConfig={
"order": {
"cardorder": {
mithunhegde-egov marked this conversation as resolved.
Show resolved Hide resolved
"HRMS": 1,
"PGR": 2
},
"linkorder": {
"HRMS": {
"HRMS_Create_Employee": 1,
"HRMSInbox": 2,
"Configure_master": 3
},
"PGR": {
"Create_Complaints": 1,
"ComplaintsInbox": 2,
"Configure_master": 3
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Well-organized linkorder structure with room for improvement

The linkorder property effectively organizes links for each module with sequential numbering, allowing for easy insertion of new links if needed.

Consider the following improvements:

  1. Extract common links like "Configure Masters" into a separate configuration object for reusability and consistency across modules.
  2. Implement a mechanism to load this configuration from an external source (e.g., a JSON file or environment variables) for easier updates without code changes.
  3. Add support for environment-specific configurations, allowing different orderings for development, staging, and production environments.

These enhancements would make the system more adaptable to changing requirements without necessitating code modifications.

}
}