trigger data button
This commit is contained in:
parent
a821b0a3d1
commit
692968e0e5
@ -1,4 +1,4 @@
|
||||
import React, { useState, useMemo, useEffect } from 'react';
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import AdminHeader from '@/components/admin/AdminHeader';
|
||||
import {
|
||||
IconButton,
|
||||
@ -18,7 +18,7 @@ import {
|
||||
import { Breadcrumbs } from '@mui/material';
|
||||
import { Link } from 'react-router-dom';
|
||||
import Card from '@/components/common/Card';
|
||||
import { getManufacturingIndex, getManufacturingMonthlyOverview } from '@/services/manufacturingIndex/ManufacturingIndex';
|
||||
import { getManufacturingIndex, getManufacturingMonthlyOverview, autoSubmitSurvey } from '@/services/manufacturingIndex/ManufacturingIndex';
|
||||
|
||||
// Import popup components
|
||||
import ManufacturingTotal from './ManufacturingIndexPopup/ManufacturingTotal';
|
||||
@ -57,7 +57,12 @@ const ManufacturingIndex = () => {
|
||||
const [monthlyOverviewData, setMonthlyOverviewData] = useState(null);
|
||||
const [isPopupOpen, setIsPopupOpen] = useState(false);
|
||||
const [apiLoading, setApiLoading] = useState(false);
|
||||
const [quarter, setQuarter] = useState('Q2');
|
||||
const [surveySubmitLoading, setSurveySubmitLoading] = useState(false);
|
||||
const [surveySubmitStatus, setSurveySubmitStatus] = useState('');
|
||||
const [surveySubmitError, setSurveySubmitError] = useState(null);
|
||||
const [nextScheduledRun, setNextScheduledRun] = useState(''); // Default value
|
||||
const [refreshKey, setRefreshKey] = useState(0);
|
||||
|
||||
const [pagination, setPagination] = useState({
|
||||
currentPage: 1,
|
||||
@ -235,7 +240,7 @@ const ManufacturingIndex = () => {
|
||||
};
|
||||
|
||||
fetchData();
|
||||
}, []);
|
||||
}, [refreshKey]);
|
||||
|
||||
// ARROW CLICK
|
||||
const handleMonthSelect = async (row) => {
|
||||
@ -315,6 +320,46 @@ const ManufacturingIndex = () => {
|
||||
applyFilters(year, searchTerm);
|
||||
};
|
||||
|
||||
const handleQuarterChange = (e) => {
|
||||
const selectedQuarter = e.target.value;
|
||||
setQuarter(selectedQuarter);
|
||||
};
|
||||
|
||||
const handleTriggerData = () => {
|
||||
setError(null);
|
||||
setRefreshKey(prev => prev + 1);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const submitSurvey = async () => {
|
||||
if (year === 'All' || !quarter) {
|
||||
setSurveySubmitStatus('');
|
||||
setSurveySubmitError(null);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setSurveySubmitLoading(true);
|
||||
setSurveySubmitStatus('');
|
||||
setSurveySubmitError(null);
|
||||
|
||||
const response = await autoSubmitSurvey(parseInt(year, 10), quarter);
|
||||
|
||||
setSurveySubmitStatus(
|
||||
response?.data?.message || 'Survey auto-submit request sent successfully.'
|
||||
);
|
||||
} catch (error) {
|
||||
setSurveySubmitError(
|
||||
error?.response?.data?.message || error.message || 'Survey auto-submit failed.'
|
||||
);
|
||||
} finally {
|
||||
setSurveySubmitLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
submitSurvey();
|
||||
}, [year, quarter]);
|
||||
|
||||
const handleExportCSV = () => {
|
||||
const headers = ['Month', 'Quarter', 'Index', 'Total Weight', 'MoM Change', 'YoY Change', 'Generated On', 'Status'];
|
||||
const csvContent = [
|
||||
@ -572,15 +617,31 @@ const ManufacturingIndex = () => {
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center w-32">
|
||||
<div className="flex items-center w-auto min-w-[88px]">
|
||||
<SelectField
|
||||
value={year}
|
||||
onChange={handleYearChange}
|
||||
className="w-full h-[40px]"
|
||||
className="w-auto h-[40px]"
|
||||
size="small"
|
||||
options={years.map(y => ({ value: y, label: y }))}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex items-center w-auto min-w-[72px]">
|
||||
<SelectField
|
||||
value={quarter}
|
||||
onChange={handleQuarterChange}
|
||||
className="w-auto h-[40px]"
|
||||
size="small"
|
||||
options={['Q1', 'Q2', 'Q3', 'Q4'].map(q => ({ value: q, label: q }))}
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className="h-10 px-3 xl:px-4 rounded-[6px] bg-[#F7F7F7] border border-[#C3C6CB] text-sm inline-flex items-center gap-2 hover:bg-gray-50 whitespace-nowrap font-medium text-[#232528]"
|
||||
onClick={handleTriggerData}
|
||||
>
|
||||
Trigger Data
|
||||
</button>
|
||||
<button
|
||||
className="h-10 px-3 xl:px-4 rounded-[6px] bg-[#F7F7F7] border border-[#C3C6CB] text-sm inline-flex items-center gap-2 hover:bg-gray-50 whitespace-nowrap"
|
||||
onClick={handleExportCSV}
|
||||
@ -591,6 +652,17 @@ const ManufacturingIndex = () => {
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{surveySubmitLoading || surveySubmitStatus || surveySubmitError ? (
|
||||
<div className="px-6 pb-4 pt-2 text-sm text-gray-700">
|
||||
{surveySubmitLoading ? (
|
||||
<span>Submitting survey request...</span>
|
||||
) : surveySubmitError ? (
|
||||
<span className="text-red-600">{surveySubmitError}</span>
|
||||
) : (
|
||||
<span className="text-green-600">{surveySubmitStatus}</span>
|
||||
)}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{/* Table */}
|
||||
@ -689,4 +761,4 @@ const ManufacturingIndex = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default ManufacturingIndex;
|
||||
export default ManufacturingIndex;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { getRequest } from '@/services/api/CommonService';
|
||||
import { getRequest, postRequest } from '@/services/api/CommonService';
|
||||
|
||||
const MANUFACTURING_ENDPOINT = '/manufacturing';
|
||||
|
||||
@ -33,4 +33,17 @@ export const getManufacturingMonthlyOverview = async (year, quarter) => {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const autoSubmitSurvey = async (year, quarter) => {
|
||||
try {
|
||||
const response = await postRequest('/api/survey_auto_submit', {
|
||||
year,
|
||||
quarter,
|
||||
});
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('Error auto-submitting survey:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user