Skip to content

Commit

Permalink
finalizing project
Browse files Browse the repository at this point in the history
  • Loading branch information
aomini committed Aug 13, 2019
1 parent 80913c1 commit 95bb550
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 4 deletions.
9 changes: 7 additions & 2 deletions client/src/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import axios from "axios";
import { FETCH_USER } from "./types";
import { FETCH_USER, FETCH_SURVEYS } from "./types";

const fetchUser = () => async dispatch => {
const res = await axios.get("/api/currentUser");
dispatch({ type: FETCH_USER, payload: res.data });
};

const fetchSurveys = () => async dispatch => {
const res = await axios.get("/api/surveys");
dispatch({ type: FETCH_SURVEYS, payload: res.data });
};

const handleToken = token => async dispatch => {
const res = await axios.post("/api/stripe", token);
dispatch({ type: FETCH_USER, payload: res.data });
Expand All @@ -17,4 +22,4 @@ const sendSurvey = (survey, history) => async dispatch => {
dispatch({ type: FETCH_USER, payload: res.data });
};

export { fetchUser, handleToken, sendSurvey };
export { fetchUser, handleToken, sendSurvey, fetchSurveys };
1 change: 1 addition & 0 deletions client/src/actions/types.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const FETCH_USER = "fetch_user";
export const FETCH_SURVEYS = "fetch_surveys";
3 changes: 2 additions & 1 deletion client/src/components/Dashboard.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from "react";
import { Link } from "react-router-dom";
import SurveyList from "./Surveys/SurveyLists";

const Dashboard = () => {
return (
<div>
Dashboard
<SurveyList />
<div className="fixed-action-btn">
<Link className="btn-floating btn-large red" to="/surveys/new">
<i className="material-icons">add</i>
Expand Down
40 changes: 40 additions & 0 deletions client/src/components/Surveys/SurveyLists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { Component } from "react";
import { fetchSurveys } from "../../actions";
import { connect } from "react-redux";

class SurveyList extends Component {
componentDidMount() {
this.props.fetchSurveys();
}

renderSurveys() {
return this.props.surveys.reverse().map(survey => {
return (
<div className="card darken-1" key={survey._id}>
<div className="card-content">
<span className="card-title">{survey.title}</span>
<p>{survey.body}</p>
<p className="right">
Sent On: {new Date(survey.dateSent).toLocaleDateString()}
</p>
</div>
<div className="card-action">
<a>YES: {survey.yes}</a>
<a>NO: {survey.no}</a>
</div>
</div>
);
});
}

render() {
return <div>{this.renderSurveys()}</div>;
}
}

const mapStateToProps = ({ surveys }) => ({ surveys });

export default connect(
mapStateToProps,
{ fetchSurveys }
)(SurveyList);
4 changes: 3 additions & 1 deletion client/src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { combineReducers } from "redux";
import { reducer as formReducer } from "redux-form";

import authReducer from "./authReducer";
import surveyReducer from "./surveyReducer";

export default combineReducers({
auth: authReducer,
form: formReducer
form: formReducer,
surveys: surveyReducer
});
10 changes: 10 additions & 0 deletions client/src/reducers/surveyReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { FETCH_SURVEYS } from "../actions/types";

export default (state = [], action) => {
switch (action.type) {
case FETCH_SURVEYS:
return action.payload || state;
default:
return state;
}
};
7 changes: 7 additions & 0 deletions routes/surveyRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ module.exports = app => {
res.send({});
});

app.get("/api/surveys", auth, async (req, res) => {
const surveys = await Survey.find({ _user: req.user.id }).select(
"-recipents"
);
res.send(surveys);
});

app.post("/api/surveys", auth, credits, async (req, res) => {
const { title, subject, body, recipents } = req.body;
const survey = new Survey({
Expand Down

0 comments on commit 95bb550

Please sign in to comment.