DEV Community

Cover image for Build an Ovulation Calculator App with Next.js, TypeScript, Tailwind CSS and useReducer Hook
Code of Relevancy
Code of Relevancy

Posted on • Edited on

Build an Ovulation Calculator App with Next.js, TypeScript, Tailwind CSS and useReducer Hook

We will be showcasing a powerful ovulation calculator app built using Next.js, TypeScript, Tailwind CSS, and the useReducer Hook. This app is designed to help women pinpoint their most fertile days by identifying when they are likely ovulating. With this tool, you can better understand your own menstrual cycle, as it takes into account the varying lengths and dates of periods from person to person.

How to use this app:

To use the app, simply enter your last period date and cycle length, and the app will automatically calculate your fertile window, ovulation date, next period date, pregnancy test date, and expected due date. It's a simple, yet powerful tool that can help you better understand and track your menstrual cycle. The app is user-friendly and easy to navigate, making it a valuable resource for anyone looking to plan or prevent pregnancy.


Tutorial and Basic Source Code:


Learn more about Ovulation Calculator

What is Fertile Window?

The fertile window is the period of time during a woman's menstrual cycle when she is most likely to conceive. This typically occurs in the days leading up to and including ovulation, when an egg is released from the ovary. The fertile window is usually determined by tracking changes in cervical mucus and basal body temperature, and can also be predicted by ovulation predictor kits or fertility tracking apps. It is important to note that the fertile window can vary from woman to woman and cycle to cycle, and there is no guaranteed "safe" period for intercourse if a woman wishes to avoid pregnancy.

What is Ovulation Date?

The ovulation date is the day that a woman releases an egg from her ovary, which is the most fertile time of her menstrual cycle. Ovulation typically occurs around day 14 of a 28-day menstrual cycle, but this can vary depending on the length of a woman's cycle. Ovulation can be predicted by tracking changes in cervical mucus, basal body temperature, and hormone levels. Ovulation predictor kits and fertility tracking apps can also be used to determine the ovulation date.

What is Next Period Date?

The next period date is the date on which a woman's menstrual cycle is expected to begin again. This date is typically calculated based on the length of a woman's menstrual cycle, which is the number of days from the first day of one period to the first day of the next period. The average menstrual cycle is 28 days, but this can vary from woman to woman and cycle to cycle.

To calculate the next period date, you can start by identifying the first day of your last period, and then add the number of days in your menstrual cycle. For example, if your last period began on the 1st of the month and your cycle is 28 days, your next period is expected to begin on the 29th of the same month.

What is Pregnancy Test Date?

A pregnancy test date is the date on which a woman plans to take a pregnancy test to determine if she is pregnant. This date is typically determined based on the timing of a woman's menstrual cycle, and the earliest date that a pregnancy test can accurately detect the presence of the pregnancy hormone, human chorionic gonadotropin (hCG) in the urine.

What is Expected Due Date?

The expected due date, also known as the estimated due date (EDD), is the date on which a woman is expected to give birth. This date is typically calculated based on the first day of a woman's last menstrual period (LMP) and is usually calculated as 40 weeks or 280 days from the first day of the LMP. However, only about 5% of women will give birth on their due date, most women give birth within a window of two weeks before or after the due date.


Technologies

Next.js:

Next.js is a framework that allows for the development of server-rendered React applications, providing a powerful and efficient tool for building web apps. TypeScript, on the other hand, is a superset of JavaScript that adds static typing and other features that make it a great choice for building large-scale applications.

Tailwind CSS:

The app designed using Tailwind CSS, a utility-first CSS framework that makes it easy to build responsive, scalable and consistent interfaces. Tailwind CSS provides a set of pre-defined classes that allow developers to quickly and easily create UI elements that are visually consistent across the app.

TypeScript:

TypeScript is an open-source programming language that is a superset of JavaScript. It was developed and is maintained by Microsoft. TypeScript adds optional static typing, class-based object-oriented programming, and other features to JavaScript. This allows for better code organization, improved developer productivity, and more robust code.


Code

useReducer hook:

The useReducer hook is generally considered to be a better choice when managing complex state in a React application. It allows you to handle state updates in a more centralized and predictable way, and also makes it easier to manage updates to state that are dependent on the current state. However, it might be an overkill for small or simple cases, in these cases useState Hook is more than enough.

Image description


useEffect hook:

Calculator component uses the useEffect Hook to create a side effect that sets the outcomes when the lastPeriod or cycleLength state values change.

Image description

The useEffect Hook takes in a callback function, which is executed when the component is rendered for the first time and subsequently whenever the values of the lastPeriod or cycleLength state variables change.

The callback function first checks if the lastPeriod or cycleLength state variables have been defined (i.e. not null or undefined) using the logical NOT operator (!). If either of them are not defined, the function returns and the effect is not executed.

Otherwise, the callback function dispatches an action to the outcomes reducer using the dispatchOutcomes function, which is passed as a prop. The action has a type of 'SET_OUTCOMES' and a payload of the result of calling the calculateOutcomes function, which takes in lastPeriod and cycleLength as arguments.

The second argument of the useEffect Hook is an array of dependencies that the Hook should watch for changes. In this case, it is watching for changes in the lastPeriod and cycleLength state variables, so that the effect is only executed when those values change.


Date formatting helper:

The toFormatted function takes in a date and an optional formatString as arguments. The formatString defaults to DATE_FORMAT if it is not provided. The function first checks if the date is valid using the isValid function. If the date is not valid, the function returns null. If the date is valid, the function returns the date formatted using the format function of date-fns library.

Image description


Please consider following and supporting us by subscribing to our channel. Your support is greatly appreciated and will help us continue creating content for you to enjoy. Thank you in advance for your support!

YouTube
Discord
GitHub

Top comments (0)