From 827dbceadb0be1044c83107361785d36289dcf74 Mon Sep 17 00:00:00 2001 From: Ravi kumar <1002kumarravi@gmail.com> Date: Thu, 3 Aug 2023 17:29:44 +0530 Subject: [PATCH 01/11] feat: <1 year github red flag for new joiner --- css/intro.css | 11 +++++++++++ js/intro.js | 27 +++++++++++++++++++++++++-- js/utils.js | 23 +++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) 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..c0c4c017f 100644 --- a/js/intro.js +++ b/js/intro.js @@ -64,7 +64,7 @@ function generateNoDataFoundPage() { notFound.appendChild(notFoundDiv); } -function generateSavedDetailsForm(users) { +function generateSavedDetailsForm(users, dateDiff = NaN) { const renderIntroPage = createElement({ type: 'section', classList: ['render-page'], @@ -77,6 +77,20 @@ function generateSavedDetailsForm(users) { const container = createElement({ type: 'div', classList: ['container'] }); renderIntroPage.appendChild(container); + const githubStatusValue = createElement({ + type: 'h4', + classList: [ + `${dateDiff.years > 0 ? 'github-created-text' : 'github-created-alert'}`, + ], + id: 'ghInfo', + }); + if (dateDiff.years || dateDiff.months) { + githubStatusValue.innerText = `User GitHub account created ${ + dateDiff.years > 0 ? 'more then a year' : `${dateDiff.months} months` + } ago`; + } + container.appendChild(githubStatusValue); + const nameLabel = createElement({ type: 'p', classList: ['input-label-dark'], @@ -240,6 +254,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 = getDateDifferenceInYearsAndMonths( + userInformation?.data.user.github_created_at, + new Date(), + ); + if (usersRequest.status === 200) { const userData = usersRequest.data.data[0]; let userSavedData = { @@ -257,7 +280,7 @@ async function showSavedDetails() { numberOfHours: userData.intro.numberOfHours, foundFrom: userData.foundFrom, }; - generateSavedDetailsForm(userSavedData); + generateSavedDetailsForm(userSavedData, dateDiff); } else if (usersRequest.status === 404) { generateNoDataFoundPage(); loading.classList.add('hidden'); diff --git a/js/utils.js b/js/utils.js index 28c96254e..2f796ed1f 100644 --- a/js/utils.js +++ b/js/utils.js @@ -32,3 +32,26 @@ function createElement({ type, classList = [], id }) { element.id = id ?? true; return element; } + +function getDateDifferenceInYearsAndMonths(startDate, endDate) { + const start = new Date(startDate); + const end = new Date(endDate); + + if (start > end) { + throw new Error('Start date cannot be after the end date.'); + } + + const yearDiff = end.getFullYear() - start.getFullYear(); + const monthDiff = end.getMonth() - start.getMonth(); + + let totalMonths = yearDiff * 12 + monthDiff; + + if (end.getDate() < start.getDate()) { + totalMonths -= 1; + } + + const years = Math.floor(totalMonths / 12); + const months = totalMonths % 12; + + return { years, months }; +} From d587f87d0d3a1abbc186b265b2093e6061e5bba0 Mon Sep 17 00:00:00 2001 From: Ravi kumar <1002kumarravi@gmail.com> Date: Thu, 3 Aug 2023 19:55:03 +0530 Subject: [PATCH 02/11] add: change more then year ago with actual year --- js/intro.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/js/intro.js b/js/intro.js index c0c4c017f..7ba878990 100644 --- a/js/intro.js +++ b/js/intro.js @@ -86,7 +86,9 @@ function generateSavedDetailsForm(users, dateDiff = NaN) { }); if (dateDiff.years || dateDiff.months) { githubStatusValue.innerText = `User GitHub account created ${ - dateDiff.years > 0 ? 'more then a year' : `${dateDiff.months} months` + dateDiff.years > 0 + ? `${dateDiff.years} year` + : `${dateDiff.months} months` } ago`; } container.appendChild(githubStatusValue); From 58d411f88b7fc66f351abd3cd947c8329a6fdce2 Mon Sep 17 00:00:00 2001 From: Ravi kumar <1002kumarravi@gmail.com> Date: Thu, 3 Aug 2023 20:00:50 +0530 Subject: [PATCH 03/11] fix: year word with years --- js/intro.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/intro.js b/js/intro.js index 7ba878990..7d85f3096 100644 --- a/js/intro.js +++ b/js/intro.js @@ -87,7 +87,7 @@ function generateSavedDetailsForm(users, dateDiff = NaN) { if (dateDiff.years || dateDiff.months) { githubStatusValue.innerText = `User GitHub account created ${ dateDiff.years > 0 - ? `${dateDiff.years} year` + ? `${dateDiff.years} years` : `${dateDiff.months} months` } ago`; } From 52d08196b9c09388d71776ab480141219a0f49e7 Mon Sep 17 00:00:00 2001 From: Ravi kumar <1002kumarravi@gmail.com> Date: Fri, 4 Aug 2023 06:38:24 +0530 Subject: [PATCH 04/11] fix: optional chaining for github_created_at --- js/intro.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/intro.js b/js/intro.js index 7d85f3096..5bc056791 100644 --- a/js/intro.js +++ b/js/intro.js @@ -261,7 +261,7 @@ async function showSavedDetails() { ); const dateDiff = getDateDifferenceInYearsAndMonths( - userInformation?.data.user.github_created_at, + userInformation?.data.user?.github_created_at, new Date(), ); From 1adfadc07113d43343f8481b6d895b32cf020ef0 Mon Sep 17 00:00:00 2001 From: Ravi kumar <1002kumarravi@gmail.com> Date: Mon, 7 Aug 2023 00:47:15 +0530 Subject: [PATCH 05/11] test for dateDifference function | utils.js --- __test__/utils.test.js | 37 +++++++++++++++++++++++++++++++++++++ js/intro.js | 30 +++++++++++++++++++----------- js/utils.js | 37 ++++++++++++++++++++++++------------- 3 files changed, 80 insertions(+), 24 deletions(-) create mode 100644 __test__/utils.test.js diff --git a/__test__/utils.test.js b/__test__/utils.test.js new file mode 100644 index 000000000..76b6d9a6f --- /dev/null +++ b/__test__/utils.test.js @@ -0,0 +1,37 @@ +const utils = require('../js/utils'); + +describe('Utils functiona 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/js/intro.js b/js/intro.js index 5bc056791..a33a8e7be 100644 --- a/js/intro.js +++ b/js/intro.js @@ -77,21 +77,28 @@ function generateSavedDetailsForm(users, dateDiff = NaN) { const container = createElement({ type: 'div', classList: ['container'] }); renderIntroPage.appendChild(container); - const githubStatusValue = createElement({ + const githubCreatedValue = createElement({ type: 'h4', classList: [ `${dateDiff.years > 0 ? 'github-created-text' : 'github-created-alert'}`, ], - id: 'ghInfo', }); - if (dateDiff.years || dateDiff.months) { - githubStatusValue.innerText = `User GitHub account created ${ - dateDiff.years > 0 - ? `${dateDiff.years} years` - : `${dateDiff.months} months` - } ago`; + + if (dateDiff.years > 0) { + githubCreatedValue.innerText = `User GitHub account created ${dateDiff.years} years ago`; + } else if (dateDiff.years === 0 && dateDiff.months > 0) { + githubCreatedValue.innerText = `User GitHub account created ${dateDiff.months} months ago`; + } else if ( + dateDiff.years === 0 && + dateDiff.months === 0 && + dateDiff.days >= 0 + ) { + githubCreatedValue.innerText = `User GitHub account created ${ + dateDiff.days > 0 ? dateDiff.days : '1' + } days ago`; } - container.appendChild(githubStatusValue); + + container.appendChild(githubCreatedValue); const nameLabel = createElement({ type: 'p', @@ -260,10 +267,11 @@ async function showSavedDetails() { `${BASE_URL}/users/userId/${userId}`, ); - const dateDiff = getDateDifferenceInYearsAndMonths( + const dateDiff = dateDifference( userInformation?.data.user?.github_created_at, - new Date(), + new Date().getTime(), ); + console.log(dateDiff); if (usersRequest.status === 200) { const userData = usersRequest.data.data[0]; diff --git a/js/utils.js b/js/utils.js index 2f796ed1f..dc34a2e05 100644 --- a/js/utils.js +++ b/js/utils.js @@ -33,25 +33,36 @@ function createElement({ type, classList = [], id }) { return element; } -function getDateDifferenceInYearsAndMonths(startDate, endDate) { - const start = new Date(startDate); - const end = new Date(endDate); +function dateDifference(startDate, endDate) { + let start = new Date(startDate); + let end = new Date(endDate); if (start > end) { - throw new Error('Start date cannot be after the end date.'); + [start, end] = [end, start]; } - const yearDiff = end.getFullYear() - start.getFullYear(); - const monthDiff = end.getMonth() - start.getMonth(); + let yearDiff = end.getFullYear() - start.getFullYear(); + let monthDiff = end.getMonth() - start.getMonth(); + let dayDiff = end.getDate() - start.getDate(); - let totalMonths = yearDiff * 12 + monthDiff; - - if (end.getDate() < start.getDate()) { - totalMonths -= 1; + if (dayDiff < 0) { + monthDiff--; + const lastMonth = new Date(end.getFullYear(), end.getMonth(), 0); + dayDiff = lastMonth.getDate() + dayDiff; } - const years = Math.floor(totalMonths / 12); - const months = totalMonths % 12; + if (monthDiff < 0) { + yearDiff--; + monthDiff = 12 + monthDiff; + } - return { years, months }; + return { + years: yearDiff, + months: monthDiff, + days: dayDiff, + }; } + +module.exports = { + dateDifference, +}; From 83966fdbbd71750bafa9802cb2c09b98df3ee1ec Mon Sep 17 00:00:00 2001 From: Ravi kumar <1002kumarravi@gmail.com> Date: Mon, 7 Aug 2023 06:14:49 +0530 Subject: [PATCH 06/11] remove console.log --- js/intro.js | 1 - 1 file changed, 1 deletion(-) diff --git a/js/intro.js b/js/intro.js index a33a8e7be..c7b62ff3e 100644 --- a/js/intro.js +++ b/js/intro.js @@ -271,7 +271,6 @@ async function showSavedDetails() { userInformation?.data.user?.github_created_at, new Date().getTime(), ); - console.log(dateDiff); if (usersRequest.status === 200) { const userData = usersRequest.data.data[0]; From 265e0ae68213471c9b557d5c0dd4425b57a16910 Mon Sep 17 00:00:00 2001 From: Ravi kumar <1002kumarravi@gmail.com> Date: Mon, 7 Aug 2023 06:36:15 +0530 Subject: [PATCH 07/11] increase timeout for test --- __test__/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 () => { From 31417d65fe3dd2df0b4de6c41c11df9461f050ca Mon Sep 17 00:00:00 2001 From: Ravi kumar <1002kumarravi@gmail.com> Date: Mon, 7 Aug 2023 13:31:08 +0530 Subject: [PATCH 08/11] fix: data passing way in function --- js/intro.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/js/intro.js b/js/intro.js index c7b62ff3e..9be3245aa 100644 --- a/js/intro.js +++ b/js/intro.js @@ -64,7 +64,7 @@ function generateNoDataFoundPage() { notFound.appendChild(notFoundDiv); } -function generateSavedDetailsForm(users, dateDiff = NaN) { +function generateSavedDetailsForm(users) { const renderIntroPage = createElement({ type: 'section', classList: ['render-page'], @@ -80,21 +80,21 @@ function generateSavedDetailsForm(users, dateDiff = NaN) { const githubCreatedValue = createElement({ type: 'h4', classList: [ - `${dateDiff.years > 0 ? 'github-created-text' : 'github-created-alert'}`, + `${users.dateDiff.years > 0 ? 'github-created-text' : 'github-created-alert'}`, ], }); - if (dateDiff.years > 0) { - githubCreatedValue.innerText = `User GitHub account created ${dateDiff.years} years ago`; - } else if (dateDiff.years === 0 && dateDiff.months > 0) { - githubCreatedValue.innerText = `User GitHub account created ${dateDiff.months} months ago`; + 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 ( - dateDiff.years === 0 && - dateDiff.months === 0 && - dateDiff.days >= 0 + users.dateDiff.years === 0 && + users.dateDiff.months === 0 && + users.dateDiff.days >= 0 ) { githubCreatedValue.innerText = `User GitHub account created ${ - dateDiff.days > 0 ? dateDiff.days : '1' + users.dateDiff.days > 0 ? users.dateDiff.days : '1' } days ago`; } @@ -288,8 +288,9 @@ async function showSavedDetails() { whyRds: userData.intro.whyRds, numberOfHours: userData.intro.numberOfHours, foundFrom: userData.foundFrom, + dateDiff: dateDiff, }; - generateSavedDetailsForm(userSavedData, dateDiff); + generateSavedDetailsForm(userSavedData); } else if (usersRequest.status === 404) { generateNoDataFoundPage(); loading.classList.add('hidden'); From d6ca4a476caed36b5a5546ef6a04f17d6ff5cac4 Mon Sep 17 00:00:00 2001 From: Ravi kumar <1002kumarravi@gmail.com> Date: Mon, 7 Aug 2023 13:33:26 +0530 Subject: [PATCH 09/11] fix: typo error --- __test__/utils.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__test__/utils.test.js b/__test__/utils.test.js index 76b6d9a6f..126f49f95 100644 --- a/__test__/utils.test.js +++ b/__test__/utils.test.js @@ -1,6 +1,6 @@ const utils = require('../js/utils'); -describe('Utils functiona test case', () => { +describe('Utils function test case', () => { describe('Difference between two dates', function () { test('should handle negative differences correctly', () => { const startDate = 1691260200000; // 6 August 2023 From bd6be551eae6e7cf579b19d0777e0980979b974a Mon Sep 17 00:00:00 2001 From: Ravi kumar <1002kumarravi@gmail.com> Date: Tue, 8 Aug 2023 12:14:29 +0530 Subject: [PATCH 10/11] fix: lint problem --- js/intro.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/js/intro.js b/js/intro.js index 9be3245aa..c5b2d9abb 100644 --- a/js/intro.js +++ b/js/intro.js @@ -80,7 +80,11 @@ function generateSavedDetailsForm(users) { const githubCreatedValue = createElement({ type: 'h4', classList: [ - `${users.dateDiff.years > 0 ? 'github-created-text' : 'github-created-alert'}`, + `${ + users.dateDiff.years > 0 + ? 'github-created-text' + : 'github-created-alert' + }`, ], }); @@ -272,6 +276,8 @@ async function showSavedDetails() { new Date().getTime(), ); + console.log(usersRequest.data.data[0]); + if (usersRequest.status === 200) { const userData = usersRequest.data.data[0]; let userSavedData = { From 4709efce8d328fa68610b185890a947fa0ed0441 Mon Sep 17 00:00:00 2001 From: Ravi kumar <1002kumarravi@gmail.com> Date: Thu, 10 Aug 2023 09:50:59 +0530 Subject: [PATCH 11/11] remove console.log | intro.js --- js/intro.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/js/intro.js b/js/intro.js index c5b2d9abb..e915926da 100644 --- a/js/intro.js +++ b/js/intro.js @@ -276,8 +276,6 @@ async function showSavedDetails() { new Date().getTime(), ); - console.log(usersRequest.data.data[0]); - if (usersRequest.status === 200) { const userData = usersRequest.data.data[0]; let userSavedData = {