now everything works with React ToolKit

This commit is contained in:
QkoSad
2023-06-06 22:28:48 +03:00
parent 35b69eae3f
commit 703784307a
52 changed files with 36817 additions and 7677 deletions
+98 -121
View File
@@ -1,57 +1,48 @@
import api from '../utils/api';
import { setAlert } from './alert';
import api from "../utils/api";
import { createAlert } from "./alert";
import {
GET_PROFILE,
GET_PROFILES,
PROFILE_ERROR,
UPDATE_PROFILE,
CLEAR_PROFILE,
ACCOUNT_DELETED,
GET_REPOS,
NO_REPOS
} from './types';
noRepos,
getRepos,
clearProfile,
profileError,
getProfilesType,
updateProfile,
getProfile,
} from "../reducers/profile";
import { accountDeleted } from "../reducers/auth";
/*
NOTE: we don't need a config object for axios as the
default headers in axios are already Content-Type: application/json
also axios stringifies and parses JSON for you, so no need for
JSON.stringify or JSON.parse
*/
// Get current users profile
export const getCurrentProfile = () => async (dispatch) => {
try {
const res = await api.get('/profile/me');
const res = await api.get("/profile/me");
dispatch({
type: GET_PROFILE,
payload: res.data
});
dispatch(getProfile(res.data));
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status }
});
dispatch(
profileError({
msg: err.response.statusText,
status: err.response.status,
})
);
}
};
// Get all profiles
export const getProfiles = () => async (dispatch) => {
dispatch({ type: CLEAR_PROFILE });
dispatch(clearProfile());
try {
const res = await api.get('/profile');
const res = await api.get("/profile");
dispatch({
type: GET_PROFILES,
payload: res.data
});
dispatch(getProfilesType(res.data));
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status }
});
dispatch(
profileError({
msg: err.response.statusText,
status: err.response.status,
})
);
}
};
@@ -60,15 +51,14 @@ export const getProfileById = (userId) => async (dispatch) => {
try {
const res = await api.get(`/profile/user/${userId}`);
dispatch({
type: GET_PROFILE,
payload: res.data
});
dispatch(getProfile(res.data));
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status }
});
dispatch(
profileError({
msg: err.response.statusText,
status: err.response.status,
})
);
}
};
@@ -77,101 +67,88 @@ export const getGithubRepos = (username) => async (dispatch) => {
try {
const res = await api.get(`/profile/github/${username}`);
dispatch({
type: GET_REPOS,
payload: res.data
});
dispatch(getRepos(res.data));
} catch (err) {
dispatch({
type: NO_REPOS
});
dispatch(noRepos());
}
};
// Create or update profile
export const createProfile =
(formData, navigate, edit = false) =>
(formData, edit = false) =>
async (dispatch) => {
try {
const res = await api.post('/profile', formData);
const res = await api.post("/profile", formData);
dispatch({
type: GET_PROFILE,
payload: res.data
});
dispatch(getProfile(res.data));
dispatch(
setAlert(edit ? 'Profile Updated' : 'Profile Created', 'success')
createAlert(edit ? "Profile Updated" : "Profile Created", "success")
);
if (!edit) {
navigate('/dashboard');
}
} catch (err) {
const errors = err.response.data.errors;
if (errors) {
errors.forEach((error) => dispatch(setAlert(error.msg, 'danger')));
errors.forEach((error) => dispatch(createAlert(error.msg, "danger")));
}
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status }
});
dispatch(
profileError({
msg: err.response.statusText,
status: err.response.status,
})
);
}
};
// Add Experience
export const addExperience = (formData, navigate) => async (dispatch) => {
export const addExperience = (formData) => async (dispatch) => {
try {
const res = await api.put('/profile/experience', formData);
const res = await api.put("/profile/experience", formData);
dispatch({
type: UPDATE_PROFILE,
payload: res.data
});
dispatch(updateProfile(res.data));
dispatch(setAlert('Experience Added', 'success'));
dispatch(createAlert("Experience Added", "success"));
navigate('/dashboard');
} catch (err) {
const errors = err.response.data.errors;
if (errors) {
errors.forEach((error) => dispatch(setAlert(error.msg, 'danger')));
errors.forEach((error) => dispatch(createAlert(error.msg, "danger")));
}
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status }
});
dispatch(
profileError({
msg: err.response.statusText,
status: err.response.status,
})
);
}
};
// Add Education
export const addEducation = (formData, navigate) => async (dispatch) => {
export const addEducation = (formData) => async (dispatch) => {
try {
const res = await api.put('/profile/education', formData);
const res = await api.put("/profile/education", formData);
dispatch({
type: UPDATE_PROFILE,
payload: res.data
});
dispatch(updateProfile(res.data));
dispatch(setAlert('Education Added', 'success'));
dispatch(createAlert("Education Added", "success"));
navigate('/dashboard');
} catch (err) {
const errors = err.response.data.errors;
if (errors) {
errors.forEach((error) => dispatch(setAlert(error.msg, 'danger')));
errors.forEach((error) => dispatch(createAlert(error.msg, "danger")));
}
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status }
});
dispatch(
profileError({
msg: err.response.statusText,
status: err.response.status,
})
);
}
};
@@ -180,17 +157,16 @@ export const deleteExperience = (id) => async (dispatch) => {
try {
const res = await api.delete(`/profile/experience/${id}`);
dispatch({
type: UPDATE_PROFILE,
payload: res.data
});
dispatch(updateProfile(res.data));
dispatch(setAlert('Experience Removed', 'success'));
dispatch(createAlert("Experience Removed", "success"));
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status }
});
dispatch(
profileError({
msg: err.response.statusText,
status: err.response.status,
})
);
}
};
@@ -199,35 +175,36 @@ export const deleteEducation = (id) => async (dispatch) => {
try {
const res = await api.delete(`/profile/education/${id}`);
dispatch({
type: UPDATE_PROFILE,
payload: res.data
});
dispatch(updateProfile(res.data));
dispatch(setAlert('Education Removed', 'success'));
dispatch(createAlert("Education Removed", "success"));
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status }
});
dispatch(
profileError({
msg: err.response.statusText,
status: err.response.status,
})
);
}
};
// Delete account & profile
export const deleteAccount = () => async (dispatch) => {
if (window.confirm('Are you sure? This can NOT be undone!')) {
if (window.confirm("Are you sure? This can NOT be undone!")) {
try {
await api.delete('/profile');
await api.delete("/profile");
dispatch({ type: CLEAR_PROFILE });
dispatch({ type: ACCOUNT_DELETED });
dispatch(clearProfile());
dispatch(accountDeleted());
dispatch(setAlert('Your account has been permanently deleted'));
dispatch(createAlert("Your account has been permanently deleted"));
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status }
});
dispatch(
profileError({
msg: err.response.statusText,
status: err.response.status,
})
);
}
}
};
};