Skip to content

Commit

Permalink
Sort rule by name
Browse files Browse the repository at this point in the history
  • Loading branch information
poulch committed Dec 21, 2023
1 parent 1152040 commit 61fc1cf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Rule } from "@dashboard/discounts/models";
import { useState } from "react";

import { ruleAlphabetically } from "../../DiscountDetailsForm/utils";

export const useRulesHandlers = () => {
const [rules, setRules] = useState<Rule[]>([]);

Expand All @@ -17,7 +19,7 @@ export const useRulesHandlers = () => {
return rules;
});
} else {
setRules([...rules, ruleObj]);
setRules([...rules, ruleObj].sort(ruleAlphabetically));
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { CommonError } from "@dashboard/utils/errors/common";
import { useEffect, useState } from "react";

import { getCurrentConditionsValuesLabels } from "../utils";
import { getCurrentConditionsValuesLabels, ruleAlphabetically } from "../utils";

interface UseRulesHandlersProps {
data: PromotionDetailsFragment | undefined | null;
Expand Down Expand Up @@ -65,7 +65,7 @@ export const useRulesHandlers = ({

const addNewRuleToArray = (rule: Rule) => {
updateLabels(rule);
setRules(prevRules => [...prevRules, rule]);
setRules(prevRules => [...prevRules, rule].sort(ruleAlphabetically));
};

const removeRuleFromArray = (index: number) => {
Expand Down
10 changes: 10 additions & 0 deletions src/discounts/components/DiscountDetailsForm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ export const getCurrentConditionsValuesLabels = (rules: Rule[]) => {
return acc;
}, {} as Record<string, string>);
};

export const ruleAlphabetically = (a: Rule, b: Rule) => {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
};

0 comments on commit 61fc1cf

Please sign in to comment.