Skip to content

Commit

Permalink
Merge pull request #40 from the-collab-lab/ym-dates-refactor
Browse files Browse the repository at this point in the history
Comprehensive refactor of functions dealing with dates and tests reduction
  • Loading branch information
yiremorlans committed May 24, 2023
2 parents f56fea2 + 6cb9e6b commit fe6136e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 105 deletions.
27 changes: 11 additions & 16 deletions src/components/ListItem.jsx
@@ -1,10 +1,7 @@
import { useState, useEffect } from 'react';
import './ListItem.css';
import { updateItem } from '../api/firebase';
import {
getItemDaysUntilNextPurchase,
getItemDaysSinceLastPurchase,
} from '../utils';
import { getDaysBetweenDates, transformToJSDate } from '../utils';
import { deleteItem } from '../api/firebase';

export function ListItem({ item, listId }) {
Expand Down Expand Up @@ -39,22 +36,20 @@ export function ListItem({ item, listId }) {
}, [checked, item.dateLastPurchased]);

const purchaseUrgencyMessage = (item) => {
if (getItemDaysUntilNextPurchase(item) <= 7) {
const itemDays = getDaysBetweenDates(
transformToJSDate(item.dateNextPurchased),
new Date(),
);
const dateLastPurchased = item.dateLastPurchased;

if (itemDays <= 7) {
return 'S';
} else if (
getItemDaysUntilNextPurchase(item) > 7 &&
getItemDaysUntilNextPurchase(item) < 30
) {
} else if (itemDays > 7 && itemDays < 30) {
return 'KS';
} else if (
getItemDaysUntilNextPurchase(item) >= 30 &&
getItemDaysSinceLastPurchase(item) < 60
) {
} else if (itemDays >= 30 && itemDays < 60) {
return 'NS';
} else if (getItemDaysSinceLastPurchase(item) > 60) {
} else if (itemDays > 60 && !dateLastPurchased) {
return 'I';
} else {
return 'NP';
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/utils/dates.js
Expand Up @@ -32,7 +32,7 @@ export function transformToJSDate(date) {
export function getDaysBetweenDates(startDate, endDate) {
let timeDifference = endDate?.getTime() - startDate?.getTime();
let totalDays = Math.abs(timeDifference / ONE_DAY_IN_MILLISECONDS);
return Math.floor(totalDays);
return Math.ceil(totalDays);
}
/**
* Receives a whole number of days that is estimated from calculateEstimate function
Expand Down
30 changes: 12 additions & 18 deletions src/utils/items.js
@@ -1,21 +1,5 @@
import { transformToJSDate, getDaysBetweenDates } from './dates';

export function getItemDaysUntilNextPurchase(item) {
const today = new Date();
const nextPurchase = transformToJSDate(item.dateNextPurchased);

return getDaysBetweenDates(nextPurchase, today);
}

export function getItemDaysSinceLastPurchase(item) {
const today = new Date();
const lastPurchase = transformToJSDate(
item.dateLastPurchased || item.dateCreated,
);

return getDaysBetweenDates(lastPurchase, today);
}

export function sortItems(data) {
return data.sort((a, b) => {
const currentDate = transformToJSDate(a.dateNextPurchased);
Expand All @@ -34,13 +18,23 @@ export function sortItems(data) {
export function comparePurchaseUrgency(data) {
const inactiveItems = sortItems(
data.filter((item) => {
return getItemDaysSinceLastPurchase(item) >= 60;
return (
getDaysBetweenDates(
transformToJSDate(item.dateLastPurchased || item.dateCreated),
new Date(),
) >= 60
);
}),
);

const activeItems = sortItems(
data.filter((item) => {
return getItemDaysSinceLastPurchase(item) < 60;
return (
getDaysBetweenDates(
transformToJSDate(item.dateLastPurchased || item.dateCreated),
new Date(),
) < 60
);
}),
);

Expand Down
4 changes: 0 additions & 4 deletions src/views/List.jsx
Expand Up @@ -114,10 +114,6 @@ export function List({ data, listId }) {
<span className="legendIcon inline-block">I</span>
<span className="inline-block">Inactive!</span>
</li>
<li className="col-span-3">
<span className="legendIcon inline-block">NP</span>
<span className="inline-block">Not Yet Purchased!</span>
</li>
</ul>
</div>
</div>
Expand Down
67 changes: 1 addition & 66 deletions tests/items.test.jsx
@@ -1,71 +1,6 @@
import {
getItemDaysUntilNextPurchase,
getItemDaysSinceLastPurchase,
sortItems,
comparePurchaseUrgency,
} from '../src/utils';
import { sortItems, comparePurchaseUrgency } from '../src/utils';
import { Timestamp } from 'firebase/firestore';

// tests for getItemDaysUntilNextPurchase function

describe('uses getDaysBetweenDates function to calculate number of days between today and the dateNextPurchased property of an item', () => {
const getNextDate = (daysToAdd) => {
const today = new Date();
const nextDate = new Date(today.setDate(today.getDate() + daysToAdd));
return nextDate;
};
it('receives an item with a date property of today and calculates the number of days between today and the received date', () => {
const item = {
dateNextPurchased: Timestamp.fromDate(new Date(getNextDate(20))),
};
expect(getItemDaysUntilNextPurchase(item)).toEqual(19);
});
it('receives an item with a date property 30 days in the future and returns the number of days between today and that date, not counting today', () => {
let item = {
dateNextPurchased: Timestamp.fromDate(new Date(getNextDate(30))),
};
expect(getItemDaysUntilNextPurchase(item)).toEqual(29);
});

it('receives an object and returns a number type', () => {
const item = {
dateNextPurchased: Timestamp.now(),
};
expect(getItemDaysUntilNextPurchase(item)).toEqual(Number());
});
});

// tests for getItemDaysSinceLastPurchase

describe('the getItemDaysSinceLastPurchase function receives an item object from the database and calculates the number of days between today and either the items dateLastPurchased date property or if no date last purchased then the items date created date property', () => {
const customFireStoreDate = (dateString) =>
Timestamp.fromDate(new Date(dateString));

const getPastDate = (daysToSubtract) => {
const today = new Date();
const pastDate = new Date(today.setDate(today.getDate() - daysToSubtract));
return pastDate;
};

it('given an item with a daysSinceLastPurchase property, then calculates the number of days between today and that date', () => {
const item = {
dateLastPurchased: customFireStoreDate(getPastDate(30)),
dateCreated: customFireStoreDate(getPastDate(60)),
};

expect(getItemDaysSinceLastPurchase(item)).toEqual(30);
});

it('given an item without a daysSinceLastPurchase property, then calculates the number of days between today and the dateCreated date property', () => {
const item = {
dateLastPurchased: null,
dateCreated: customFireStoreDate(getPastDate(60)),
};

expect(getItemDaysSinceLastPurchase(item)).toEqual(60);
});
});

// test for sortItems function
describe('sortItems', () => {
const customFireStoreDate = (dateString) =>
Expand Down

0 comments on commit fe6136e

Please sign in to comment.