refactord pagespeed slice

This commit is contained in:
Alex Holliday committed 2024-08-17 09:50:52 -07:00
1 parent ceb8985afb
commit 10fceb6ff1
2 files changed
+31 -77

No files matched your search

@@ -13,32 +13,7 @@ export const createPageSpeed = 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 getPageSpeedMonitors = createAsyncThunk(
"pageSpeedMonitors/getPageSpeedMonitors",
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,14 +33,15 @@ export const getPageSpeedByUserId = 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=pagespeed&sortOrder=desc`, token,
{ user._id,
headers: { 25,
Authorization: `Bearer ${token}`, ["pagespeed"],
}, "desc",
} false
); );
return res.data; return res.data;
} catch (error) { } catch (error) {
if (error.response && error.response.data) { if (error.response && error.response.data) {
@@ -91,15 +67,10 @@ export const updatePageSpeed = 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 +91,7 @@ export const deletePageSpeed = 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 +119,6 @@ const pageSpeedMonitorSlice = createSlice({
}, },
extraReducers: (builder) => { extraReducers: (builder) => {
builder builder
// *****************************************************
// All Monitors
// *****************************************************
.addCase(getPageSpeedMonitors.pending, (state) => {
state.isLoading = true;
})
.addCase(getPageSpeedMonitors.fulfilled, (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
state.monitors = action.payload.data;
})
.addCase(getPageSpeedMonitors.rejected, (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Getting montiors failed";
})
// ***************************************************** // *****************************************************
// Monitors by userId // Monitors by userId
// ***************************************************** // *****************************************************
+17 -10
View File
@@ -43,16 +43,23 @@ axiosInstance.getMonitorsByUserId = async (
sortOrder, sortOrder,
normalize normalize
) => { ) => {
const typeQuery = types.reduce((acc, type) => acc + `&type=${type}`, ""); const params = new URLSearchParams();
return axiosInstance.get(
`/monitors/user/${userId}?limit=${limit}${typeQuery}&sortOrder=${sortOrder}&normalize=${normalize}`, if (limit) params.append("limit", limit);
{ if (types) {
headers: { types.forEach((type) => {
Authorization: `Bearer ${authToken}`, params.append("type", type);
"Content-Type": "application/json", });
}, }
} if (sortOrder) params.append("sortOrder", sortOrder);
); if (normalize) params.append("normalize", normalize);
return axiosInstance.get(`/monitors/user/${userId}?${params.toString()}`, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
});
}; };
// Updates a single monitor // Updates a single monitor