diff --git a/ipi-survey-platform/src/components/dashboard/SurveyStatus.jsx b/ipi-survey-platform/src/components/dashboard/SurveyStatus.jsx index 1b9688d..de463df 100644 --- a/ipi-survey-platform/src/components/dashboard/SurveyStatus.jsx +++ b/ipi-survey-platform/src/components/dashboard/SurveyStatus.jsx @@ -2,11 +2,6 @@ import React from 'react'; import { useNavigate } from 'react-router-dom'; import SurveyCarousel from './SurveyCarousel'; -const formatQuarterYear = (quarter, year) => { - if (!quarter || !year) return '—'; - return `${quarter} ${year}`; -}; - const formatDate = (value) => { if (!value) return '—'; const date = new Date(value); @@ -31,12 +26,42 @@ const formatRelativeDays = (value) => { return `Due in ${diffDays} day${diffDays === 1 ? '' : 's'}`; }; +const getQuarterData = () => { + const now = new Date(); + const month = now.getMonth() + 1; + const year = now.getFullYear(); + + let currentQuarter = ''; + if (month >= 1 && month <= 3) currentQuarter = 'Q1'; + else if (month >= 4 && month <= 6) currentQuarter = 'Q2'; + else if (month >= 7 && month <= 9) currentQuarter = 'Q3'; + else currentQuarter = 'Q4'; + + const quarters = ['Q1', 'Q2', 'Q3', 'Q4']; + const currentIndex = quarters.indexOf(currentQuarter); + + const previousQuarter = + currentIndex === 0 ? 'Q4' : quarters[currentIndex - 1]; + const nextQuarter = + currentIndex === 3 ? 'Q1' : quarters[currentIndex + 1]; + + const previousYear = currentIndex === 0 ? year - 1 : year; + const nextYear = currentIndex === 3 ? year + 1 : year; + + return { + previous: { quarter: previousQuarter, year: previousYear }, + current: { quarter: currentQuarter, year }, + next: { quarter: nextQuarter, year: nextYear }, + }; +}; + export const SurveyStatus = ({ data = null, loading = false, error = '' }) => { const navigate = useNavigate(); - const handleStartSurvey = (survey) => { }; + const { previous, current, next } = getQuarterData(); + const surveyData = data || {}; const nextDeadline = surveyData?.next_deadline; const formattedDueDate = formatDate(nextDeadline); @@ -44,28 +69,28 @@ export const SurveyStatus = ({ data = null, loading = false, error = '' }) => { const surveys = [ { - title: `${formatQuarterYear(surveyData.previous_quarter, surveyData.previous_quarter_year)} Survey Ready`, + title: `${previous.quarter} ${previous.year} Survey Ready`, subtitle: 'Complete your quarterly Industrial Production Index (IPI) data submission', dueDate: formattedDueDate, relativeDue, estTime: '15–20 minutes', - status: surveyData?.submission_status || 'Pending', + status: 'Pending', }, { - title: `${formatQuarterYear(surveyData.current_quarter, surveyData.current_quarter_year)} Survey Ready`, + title: `${current.quarter} ${current.year} Survey Ready`, subtitle: 'Complete your quarterly Industrial Production Index (IPI) data submission', dueDate: formattedDueDate, relativeDue, estTime: '15–20 minutes', - status: surveyData?.submission_status || 'Pending', + status: 'Pending', }, { - title: `${formatQuarterYear(surveyData.next_quarter, surveyData.next_quarter_year)} Survey Ready`, + title: `${next.quarter} ${next.year} Survey Ready`, subtitle: 'Complete your quarterly Industrial Production Index (IPI) data submission', dueDate: formattedDueDate, relativeDue, estTime: '15–20 minutes', - status: surveyData?.submission_status || 'Pending', + status: 'Pending', }, ]; @@ -85,7 +110,9 @@ export const SurveyStatus = ({ data = null, loading = false, error = '' }) => {
{loading ? ( -
Loading survey status...
+
+ Loading survey status... +
) : error ? (
{error}
) : ( diff --git a/ipi-survey-platform/src/components/dashboard/WelcomeSection.jsx b/ipi-survey-platform/src/components/dashboard/WelcomeSection.jsx index ea97fd7..94db262 100644 --- a/ipi-survey-platform/src/components/dashboard/WelcomeSection.jsx +++ b/ipi-survey-platform/src/components/dashboard/WelcomeSection.jsx @@ -1,16 +1,22 @@ import React from 'react'; import HeaderBar from '@/components/layout/HeaderBar'; -const formatDate = (value) => { - if (!value) return '—'; - const date = new Date(value); - if (Number.isNaN(date.getTime())) return '—'; - return date.toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric' }); +const getCurrentQuarterAndYear = () => { + const now = new Date(); + const month = now.getMonth() + 1; + const year = now.getFullYear(); + let quarter = ''; + + if (month >= 1 && month <= 3) quarter = 'Q1'; + else if (month >= 4 && month <= 6) quarter = 'Q2'; + else if (month >= 7 && month <= 9) quarter = 'Q3'; + else quarter = 'Q4'; + + return { quarter, year }; }; export const WelcomeSection = ({ data = null, loading = false, error = '' }) => { - const currentQuarter = data?.current_quarter; - const currentYear = data?.current_quarter_year; + const { quarter: currentQuarter, year: currentYear } = getCurrentQuarterAndYear(); let description = 'Current reporting information unavailable.'; if (loading) { @@ -49,4 +55,6 @@ export const WelcomeSection = ({ data = null, loading = false, error = '' }) =>
); -}; \ No newline at end of file +}; + +export default WelcomeSection; \ No newline at end of file