Skip to content

Commit

Permalink
refactor: use ternary operator instead of if statements in useGoalSum…
Browse files Browse the repository at this point in the history
…maryStore
  • Loading branch information
vinaybadgujar102 committed Jun 9, 2024
1 parent 1c168bd commit c91e761
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import useGoalSummaryStore from "@src/hooks/useGoalSummary";
import useGoalSummaryStore from "@src/hooks/useGoalSummaryStore";
import { GoalItem } from "@src/models/GoalItem";
import React from "react";

Expand Down
55 changes: 0 additions & 55 deletions src/hooks/useGoalSummary.ts

This file was deleted.

35 changes: 35 additions & 0 deletions src/hooks/useGoalSummaryStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { GoalItem } from "@src/models/GoalItem";
import { formatSingularPlural, calculateDaysLeft } from "@src/utils";
import { useTranslation } from "react-i18next";
import { useSublistSummary } from "./useSublistSummary";

const useGoalSummaryStore = (goal: GoalItem) => {
const { t } = useTranslation();
const { subGoalsCount, subBudgetsCount } = useSublistSummary({ goal });

const getSublistSummaryText = () =>
goal.due
? ""
: subGoalsCount > 0 && subBudgetsCount === 0
? formatSingularPlural(subGoalsCount, "goal")
: subBudgetsCount > 0 && subGoalsCount === 0
? formatSingularPlural(subBudgetsCount, "budget")
: subGoalsCount > 0 && subBudgetsCount > 0
? `${formatSingularPlural(subGoalsCount, "goal")}, ${formatSingularPlural(subBudgetsCount, "budget")}`
: "";

const getDurationSummaryText = () => (goal.duration ? t("hourWithCount", { count: Number(goal.duration) }) : "");

const getDueDateSummaryText = () => (goal.due ? calculateDaysLeft(goal.due) : "");

const getHabitSummaryText = () => (goal.habit === "weekly" ? t("everyWeek") : "");

return {
sublistSummary: getSublistSummaryText(),
durationSummary: getDurationSummaryText(),
dueDateSummary: getDueDateSummaryText(),
habitSummary: getHabitSummaryText(),
};
};

export default useGoalSummaryStore;

0 comments on commit c91e761

Please sign in to comment.