Skip to content

Commit

Permalink
added delete education and experience, connected to buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
luckynumberke7in committed Oct 31, 2020
1 parent bf21627 commit 0bb8d0c
Show file tree
Hide file tree
Showing 8 changed files with 4,839 additions and 762 deletions.
5,376 changes: 4,679 additions & 697 deletions MERN-social-app.sublime-workspace

Large diffs are not rendered by default.

68 changes: 67 additions & 1 deletion client/src/actions/profile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import axios from "axios";
import { setAlert } from "./alert";

import { GET_PROFILE, PROFILE_ERROR, UPDATE_PROFILE } from "./types";
import {
GET_PROFILE,
PROFILE_ERROR,
UPDATE_PROFILE,
CLEAR_PROFILE,
ACCOUNT_DELETED,
} from "./types";

// get req for current user profile... or display error
export const getCurrentProfile = () => async (dispatch) => {
Expand Down Expand Up @@ -123,3 +129,63 @@ export const addEducation = (formData, history) => async (dispatch) => {
});
}
};

// delete experience
export const deleteExperience = (id) => async (dispatch) => {
try {
const res = await axios.delete(`/api/profile/experience/${id}`);

dispatch({
type: UPDATE_PROFILE,
payload: res.data,
});

dispatch(setAlert("Experience Removed", "success"));
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status },
});
}
};

// delete education
export const deleteEducation = (id) => async (dispatch) => {
try {
const res = await axios.delete(`/api/profile/education/${id}`);

dispatch({
type: UPDATE_PROFILE,
payload: res.data,
});

dispatch(setAlert("Education Removed", "success"));
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status },
});
}
};

// delete account and profile
export const deleteAccount = () => async (dispatch) => {
if (window.confirm("Are you sure? This can NOT be undone!")) {
try {
const res = await axios.delete("api/profile");

dispatch({ type: CLEAR_PROFILE });
dispatch({
type: ACCOUNT_DELETED,
payload: res.data,
});

dispatch(setAlert("Your account has been permanantly deleted"));
} catch (err) {
dispatch({
type: PROFILE_ERROR,
payload: { msg: err.response.statusText, status: err.response.status },
});
}
}
};
1 change: 1 addition & 0 deletions client/src/actions/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export const GET_PROFILE = "GET_PROFILE";
export const UPDATE_PROFILE = "UPDATE_PROFILE";
export const CLEAR_PROFILE = "CLEAR_PROFILE";
export const PROFILE_ERROR = "PROFILE_ERROR";
export const ACCOUNT_DELETED = "ACCOUNT_DELETED";
15 changes: 13 additions & 2 deletions client/src/components/dashboard/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import Spinner from "../layout/Spinner";
import DashboardActions from "./DashboardActions";
import Education from "./Education";
import Experience from "./Experience";
import { getCurrentProfile } from "../../actions/profile";
import { getCurrentProfile, deleteAccount } from "../../actions/profile";

const Dashboard = ({
getCurrentProfile,
deleteAccount,
auth: { user },
profile: { profile, loading },
}) => {
Expand All @@ -29,6 +30,13 @@ const Dashboard = ({
<DashboardActions />
<Experience experience={profile.experience} />
<Education education={profile.education} />

<div className="my-2">
<button className="btn btn-danger" conClick={() => deleteAccount()}>
<i className="fas fa-user-minus" />
Delete My Account
</button>
</div>
</Fragment>
) : (
<Fragment>
Expand All @@ -44,6 +52,7 @@ const Dashboard = ({

Dashboard.propTypes = {
getCurrentProfile: PropTypes.func.isRequired,
deleteAccount: PropTypes.func.isRequired,
auth: PropTypes.object.isRequired,
profile: PropTypes.object.isRequired,
};
Expand All @@ -53,4 +62,6 @@ const mapStateToProps = (state) => ({
profile: state.profile,
});

export default connect(mapStateToProps, { getCurrentProfile })(Dashboard);
export default connect(mapStateToProps, { getCurrentProfile, deleteAccount })(
Dashboard
);
13 changes: 10 additions & 3 deletions client/src/components/dashboard/Education.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import React, { Fragment } from "react";
import PropTypes from "prop-types";
import Moment from "react-moment";
import { connect } from "react-redux";
import { deleteEducation } from "../../actions/profile";

const Education = ({ education }) => {
const Education = ({ education, deleteEducation }) => {
const educations = education.map((edu) => (
<tr key={edu._id}>
<td>{edu.school}</td>
Expand All @@ -17,7 +18,12 @@ const Education = ({ education }) => {
)}
</td>
<td>
<button className="btn btn-danger">Delete</button>
<button
onClick={() => deleteEducation(edu._id)}
className="btn btn-danger"
>
Delete
</button>
</td>
</tr>
));
Expand All @@ -40,6 +46,7 @@ const Education = ({ education }) => {

Education.propTypes = {
education: PropTypes.array.isRequired,
deleteEducation: PropTypes.func.isRequired,
};

export default Education;
export default connect(null, { deleteEducation })(Education);
13 changes: 10 additions & 3 deletions client/src/components/dashboard/Experience.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import React, { Fragment } from "react";
import PropTypes from "prop-types";
import Moment from "react-moment";
import { connect } from "react-redux";
import { deleteExperience } from "../../actions/profile";

const Experience = ({ experience }) => {
const Experience = ({ experience, deleteExperience }) => {
const experiences = experience.map((exp) => (
<tr key={exp._id}>
<td>{exp.company}</td>
Expand All @@ -17,7 +18,12 @@ const Experience = ({ experience }) => {
)}
</td>
<td>
<button className="btn btn-danger">Delete</button>
<button
onClick={() => deleteExperience(exp._id)}
className="btn btn-danger"
>
Delete
</button>
</td>
</tr>
));
Expand All @@ -40,6 +46,7 @@ const Experience = ({ experience }) => {

Experience.propTypes = {
experience: PropTypes.array.isRequired,
deleteExperience: PropTypes.func.isRequired,
};

export default Experience;
export default connect(null, { deleteExperience })(Experience);
14 changes: 8 additions & 6 deletions client/src/reducers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import {
LOGIN_SUCCESS,
LOGIN_FAIL,
LOGOUT,
} from '../actions/types';
ACCOUNT_DELETED,
} from "../actions/types";

// set state for action taking place
const initialState = {
token: localStorage.getItem('token'),
token: localStorage.getItem("token"),
isAuthenticated: null,
loading: true,
user: null,
Expand All @@ -30,18 +31,19 @@ export default function (state = initialState, action) {
};
case REGISTER_SUCCESS:
case LOGIN_SUCCESS:
localStorage.setItem('token', payload.token);
localStorage.setItem("token", payload.token);
return {
...state,
...payload,
isAuthenticated: true,
loading: false,
};
case REGISTER_FAIL:
case LOGIN_FAIL:
case AUTH_ERROR:
case ACCOUNT_DELETED:
case LOGIN_FAIL:
case LOGOUT:
localStorage.removeItem('token');
case REGISTER_FAIL:
localStorage.removeItem("token");
return {
...state,
...payload,
Expand Down
Loading

0 comments on commit 0bb8d0c

Please sign in to comment.