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

Fixed case if user removes all cycles #193

Closed
wants to merge 3 commits into from
Closed
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
26 changes: 7 additions & 19 deletions src/pages/TabHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
getActiveDates,
getPastFuturePeriodDays,
isPeriodToday,
isMarkedFutureDays,
getForecastPeriodDays,
getOvulationDays,
} from "../state/CalculationLogics";
Expand Down Expand Up @@ -139,7 +138,6 @@ const ViewCalendar = (props: SelectCalendarProps) => {

const EditCalendar = (props: SelectCalendarProps) => {
const datetimeRef = useRef<null | HTMLIonDatetimeElement>(null);
const [cannotSaveAlert] = useIonAlert();

const { t } = useTranslation();
const { cycles, updateCycles } = useContext(CyclesContext);
Expand Down Expand Up @@ -178,26 +176,16 @@ const EditCalendar = (props: SelectCalendarProps) => {
<IonButton
color="blackout-basic"
onClick={() => {
// NOTE: `confirm` should be called to update values in `datetimeRef`
datetimeRef.current?.confirm().catch((err) => console.error(err));
if (datetimeRef.current?.value) {
const periodDaysString = (
datetimeRef.current.value as string[]
).map((isoDateString) => {
return parseISO(isoDateString).toString();
});

if (isMarkedFutureDays(periodDaysString)) {
datetimeRef.current.value = getLastPeriodDays(cycles);
const periodDaysString = (
(datetimeRef.current?.value as string[]) ?? []
).map((isoDateString) => {
return parseISO(isoDateString).toString();
});

cannotSaveAlert({
header: t("You can't mark future days"),
buttons: ["OK"],
}).catch((err) => console.error(err));

return;
}
updateCycles(getNewCyclesHistory(periodDaysString));
}
updateCycles(getNewCyclesHistory(periodDaysString));
props.setIsEditCalendar(false);
}}
>
Expand Down
19 changes: 0 additions & 19 deletions src/state/CalculationLogics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
parseISO,
startOfDay,
startOfToday,
differenceInMilliseconds,
} from "date-fns";

import { Cycle } from "../data/ClassCycle";
Expand Down Expand Up @@ -411,24 +410,6 @@ export function isPeriodToday(cycles: Cycle[]) {
return dayOfCycle <= cycles[0].periodLength;
}

export function isMarkedFutureDays(periodDays: string[]) {
periodDays.sort((left, right) => {
const leftDate = startOfDay(new Date(left));
const rightDate = startOfDay(new Date(right));
return differenceInMilliseconds(leftDate, rightDate);
});

const today = startOfToday();

if (periodDays.includes(today.toString())) {
return false;
}

return periodDays.some((date) => {
return startOfDay(new Date(date)) > today;
});
}

export function getOvulationDays(cycles: Cycle[]) {
if (cycles.length < 2) {
return [];
Expand Down
46 changes: 0 additions & 46 deletions src/tests/CalculationLogic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
getPastFuturePeriodDays,
getLastStartDate,
getLengthOfLastPeriod,
isMarkedFutureDays,
getForecastPeriodDays,
getOvulationDays,
} from "../state/CalculationLogics";
Expand Down Expand Up @@ -987,51 +986,6 @@ describe("getLengthOfLastPeriod", () => {
});
});

describe("isMarkedFutureDays", () => {
test("nothing marked", () => {
expect(isMarkedFutureDays([])).toEqual(false);
});

test("past periods marked", () => {
// @ts-expect-error mocked `t` method
jest.spyOn(i18n, "t").mockImplementation((key) => key);
const dates: string[] = [];

for (let i = 5; i < 10; ++i) {
dates.push(subDays(startOfToday(), i).toString());
}

expect(isMarkedFutureDays(dates)).toEqual(false);
});

test("period now marked", () => {
// @ts-expect-error mocked `t` method
jest.spyOn(i18n, "t").mockImplementation((key) => key);
const dates: string[] = [];

for (let i = 0; i < 3; ++i) {
dates.push(subDays(startOfToday(), i).toString());
}
for (let i = 1; i < 3; ++i) {
dates.push(addDays(startOfToday(), i).toString());
}

expect(isMarkedFutureDays(dates)).toEqual(false);
});

test("future days are marked", () => {
// @ts-expect-error mocked `t` method
jest.spyOn(i18n, "t").mockImplementation((key) => key);
const dates: string[] = [];

for (let i = 1; i < 6; ++i) {
dates.push(addDays(startOfToday(), i).toString());
}

expect(isMarkedFutureDays(dates)).toEqual(true);
});
});

describe("getForecastPeriodDays", () => {
test("cycles array is empty", () => {
// @ts-expect-error mocked `t` method
Expand Down
1 change: 0 additions & 1 deletion src/utils/translations/ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const ru = {
High: "Высокая",
Low: "Низкая",
"Period today": "Месячные сегодня",
"You can't mark future days": "Вы не можете отметить будущие дни",
edit: "редактировать",
save: "сохранить",
"Period is": "Месячные",
Expand Down