From bc288f2bee7d56558ee3fabc2ede9e885a079a62 Mon Sep 17 00:00:00 2001 From: Aakash Shankar Date: Sun, 17 Nov 2024 22:26:52 -0500 Subject: [PATCH] #184 timezone follow ups for the profile and service lists (#193) --- src/accounts/templates/profile_base.html | 12 ++++- src/forum/templates/category_detail.html | 2 +- src/forum/templates/post_detail.html | 2 +- src/services/templates/service_list.html | 1 + src/static/js/{forum_common.js => common.js} | 1 - src/static/js/profile.js | 50 +++++++++++++------- src/static/js/service_list.js | 50 +++++++++----------- 7 files changed, 69 insertions(+), 49 deletions(-) rename src/static/js/{forum_common.js => common.js} (90%) diff --git a/src/accounts/templates/profile_base.html b/src/accounts/templates/profile_base.html index 7bca19e..197e1ce 100644 --- a/src/accounts/templates/profile_base.html +++ b/src/accounts/templates/profile_base.html @@ -81,8 +81,7 @@

My Reviews

{{ review.ServiceName }}

Rating: {{ review.RatingStars }} stars

{{ review.RatingMessage }}

-

{{ review.Timestamp|date:"M d, Y" }}

- +

{% endfor %} {% else %} @@ -177,4 +176,13 @@

Leave a Review

const userIsAuthenticated = {{ user.is_authenticated|lower }}; + {% endblock %} diff --git a/src/forum/templates/category_detail.html b/src/forum/templates/category_detail.html index 22680a2..5e4d4a2 100644 --- a/src/forum/templates/category_detail.html +++ b/src/forum/templates/category_detail.html @@ -86,7 +86,7 @@

{% block scripts %} {{ block.super }} - + - + + {% endblock %} \ No newline at end of file diff --git a/src/static/js/forum_common.js b/src/static/js/common.js similarity index 90% rename from src/static/js/forum_common.js rename to src/static/js/common.js index 1b81f4a..b750c7a 100644 --- a/src/static/js/forum_common.js +++ b/src/static/js/common.js @@ -1,6 +1,5 @@ // static/js/forum.js function formatDateTime(utcTimestamp) { - console.log('Input timestamp:', utcTimestamp); // Keep this for debugging try { let date = new Date(utcTimestamp); diff --git a/src/static/js/profile.js b/src/static/js/profile.js index f996d4a..0bbfc83 100644 --- a/src/static/js/profile.js +++ b/src/static/js/profile.js @@ -5,23 +5,38 @@ function toggleEdit() { editSection.classList.toggle('hidden'); } -document.addEventListener('DOMContentLoaded', () => { - function formatTimestamp(utcTimestamp) { - // Ensure we're parsing the UTC timestamp correctly - const date = new Date(utcTimestamp + 'Z'); // Append 'Z' to ensure UTC interpretation - - const options = { - year: 'numeric', - month: 'long', - day: 'numeric', - hour: '2-digit', - minute: '2-digit', - hour12: true, - }; - - return date.toLocaleString(undefined, options); +function formatTimestamp(utcTimestamp) { + if (!utcTimestamp) { + return 'N/A'; + } + + try { + // Remove the timezone offset if it exists + const cleanTimestamp = utcTimestamp.replace(/([+-]\d{2}:\d{2}|Z)$/, ''); + const date = new Date(cleanTimestamp + 'Z'); + + if (isNaN(date.getTime())) { + console.log('Invalid date created for:', utcTimestamp); + return 'Invalid Date'; + } + + const options = { + year: 'numeric', + month: 'long', + day: 'numeric', + hour: '2-digit', + minute: '2-digit', + hour12: true, + }; + + return date.toLocaleString(undefined, options); + } catch (error) { + console.error('Error formatting date:', error); + return 'Error formatting date'; + } } +document.addEventListener('DOMContentLoaded', () => { async function fetchAndDisplayReviews(serviceId, page = 1) { try { const response = await fetch(`/home/get_reviews/${serviceId}/?page=${page}`); @@ -29,7 +44,7 @@ document.addEventListener('DOMContentLoaded', () => { throw new Error(`Failed to fetch reviews. Status: ${response.status}`); } - const { reviews, has_next, has_previous, current_page } = await response.json(); + const {reviews, has_next, has_previous, current_page} = await response.json(); const reviewsContainer = document.getElementById('reviewsContainer'); reviewsContainer.innerHTML = ''; @@ -76,6 +91,7 @@ document.addEventListener('DOMContentLoaded', () => { responseDiv.appendChild(responseText); const respondedAt = formatTimestamp(review.RespondedAt); + console.log(review.RespondedAt) const responseMeta = document.createElement('p'); responseMeta.classList.add('text-xs', 'text-gray-500'); responseMeta.textContent = `Responded on ${respondedAt}`; @@ -255,7 +271,7 @@ document.addEventListener('DOMContentLoaded', () => { // Add event listeners for bookmark checkboxes const bookmarkCheckboxes = document.querySelectorAll('.bookmark-checkbox'); bookmarkCheckboxes.forEach(checkbox => { - checkbox.addEventListener('change', async function() { + checkbox.addEventListener('change', async function () { const serviceId = this.dataset.serviceId; const action = this.checked ? 'add' : 'remove'; diff --git a/src/static/js/service_list.js b/src/static/js/service_list.js index 5e96c9b..c1ae950 100644 --- a/src/static/js/service_list.js +++ b/src/static/js/service_list.js @@ -1,13 +1,13 @@ // service_list.js -document.addEventListener('DOMContentLoaded', function() { +document.addEventListener('DOMContentLoaded', function () { const servicesGrid = document.getElementById('servicesGrid'); const serviceModal = document.getElementById('serviceModal'); const closeModal = document.getElementById('closeModal'); let map = null; // Service name click event - servicesGrid.addEventListener('click', function(e) { + servicesGrid.addEventListener('click', function (e) { if (e.target.classList.contains('service-name')) { const serviceId = e.target.dataset.serviceId; openServiceModal(serviceId); @@ -15,7 +15,7 @@ document.addEventListener('DOMContentLoaded', function() { }); // Close modal event - closeModal.addEventListener('click', function() { + closeModal.addEventListener('click', function () { serviceModal.classList.add('hidden'); }); @@ -50,11 +50,7 @@ document.addEventListener('DOMContentLoaded', function() { const hasResponseText = review.ResponseText && review.ResponseText.trim() !== ""; let respondedAtDate = "Responded just now"; if (review.RespondedAt) { - const date = new Date(review.RespondedAt); - const day = String(date.getDate()).padStart(2, '0'); - const month = date.toLocaleString('default', { month: 'long' }); - const year = date.getFullYear(); - respondedAtDate = `${day} ${month} ${year}`; + respondedAtDate = formatDateTime(review.RespondedAt); } reviewsContainer.innerHTML += ` @@ -65,7 +61,7 @@ document.addEventListener('DOMContentLoaded', function() {

Provider Response:

${review.ResponseText}

-

Responded at: ${respondedAtDate}

+

Responded on ${respondedAtDate}

` : `