Skip to content

Commit f466c66

Browse files
committed
Fix: error message implementation
1 parent 1a695fb commit f466c66

4 files changed

Lines changed: 60 additions & 25 deletions

File tree

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
1-
/* eslint-disable no-console */
21
import axios from 'axios';
32
import { ENDPOINTS } from '~/utils/URL';
4-
import { GET_ERRORS } from '../constants/errors';
53
import { GET_OPT_STATUS_BREAKDOWN } from '../constants/optStatusBreakdownConstants';
64

7-
export const setOptStatusBreakdown = payload => {
8-
return {
9-
type: GET_OPT_STATUS_BREAKDOWN,
10-
payload
11-
};
12-
};
13-
export const setErrors = payload => {
14-
return {
15-
type: GET_ERRORS,
16-
payload
17-
};
18-
}
19-
export const fetchOptStatusBreakdown = (startDate = "", endDate = "", role = "") => {
5+
export const setOptStatusBreakdown = payload => ({
6+
type: GET_OPT_STATUS_BREAKDOWN,
7+
payload,
8+
});
9+
10+
export const fetchOptStatusBreakdown = (startDate = '', endDate = '', role = '') => {
2011
const url = ENDPOINTS.OPT_STATUS_BREAKDOWN(startDate, endDate, role);
12+
2113
return async dispatch => {
2214
try {
2315
const response = await axios.get(url);
2416
dispatch(setOptStatusBreakdown(response.data.breakDown));
25-
} catch (error) {
26-
// eslint-disable-next-line no-console
27-
console.error("Error fetching OPT status breakdown:", error);
17+
return response.data;
18+
} catch (err) {
19+
return err?.response?.data || { message: 'Failed to fetch OPT status breakdown' };
2820
}
2921
};
3022
};
3123

32-
export default fetchOptStatusBreakdown;
24+
export default fetchOptStatusBreakdown;

src/components/OptStatusPieChart/OptStatusPieChart.jsx

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Pie } from 'react-chartjs-2';
99
import { useDispatch, useSelector } from 'react-redux';
1010
import ChartDataLabels from 'chartjs-plugin-datalabels';
1111
import { Chart as ChartJS } from 'chart.js';
12-
import { fetchOptStatusBreakdown } from '../../actions/optStatusBreakdownAction';
12+
import fetchOptStatusBreakdown from '../../actions/optStatusBreakdownAction';
1313
import { roleOptions } from './filter';
1414
import 'chart.js/auto';
1515
import styles from './OptStatusPieChart.module.css';
@@ -26,19 +26,36 @@ const COLORS = {
2626

2727
const OptStatusPieChart = () => {
2828
const dispatch = useDispatch();
29-
const { optStatusBreakdown = [] } = useSelector(state => state.optStatusBreakdown);
3029
const { darkMode } = useSelector(state => state.theme);
3130

3231
const [startDate, setStartDate] = useState('');
3332
const [endDate, setEndDate] = useState('');
3433
const [role, setRole] = useState('');
3534

35+
const [localBreakdown, setLocalBreakdown] = useState([]);
36+
const [localError, setLocalError] = useState('');
37+
38+
const fetchData = async () => {
39+
const response = await dispatch(fetchOptStatusBreakdown(startDate, endDate, role));
40+
41+
if (response.message) {
42+
setLocalError(response.message);
43+
setLocalBreakdown([]);
44+
} else if (response.breakDown) {
45+
setLocalBreakdown(response.breakDown);
46+
setLocalError('');
47+
} else {
48+
setLocalBreakdown([]);
49+
setLocalError('Unexpected response from server');
50+
}
51+
};
52+
3653
useEffect(() => {
37-
dispatch(fetchOptStatusBreakdown(startDate, endDate, role));
54+
fetchData();
3855
}, [startDate, endDate, role, dispatch]);
3956

40-
const labels = optStatusBreakdown.map(d => d.optStatus);
41-
const dataCounts = optStatusBreakdown.map(d => d.count);
57+
const labels = localBreakdown.map(d => d.optStatus);
58+
const dataCounts = localBreakdown.map(d => d.count);
4259
const total = dataCounts.reduce((sum, value) => sum + value, 0);
4360
const backgroundColors = labels.map(label => COLORS[label] || '#ccc');
4461

@@ -86,7 +103,11 @@ const OptStatusPieChart = () => {
86103

87104
<div className={styles.chartFilterLayout}>
88105
<div className={styles.pieChartWrapper}>
89-
<Pie data={chartData} options={options} />
106+
{localError ? (
107+
<div className={styles.errorMessage}>{localError}</div>
108+
) : (
109+
<Pie data={chartData} options={options} />
110+
)}
90111
</div>
91112

92113
<div className={styles.filters}>

src/components/OptStatusPieChart/OptStatusPieChart.module.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,17 @@
147147
.dateInputs input {
148148
width: 45%;
149149
}
150+
}
151+
.errorMessage {
152+
color: #d32f2f;
153+
background: #fdecea;
154+
padding: 10px 12px;
155+
border-radius: 6px;
156+
margin-bottom: 12px;
157+
font-weight: 500;
158+
}
159+
160+
.optStatusContainerDarkMode .errorMessage {
161+
color: #ffb4ab;
162+
background: #5a2120;
150163
}

src/reducers/optStatusBreakdownReducer.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { GET_ERRORS } from '../constants/errors';
12
import { GET_OPT_STATUS_BREAKDOWN } from '../constants/optStatusBreakdownConstants';
23

34
const initialState = {
@@ -15,6 +16,14 @@ export const optStatusBreakdownReducer = (state = initialState, action) => {
1516
loading: false,
1617
error: null,
1718
};
19+
20+
case GET_ERRORS:
21+
return {
22+
...state,
23+
error: action.payload,
24+
loading: false,
25+
};
26+
1827
default:
1928
return state;
2029
}

0 commit comments

Comments
 (0)