Files
Checkmate/Client/src/Features/PageSpeedMonitor/pageSpeedMonitorSlice.js
T

277 lines
8.5 KiB
JavaScript
Raw Normal View History

2024-07-30 13:40:31 -04:00
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { jwtDecode } from "jwt-decode";
2024-08-18 17:42:49 -07:00
import { networkService } from "../../main";
2024-07-30 13:40:31 -04:00
const initialState = {
isLoading: false,
2024-09-05 13:25:19 -07:00
monitorsSummary: [],
2024-07-30 13:40:31 -04:00
success: null,
msg: null,
};
export const createPageSpeed = createAsyncThunk(
"pageSpeedMonitors/createPageSpeed",
async (data, thunkApi) => {
try {
const { authToken, monitor } = data;
2024-08-18 17:42:49 -07:00
const res = await networkService.createMonitor(authToken, monitor);
2024-07-30 13:40:31 -04:00
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);
2024-07-30 13:40:31 -04:00
}
}
);
2024-09-03 12:07:59 -07:00
export const getPagespeedMonitorById = createAsyncThunk(
"monitors/getMonitorById",
async (data, thunkApi) => {
try {
const { authToken, monitorId } = data;
const res = await networkService.getMonitorByid(authToken, monitorId);
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);
}
}
);
2024-08-27 12:07:30 -07:00
export const getPageSpeedByTeamId = createAsyncThunk(
"pageSpeedMonitors/getPageSpeedByTeamId",
2024-07-30 13:40:31 -04:00
async (token, thunkApi) => {
const user = jwtDecode(token);
try {
2024-09-05 13:25:19 -07:00
const res = await networkService.getMonitorsAndSummaryByTeamId(
2024-08-17 09:50:52 -07:00
token,
2024-08-28 10:31:37 -07:00
user.teamId,
2024-09-05 13:25:19 -07:00
["pagespeed"]
2024-07-30 13:40:31 -04:00
);
2024-08-17 09:50:52 -07:00
2024-07-30 13:40:31 -04:00
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);
2024-07-30 13:40:31 -04:00
}
}
);
2024-08-08 16:53:50 -04:00
export const updatePageSpeed = createAsyncThunk(
"pageSpeedMonitors/updatePageSpeed",
async (data, thunkApi) => {
try {
const { authToken, monitor } = data;
const updatedFields = {
name: monitor.name,
description: monitor.description,
interval: monitor.interval,
// notifications: monitor.notifications,
};
2024-08-18 17:42:49 -07:00
const res = await networkService.updateMonitor(
2024-08-17 09:50:52 -07:00
authToken,
monitor._id,
updatedFields
2024-08-08 16:53:50 -04:00
);
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);
2024-08-08 16:53:50 -04:00
}
}
);
export const deletePageSpeed = createAsyncThunk(
"pageSpeedMonitors/deletePageSpeed",
2024-08-08 16:37:23 -04:00
async (data, thunkApi) => {
try {
const { authToken, monitor } = data;
2024-08-18 17:42:49 -07:00
const res = await networkService.deleteMonitorById(
authToken,
monitor._id
);
2024-08-08 16:37:23 -04:00
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);
2024-08-08 16:37:23 -04:00
}
}
);
2024-09-03 12:07:59 -07:00
export const pausePageSpeed = createAsyncThunk(
"pageSpeedMonitors/pausePageSpeed",
async (data, thunkApi) => {
try {
const { authToken, monitorId } = data;
const res = await networkService.pauseMonitorById(authToken, monitorId);
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);
}
}
);
2024-08-08 16:37:23 -04:00
2024-07-30 13:40:31 -04:00
const pageSpeedMonitorSlice = createSlice({
name: "pageSpeedMonitor",
initialState,
reducers: {
clearMonitorState: (state) => {
state.isLoading = false;
2024-09-05 13:25:19 -07:00
state.monitorsSummary = [];
2024-07-30 13:40:31 -04:00
state.success = null;
state.msg = null;
},
},
extraReducers: (builder) => {
builder
// *****************************************************
2024-09-03 12:07:59 -07:00
// Monitors by teamId
2024-07-30 13:40:31 -04:00
// *****************************************************
2024-08-27 12:07:30 -07:00
.addCase(getPageSpeedByTeamId.pending, (state) => {
2024-07-30 13:40:31 -04:00
state.isLoading = true;
})
2024-08-27 12:07:30 -07:00
.addCase(getPageSpeedByTeamId.fulfilled, (state, action) => {
2024-07-30 13:40:31 -04:00
state.isLoading = false;
state.success = action.payload.msg;
2024-09-05 13:25:19 -07:00
state.monitorsSummary = action.payload.data;
2024-07-30 13:40:31 -04:00
})
2024-08-27 12:07:30 -07:00
.addCase(getPageSpeedByTeamId.rejected, (state, action) => {
2024-07-30 13:40:31 -04:00
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Getting page speed monitors failed";
})
2024-09-03 12:07:59 -07:00
// *****************************************************
.addCase(getPagespeedMonitorById.pending, (state) => {
state.isLoading = true;
})
.addCase(getPagespeedMonitorById.fulfilled, (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
})
.addCase(getPagespeedMonitorById.rejected, (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Failed to get pagespeed monitor";
})
2024-07-30 13:40:31 -04:00
// *****************************************************
// Create Monitor
// *****************************************************
.addCase(createPageSpeed.pending, (state) => {
state.isLoading = true;
})
.addCase(createPageSpeed.fulfilled, (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
})
.addCase(createPageSpeed.rejected, (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Failed to create page speed monitor";
2024-08-08 16:37:23 -04:00
})
2024-08-08 16:53:50 -04:00
// *****************************************************
2024-09-03 12:07:59 -07:00
// Update Monitor
2024-08-08 16:53:50 -04:00
// *****************************************************
.addCase(updatePageSpeed.pending, (state) => {
state.isLoading = true;
})
.addCase(updatePageSpeed.fulfilled, (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
})
.addCase(updatePageSpeed.rejected, (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Failed to update page speed monitor";
})
2024-08-08 16:37:23 -04:00
// *****************************************************
// Delete Monitor
// *****************************************************
2024-08-08 16:53:50 -04:00
.addCase(deletePageSpeed.pending, (state) => {
2024-08-08 16:37:23 -04:00
state.isLoading = true;
})
2024-08-08 16:53:50 -04:00
.addCase(deletePageSpeed.fulfilled, (state, action) => {
2024-08-08 16:37:23 -04:00
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
})
2024-08-08 16:53:50 -04:00
.addCase(deletePageSpeed.rejected, (state, action) => {
2024-08-08 16:37:23 -04:00
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Failed to delete page speed monitor";
2024-09-03 12:07:59 -07:00
})
// *****************************************************
// Pause Monitor
// *****************************************************
.addCase(pausePageSpeed.pending, (state) => {
state.isLoading = true;
})
.addCase(pausePageSpeed.fulfilled, (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
})
.addCase(pausePageSpeed.rejected, (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Failed to pause page speed monitor";
2024-07-30 13:40:31 -04:00
});
},
});
export const { setMonitors, clearMonitorState } = pageSpeedMonitorSlice.actions;
export default pageSpeedMonitorSlice.reducer;