Skip to content
Merged
2 changes: 1 addition & 1 deletion __test__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const timeout = 15000;
const timeout = 25000;

describe('Dummy Test ', () => {
beforeAll(async () => {
Expand Down
37 changes: 37 additions & 0 deletions __test__/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const utils = require('../js/utils');

describe('Utils function test case', () => {
describe('Difference between two dates', function () {
test('should handle negative differences correctly', () => {
const startDate = 1691260200000; // 6 August 2023
const endDate = 1674153000000; // 20 January 2023
const difference = utils.dateDifference(startDate, endDate);

expect(difference).toEqual({ years: 0, months: 6, days: 17 });
});

test('should return correct date difference object', () => {
const startDate = 1674153000000; // 20 January 2023
const endDate = 1691260200000; // 6 August 2023
const difference = utils.dateDifference(startDate, endDate);

expect(difference).toEqual({ years: 0, months: 6, days: 17 });
});

test('should handle date in the same month correctly', () => {
const startDate = 1674153000000; // 20 January 2023
const endDate = 1674844200000; // 28 January 2023
const difference = utils.dateDifference(startDate, endDate);

expect(difference).toEqual({ years: 0, months: 0, days: 8 });
});

test('should handle date in different years correctly', () => {
const startDate = 1640889000000; // 31 December 2021
const endDate = 1640975400000; // 1 January 2022
const difference = utils.dateDifference(startDate, endDate);

expect(difference).toEqual({ years: 0, months: 0, days: 1 });
});
});
});
11 changes: 11 additions & 0 deletions css/intro.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@

.user-input {
line-height: 1.5rem;
align-items: center;
}

.github-created-alert {
color: var(--color-red);
text-align: center;
}

.github-created-text {
color: var(--color-darkest-grey);
text-align: center;
}

.loading {
Expand Down
39 changes: 39 additions & 0 deletions js/intro.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,33 @@ function generateSavedDetailsForm(users) {
const container = createElement({ type: 'div', classList: ['container'] });
renderIntroPage.appendChild(container);

const githubCreatedValue = createElement({
type: 'h4',
classList: [
`${
users.dateDiff.years > 0
? 'github-created-text'
: 'github-created-alert'
}`,
],
});

if (users.dateDiff.years > 0) {
githubCreatedValue.innerText = `User GitHub account created ${users.dateDiff.years} years ago`;
} else if (users.dateDiff.years === 0 && users.dateDiff.months > 0) {
githubCreatedValue.innerText = `User GitHub account created ${users.dateDiff.months} months ago`;
} else if (
users.dateDiff.years === 0 &&
users.dateDiff.months === 0 &&
users.dateDiff.days >= 0
) {
githubCreatedValue.innerText = `User GitHub account created ${
users.dateDiff.days > 0 ? users.dateDiff.days : '1'
} days ago`;
}

container.appendChild(githubCreatedValue);

const nameLabel = createElement({
type: 'p',
classList: ['input-label-dark'],
Expand Down Expand Up @@ -240,6 +267,17 @@ async function showSavedDetails() {
try {
const userId = urlParams.get('id');
const usersRequest = await makeApiCall(`${BASE_URL}/users/${userId}/intro`);
const userInformation = await makeApiCall(
`${BASE_URL}/users/userId/${userId}`,
);

const dateDiff = dateDifference(
userInformation?.data.user?.github_created_at,
new Date().getTime(),
);

console.log(usersRequest.data.data[0]);

if (usersRequest.status === 200) {
const userData = usersRequest.data.data[0];
let userSavedData = {
Expand All @@ -256,6 +294,7 @@ async function showSavedDetails() {
whyRds: userData.intro.whyRds,
numberOfHours: userData.intro.numberOfHours,
foundFrom: userData.foundFrom,
dateDiff: dateDiff,
};
generateSavedDetailsForm(userSavedData);
} else if (usersRequest.status === 404) {
Expand Down
34 changes: 34 additions & 0 deletions js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,37 @@ function createElement({ type, classList = [], id }) {
element.id = id ?? true;
return element;
}

function dateDifference(startDate, endDate) {
let start = new Date(startDate);
let end = new Date(endDate);

if (start > end) {
[start, end] = [end, start];
}

let yearDiff = end.getFullYear() - start.getFullYear();
let monthDiff = end.getMonth() - start.getMonth();
let dayDiff = end.getDate() - start.getDate();

if (dayDiff < 0) {
monthDiff--;
const lastMonth = new Date(end.getFullYear(), end.getMonth(), 0);
dayDiff = lastMonth.getDate() + dayDiff;
}

if (monthDiff < 0) {
yearDiff--;
monthDiff = 12 + monthDiff;
}

return {
years: yearDiff,
months: monthDiff,
days: dayDiff,
};
}

module.exports = {
dateDifference,
};