Files
Checkmate/Client/src/Features/UptimeMonitors/uptimeMonitorsSlice.js
T

241 lines
7.2 KiB
JavaScript
Raw Normal View History

2024-06-04 13:32:17 -07:00
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
2024-06-21 11:08:31 -07:00
import { jwtDecode } from "jwt-decode";
import axiosInstance from "../../Utils/axiosConfig";
2024-06-04 12:40:05 -07:00
const initialState = {
isLoading: false,
monitors: [],
success: null,
msg: null,
};
2024-07-30 11:25:14 -07:00
export const createUptimeMonitor = createAsyncThunk(
"monitors/createMonitor",
async (data, thunkApi) => {
try {
const { authToken, monitor } = data;
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);
}
return thunkApi.rejectWithValue(error.message);
}
}
);
2024-07-30 11:25:14 -07:00
export const getUptimeMonitors = createAsyncThunk(
2024-06-04 13:32:17 -07:00
"monitors/getMonitors",
async (token, thunkApi) => {
try {
const res = await axiosInstance.get("/monitors");
2024-06-04 13:32:17 -07:00
return res.data;
} catch (error) {
2024-06-25 11:14:16 -07:00
if (error.response && error.response.data) {
return thunkApi.rejectWithValue(error.response.data);
}
return thunkApi.rejectWithValue(error.message);
2024-06-04 13:32:17 -07:00
}
}
);
2024-07-30 11:25:14 -07:00
export const getUptimeMonitorsByUserId = createAsyncThunk(
"montiors/getMonitorsByUserId",
async (token, thunkApi) => {
2024-06-21 11:08:31 -07:00
const user = jwtDecode(token);
try {
2024-07-12 10:24:46 -07:00
const res = await axiosInstance.get(
2024-07-26 18:56:39 -07:00
`/monitors/user/${user._id}?limit=25&type=http&type=ping&sortOrder=desc`,
2024-07-12 10:24:46 -07:00
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);
2024-06-21 11:08:31 -07:00
return res.data;
} catch (error) {
2024-06-25 11:14:16 -07:00
if (error.response && error.response.data) {
return thunkApi.rejectWithValue(error.response.data);
}
return thunkApi.rejectWithValue(error.message);
}
}
);
2024-07-31 11:00:45 -07:00
export const updateUptimeMonitor = createAsyncThunk(
"monitors/updateMonitor",
async (data, thunkApi) => {
try {
const { authToken, monitor } = data;
2024-07-31 11:36:51 -07:00
const updatedFields = {
name: monitor.name,
description: monitor.description,
interval: monitor.interval,
notifications: monitor.notifications
2024-07-31 11:36:51 -07:00
};
2024-07-31 11:00:45 -07:00
const res = await axiosInstance.post(
`/monitors/edit/${monitor._id}`,
2024-07-31 11:36:51 -07:00
updatedFields,
{
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);
}
return thunkApi.rejectWithValue(error.message);
}
}
);
export const deleteUptimeMonitor = createAsyncThunk(
"monitors/deleteMonitor",
async (data, thunkApi) => {
try {
const { authToken, monitor } = data;
const res = await axiosInstance.post(
`/monitors/delete/${monitor._id}`,
{},
2024-07-31 11:00:45 -07:00
{
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);
}
return thunkApi.rejectWithValue(error.message);
}
}
);
2024-07-30 11:25:14 -07:00
const uptimeMonitorsSlice = createSlice({
name: "uptimeMonitors",
initialState,
2024-06-24 11:56:25 -07:00
reducers: {
2024-07-30 11:25:14 -07:00
clearUptimeMonitorState: (state) => {
2024-06-24 11:56:25 -07:00
state.isLoading = false;
state.monitors = [];
state.success = null;
state.msg = null;
},
},
2024-06-04 13:32:17 -07:00
extraReducers: (builder) => {
builder
// *****************************************************
// All Monitors
// *****************************************************
2024-07-30 11:25:14 -07:00
.addCase(getUptimeMonitors.pending, (state) => {
2024-06-04 13:32:17 -07:00
state.isLoading = true;
})
2024-07-30 11:25:14 -07:00
.addCase(getUptimeMonitors.fulfilled, (state, action) => {
2024-06-04 13:32:17 -07:00
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
state.monitors = action.payload.data;
})
2024-07-30 11:25:14 -07:00
.addCase(getUptimeMonitors.rejected, (state, action) => {
2024-06-04 13:32:17 -07:00
state.isLoading = false;
2024-06-25 11:14:16 -07:00
state.success = false;
state.msg = action.payload
? action.payload.msg
2024-07-30 11:25:14 -07:00
: "Getting uptime monitors failed";
})
// *****************************************************
// Monitors by userId
// *****************************************************
2024-07-30 11:25:14 -07:00
.addCase(getUptimeMonitorsByUserId.pending, (state) => {
state.isLoading = true;
})
2024-07-30 11:25:14 -07:00
.addCase(getUptimeMonitorsByUserId.fulfilled, (state, action) => {
state.isLoading = false;
state.success = action.payload.msg;
state.monitors = action.payload.data;
})
2024-07-30 11:25:14 -07:00
.addCase(getUptimeMonitorsByUserId.rejected, (state, action) => {
state.isLoading = false;
2024-06-25 11:14:16 -07:00
state.success = false;
state.msg = action.payload
? action.payload.msg
2024-07-30 11:25:14 -07:00
: "Getting uptime monitors failed";
})
// *****************************************************
// Create Monitor
// *****************************************************
2024-07-30 11:25:14 -07:00
.addCase(createUptimeMonitor.pending, (state) => {
state.isLoading = true;
})
2024-07-30 11:25:14 -07:00
.addCase(createUptimeMonitor.fulfilled, (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
})
2024-07-30 11:25:14 -07:00
.addCase(createUptimeMonitor.rejected, (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
2024-07-30 11:25:14 -07:00
: "Failed to create uptime monitor";
2024-07-31 11:00:45 -07:00
})
// *****************************************************
// update Monitor
// *****************************************************
.addCase(updateUptimeMonitor.pending, (state) => {
state.isLoading = true;
})
.addCase(updateUptimeMonitor.fulfilled, (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
})
.addCase(updateUptimeMonitor.rejected, (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Failed to update uptime monitor";
2024-07-31 11:36:51 -07:00
})
// *****************************************************
// Delete Monitor
// *****************************************************
.addCase(deleteUptimeMonitor.pending, (state) => {
state.isLoading = true;
})
.addCase(deleteUptimeMonitor.fulfilled, (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
})
.addCase(deleteUptimeMonitor.rejected, (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Failed to delete uptime monitor";
2024-06-04 13:32:17 -07:00
});
2024-06-04 12:40:05 -07:00
},
});
2024-07-30 11:25:14 -07:00
export const { setUptimeMonitors, clearUptimeMonitorState } =
uptimeMonitorsSlice.actions;
2024-07-30 11:25:14 -07:00
export default uptimeMonitorsSlice.reducer;