Refactored uptimeMonitorsSlice axios requests

This commit is contained in:
Alex Holliday committed 2024-08-17 09:35:50 -07:00
1 parent da50630672
commit ceb8985afb
2 files changed
+64 -67

No files matched your search

@@ -13,32 +13,7 @@ export const createUptimeMonitor = createAsyncThunk(
async (data, thunkApi) => { async (data, thunkApi) => {
try { try {
const { authToken, monitor } = data; const { authToken, monitor } = data;
const res = await axiosInstance.createMonitor(authToken, monitor);
const res = await axiosInstance.post(`/monitors`, monitor, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
return res.data;
} catch (error) {
if (error.response && error.response.data) {
return thunkApi.rejectWithValue(error.response.data);
}
const payload = {
status: false,
msg: error.message ? error.message : "Unknown error",
};
return thunkApi.rejectWithValue(payload);
}
}
);
export const getUptimeMonitors = createAsyncThunk(
"monitors/getMonitors",
async (token, thunkApi) => {
try {
const res = await axiosInstance.get("/monitors");
return res.data; return res.data;
} catch (error) { } catch (error) {
if (error.response && error.response.data) { if (error.response && error.response.data) {
@@ -58,13 +33,13 @@ export const getUptimeMonitorsByUserId = createAsyncThunk(
async (token, thunkApi) => { async (token, thunkApi) => {
const user = jwtDecode(token); const user = jwtDecode(token);
try { try {
const res = await axiosInstance.get( const res = await axiosInstance.getMonitorsByUserId(
`/monitors/user/${user._id}?limit=25&type=http&type=ping&sortOrder=desc&normalize=true`, token,
{ user._id,
headers: { 25,
Authorization: `Bearer ${token}`, ["http", "ping"],
}, "desc",
} true
); );
return res.data; return res.data;
} catch (error) { } catch (error) {
@@ -91,15 +66,10 @@ export const updateUptimeMonitor = createAsyncThunk(
interval: monitor.interval, interval: monitor.interval,
notifications: monitor.notifications, notifications: monitor.notifications,
}; };
const res = await axiosInstance.put( const res = await axiosInstance.updateMonitor(
`/monitors/${monitor._id}`, authToken,
updatedFields, monitor._id,
{ updatedFields
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
}
); );
return res.data; return res.data;
} catch (error) { } catch (error) {
@@ -120,12 +90,7 @@ export const deleteUptimeMonitor = createAsyncThunk(
async (data, thunkApi) => { async (data, thunkApi) => {
try { try {
const { authToken, monitor } = data; const { authToken, monitor } = data;
const res = await axiosInstance.delete(`/monitors/${monitor._id}`, { const res = await axiosInstance.deleteMonitorById(authToken, monitor._id);
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
return res.data; return res.data;
} catch (error) { } catch (error) {
if (error.response && error.response.data) { if (error.response && error.response.data) {
@@ -153,25 +118,6 @@ const uptimeMonitorsSlice = createSlice({
}, },
extraReducers: (builder) => { extraReducers: (builder) => {
builder builder
// *****************************************************
// All Monitors
// *****************************************************
.addCase(getUptimeMonitors.pending, (state) => {
state.isLoading = true;
})
.addCase(getUptimeMonitors.fulfilled, (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
state.monitors = action.payload.data;
})
.addCase(getUptimeMonitors.rejected, (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Getting uptime monitors failed";
})
// ***************************************************** // *****************************************************
// Monitors by userId // Monitors by userId
// ***************************************************** // *****************************************************
+51
View File
@@ -24,4 +24,55 @@ axiosInstance.interceptors.response.use(
} }
); );
// Create a new monitor
axiosInstance.createMonitor = async (authToken, monitor) => {
return axiosInstance.post(`/monitors`, monitor, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
};
// Get all uptime monitors for a user
axiosInstance.getMonitorsByUserId = async (
authToken,
userId,
limit,
types,
sortOrder,
normalize
) => {
const typeQuery = types.reduce((acc, type) => acc + `&type=${type}`, "");
return axiosInstance.get(
`/monitors/user/${userId}?limit=${limit}${typeQuery}&sortOrder=${sortOrder}&normalize=${normalize}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
}
);
};
// Updates a single monitor
axiosInstance.updateMonitor = async (authToken, monitorId, updatedFields) => {
return axiosInstance.put(`/monitors/${monitorId}`, updatedFields, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
};
// Deletes a single monitor
axiosInstance.deleteMonitorById = async (authToken, monitorId) => {
return axiosInstance.delete(`/monitors/${monitorId}`, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
};
export default axiosInstance; export default axiosInstance;