Skip to content

Commit

Permalink
refactor: optimizing interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dlcastillop committed Oct 7, 2023
1 parent 1c1fdca commit e3b6020
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 28 deletions.
28 changes: 10 additions & 18 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import { useTimer } from "./hooks";
import { useCountDown } from "./hooks";

const App = () => {
const stopwatch = useTimer(1, 0, 10, 50);
const counter = useCountDown(0, 10);

return (
<div>
<p>Stopwatch value: {stopwatch.current.withLeadingZero}</p>
<p>Stopwatch value: {stopwatch.current.withoutLeadingZero}</p>
<p>Remaining time: {stopwatch.elapsedTime.withLeadingZero}</p>
<p>Remaining time: {stopwatch.elapsedTime.withoutLeadingZero}</p>
<p>Days: {stopwatch.currentDays}</p>
<p>Hours: {stopwatch.currentHours}</p>
<p>Minutes: {stopwatch.currentMinutes}</p>
<p>Seconds: {stopwatch.currentSeconds}</p>
<p>Elapsed seconds: {stopwatch.elapsedSeconds}</p>
<p>Remaining seconds: {stopwatch.remainingSeconds}</p>
<p>Is the counter paused? {stopwatch.isPaused ? "Yes" : "No"}</p>
<p>Has the counter over? {stopwatch.isOver ? "Yes" : "No"}</p>
<button onClick={stopwatch.pause}>Pause</button>
<button onClick={stopwatch.play}>Play</button>
<button onClick={stopwatch.reset}>Reset</button>
<button onClick={stopwatch.togglePause}>Toggle Pause</button>
<p>Counter value: {counter.current.withLeadingZero}</p>
<p>Counter value: {counter.current.withoutLeadingZero}</p>
<p>Is the counter paused? {counter.isPaused ? "Yes" : "No"}</p>
<p>Has the counter over? {counter.isOver ? "Yes" : "No"}</p>
<button onClick={counter.pause}>Pause</button>
<button onClick={counter.play}>Play</button>
<button onClick={counter.reset}>Reset</button>
<button onClick={counter.togglePause}>Toggle Pause</button>
</div>
);
};
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/useInternalStopwatch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect } from "react";
import { addLeadingZero } from "../helpers";
import { IStopwatch } from "../interfaces";
import { IInternal } from "../interfaces";

export const useInternalStopwatch = (
days: number,
Expand All @@ -9,7 +9,7 @@ export const useInternalStopwatch = (
seconds: number,
startPaused?: boolean,
separator?: string
): IStopwatch => {
): IInternal => {
const [time, setTime] = useState({
days: 0,
hours: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/useInternalTimer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect } from "react";
import { addLeadingZero } from "../helpers";
import { ITimer } from "../interfaces";
import { IInternal } from "../interfaces";

export const useInternalTimer = (
days: number,
Expand All @@ -9,7 +9,7 @@ export const useInternalTimer = (
seconds: number,
startPaused?: boolean,
separator?: string
): ITimer => {
): IInternal => {
if (days < 0) {
throw new Error("The days parameter has to be more or equal than 0.");
} else if (hours < 0 || hours >= 24) {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useUnlimitedStopwatch.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useState, useEffect } from "react";
import { addLeadingZero } from "../helpers";
import { IStopwatch } from "../interfaces";
import { IUnlimitedStopwatch } from "../interfaces";

export const useUnlimitedStopwatch = (
startPaused?: boolean,
separator?: string
): IStopwatch => {
): IUnlimitedStopwatch => {
const [time, setTime] = useState({
days: 0,
hours: 0,
Expand Down
24 changes: 20 additions & 4 deletions src/interfaces/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,39 @@ interface IBaseCounterStatus {
currentMinutes: number;
currentSeconds: number;
elapsedSeconds: number;
remainingSeconds?: number;
remainingSeconds: number;
}

export interface IBaseCounter {
current: IZero;
isPaused: boolean;
isOver?: boolean;
isOver: boolean;
pause: () => void;
play: () => void;
reset: () => void;
togglePause: () => void;
}

export interface IInternal extends IBaseCounter, IBaseCounterStatus {}

export interface IStopwatch extends IBaseCounter, IBaseCounterStatus {
remainingTime?: IZero;
remainingTime: IZero;
}

export interface ITimer extends IBaseCounter, IBaseCounterStatus {
elapsedTime?: IZero;
elapsedTime: IZero;
}

export interface IUnlimitedStopwatch {
current: IZero;
isPaused: boolean;
currentDays: number;
currentHours: number;
currentMinutes: number;
currentSeconds: number;
elapsedSeconds: number;
pause: () => void;
play: () => void;
reset: () => void;
togglePause: () => void;
}

0 comments on commit e3b6020

Please sign in to comment.