diff --git a/__test__/index.test.js b/__test__/index.test.js index 32d57a235..2e4e7dd2d 100644 --- a/__test__/index.test.js +++ b/__test__/index.test.js @@ -1,4 +1,4 @@ -const timeout = 15000; +const timeout = 25000; describe('Dummy Test ', () => { beforeAll(async () => { diff --git a/__test__/utils.test.js b/__test__/utils.test.js new file mode 100644 index 000000000..126f49f95 --- /dev/null +++ b/__test__/utils.test.js @@ -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 }); + }); + }); +}); diff --git a/css/intro.css b/css/intro.css index 0d5c7248f..ace9a40f4 100644 --- a/css/intro.css +++ b/css/intro.css @@ -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 { diff --git a/js/intro.js b/js/intro.js index ba5b7286a..e915926da 100644 --- a/js/intro.js +++ b/js/intro.js @@ -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'], @@ -240,6 +267,15 @@ 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(), + ); + if (usersRequest.status === 200) { const userData = usersRequest.data.data[0]; let userSavedData = { @@ -256,6 +292,7 @@ async function showSavedDetails() { whyRds: userData.intro.whyRds, numberOfHours: userData.intro.numberOfHours, foundFrom: userData.foundFrom, + dateDiff: dateDiff, }; generateSavedDetailsForm(userSavedData); } else if (usersRequest.status === 404) { diff --git a/js/utils.js b/js/utils.js index 28c96254e..dc34a2e05 100644 --- a/js/utils.js +++ b/js/utils.js @@ -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, +};