From 61fc1cfaaac8ace2dcdc8db576700a946ef4d8f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chy=C5=82a?= Date: Thu, 21 Dec 2023 16:50:51 +0100 Subject: [PATCH] Sort rule by name --- .../DiscountCreateForm/hooks/useRulesHandlers.ts | 4 +++- .../DiscountDetailsForm/hooks/useRulesHandlers.ts | 4 ++-- src/discounts/components/DiscountDetailsForm/utils.ts | 10 ++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/discounts/components/DiscountCreateForm/hooks/useRulesHandlers.ts b/src/discounts/components/DiscountCreateForm/hooks/useRulesHandlers.ts index 2fa4de43e62..d6cfcf1fd3a 100644 --- a/src/discounts/components/DiscountCreateForm/hooks/useRulesHandlers.ts +++ b/src/discounts/components/DiscountCreateForm/hooks/useRulesHandlers.ts @@ -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([]); @@ -17,7 +19,7 @@ export const useRulesHandlers = () => { return rules; }); } else { - setRules([...rules, ruleObj]); + setRules([...rules, ruleObj].sort(ruleAlphabetically)); } }; diff --git a/src/discounts/components/DiscountDetailsForm/hooks/useRulesHandlers.ts b/src/discounts/components/DiscountDetailsForm/hooks/useRulesHandlers.ts index 49e65f9f68b..00331f2a18b 100644 --- a/src/discounts/components/DiscountDetailsForm/hooks/useRulesHandlers.ts +++ b/src/discounts/components/DiscountDetailsForm/hooks/useRulesHandlers.ts @@ -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; @@ -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) => { diff --git a/src/discounts/components/DiscountDetailsForm/utils.ts b/src/discounts/components/DiscountDetailsForm/utils.ts index bdfcaea8fcd..9fb9ea054d3 100644 --- a/src/discounts/components/DiscountDetailsForm/utils.ts +++ b/src/discounts/components/DiscountDetailsForm/utils.ts @@ -12,3 +12,13 @@ export const getCurrentConditionsValuesLabels = (rules: Rule[]) => { return acc; }, {} as Record); }; + +export const ruleAlphabetically = (a: Rule, b: Rule) => { + if (a.name < b.name) { + return -1; + } + if (a.name > b.name) { + return 1; + } + return 0; +};