Guest User

Untitled

a guest
Jan 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import { connect } from "react-redux";
  3. import PropTypes from "prop-types";
  4. import RiskDetails from "./Step1/RiskDetails";
  5. import { getBranch } from "../../actions/riskActions";
  6.  
  7. class RiskForm extends Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. step: 1,
  12. risikoName: "",
  13. risikoBereich: "",
  14. risikoKlasse: "",
  15. branche: ""
  16. };
  17.  
  18. this.onChange = this.onChange.bind(this);
  19. this.onSubmit = this.onSubmit.bind(this);
  20. }
  21.  
  22. nextStep() {
  23. let step = this.state.step;
  24. this.setState({ step: step + 1 });
  25. }
  26.  
  27. prevStep() {
  28. let step = this.state.step;
  29. this.setState({ step: step - 1 });
  30. }
  31.  
  32. onChange(e) {
  33. this.setState({ [e.target.name]: e.target.value });
  34. }
  35.  
  36. onSubmit(e) {
  37. e.preventDefault();
  38. }
  39.  
  40. render() {
  41. const { step } = this.state;
  42. const { risikoName, risikoBereich, risikoKlasse, branche } = this.state;
  43. const values = { risikoName, risikoBereich, risikoKlasse, branche };
  44. switch (step) {
  45. case 1:
  46. return (
  47. <RiskDetails
  48. nextStep={this.nextStep}
  49. onChange={this.onChange}
  50. values={values}
  51. getBranch={getBranch}
  52. />
  53. );
  54. }
  55. }
  56. }
  57.  
  58. RiskForm.propTypes = {
  59. getBranch: PropTypes.func.isRequired
  60. };
  61.  
  62. export default connect(
  63. state => {
  64. return {};
  65. },
  66. { getBranch }
  67. )(RiskForm);
  68.  
  69. import React, { Component } from "react";
  70. import { Grid, Form } from "semantic-ui-react";
  71. import PropTypes from "prop-types";
  72.  
  73. class RiskDetails extends Component {
  74. componentWillMount() {
  75. this.props.getBranch().then(res => {
  76. console.log(res);
  77. });
  78. }
  79.  
  80. render() {
  81. const { value } = this.props;
  82. return (
  83. <Grid container>
  84. <Grid.Row>
  85. <Grid.Column width={8}>
  86. <Form>
  87. <Form.Select
  88. fluid
  89. label={value.branch}
  90. //options={this.state.branch}
  91. placeholder={value.branch}
  92. />
  93. </Form>
  94. </Grid.Column>
  95. </Grid.Row>
  96. </Grid>
  97. );
  98. }
  99. }
  100.  
  101. RiskDetails.propTypes = {
  102. getBranch: PropTypes.func.isRequired
  103. };
  104.  
  105. export default RiskDetails;
  106.  
  107. import axios from "axios";
  108. import { FETCH_BRANCH } from "./types";
  109.  
  110. export function createRisk(event) {
  111. return dispatch => {
  112. return axios.post("/api/risk", event);
  113. };
  114. }
  115.  
  116. export function fetchRiskDetails(risk) {
  117. return {
  118. type: FETCH_BRANCH,
  119. risk
  120. };
  121. }
  122.  
  123. export function getBranch() {
  124. return dispatch => {
  125. console.log("starting get request")
  126. return axios.get("/api/risk").then(res => {
  127. dispatch(fetchRiskDetials(res.data));
  128. console.log(res.data);
  129. });
  130. };
  131. }
  132.  
  133. import { FETCH_BRANCH } from "../actions/types";
  134.  
  135. const initialState = {
  136. risk: {}
  137. };
  138.  
  139. export default (state = initialState, action = {}) => {
  140. switch (action.type) {
  141. case FETCH_BRANCH:
  142. return {
  143. risk: action.risk
  144. };
  145. default:
  146. return state;
  147. }
  148. };
  149.  
  150. import express from "express";
  151. import path from "path";
  152. import bodyParser from "body-parser";
  153. import mongoose from "mongoose";
  154. import cors from "cors";
  155.  
  156. //WEBPACK
  157. import webpack from "webpack";
  158. import webpackMiddleware from "webpack-dev-middleware";
  159. import webpackHotMiddleware from "webpack-hot-middleware";
  160. import wepbackConfig from "../webpack.config.dev";
  161.  
  162. //IMPORT ALL API ROUTES
  163. import authRoute from "./routes/auth";
  164. import signUpRoute from "./routes/signup";
  165. import companyRoute from "./routes/company";
  166. import riskRoute from "./routes/risk";
  167. import recommendationRoute from "./routes/recommendation";
  168.  
  169. let app = express();
  170.  
  171. const compiler = webpack(wepbackConfig);
  172.  
  173. app.use(
  174. webpackMiddleware(compiler, {
  175. hot: true,
  176. publicPath: wepbackConfig.output.publicPath,
  177. noInfo: true
  178. })
  179. );
  180. app.use(cors());
  181. app.use(express.static(path.join(__dirname, "dist")));
  182. app.use(webpackHotMiddleware(compiler));
  183. app.use(bodyParser.urlencoded({ extended: true }));
  184. app.use(bodyParser.json());
  185.  
  186. // SET UP YOUR DB FOR THE API
  187. // =============================================================================
  188.  
  189.  
  190. var url = "mongodb://localhost:27017/blubb";
  191.  
  192. mongoose
  193. .connect(
  194. url,
  195. { useNewUrlParser: true, useCreateIndex: true }
  196. )
  197. .then(() => console.log("connected to DB"))
  198. .catch(err => console.log(err));
  199.  
  200. app.get("/*", (req, res) => {
  201. res.sendFile(path.join(__dirname, "./index.html"));
  202. });
  203.  
  204. // ROUTES FOR OUR API
  205. // =============================================================================
  206. app.use("/api/auth", authRoute);
  207. app.use("/api/risk", riskRoute);
  208.  
  209. app.listen(3000, () => console.log("running"));
  210.  
  211. import express from "express";
  212. import {
  213. validatePostInput,
  214. validateDeleteInput
  215. } from "../shared/validation/risk";
  216. import Risk from "../models/risk";
  217. import authenticate from "../middleware/authenticate";
  218.  
  219. let router = express.Router();
  220.  
  221. router.get("/", (req, res) => {
  222. console.log("hi form server");
  223. Risk.find(function(err, risks) {
  224. if (err) {
  225. console.log(err);
  226. } else {
  227. res.status(400).json(risks);
  228. }
  229. });
  230. });
  231.  
  232. //create new risk
  233. router.post("/", authenticate, (req, res) => {
  234. const { errors, isValid } = validatePostInput(req.body);
  235. console.log(req.body);
  236. if (isValid) {
  237. const { risikoName, risikoBereich, risikoKlasse, createdBy } = req.body;
  238. var newRisk = new Risk({
  239. risikoName,
  240. risikoBereich,
  241. risikoKlasse,
  242. createdBy
  243. });
  244.  
  245. // save the risk
  246. newRisk.save(function(err) {
  247. if (err) {
  248. return res.status(500).json({ error: err });
  249. }
  250. res.json({ success: true });
  251. });
  252. } else {
  253. res.status(400).json(errors);
  254. }
  255. });
  256.  
  257. //delete risk
  258. router.delete("/", (req, res) => {
  259. const { errors, isValid } = validateDeleteInput(req.body);
  260. if (isValid) {
  261. const { id } = req.body;
  262. Risk.remove({ _id: id }, function(err, risk) {
  263. if (err) return res.status(500).json({ error: err });
  264. res.json({ success: true });
  265. });
  266. } else {
  267. res.status(400).json(errors);
  268. }
  269. });
  270.  
  271. export default router;
  272.  
  273. import path from "path";
  274. import webpack from "webpack";
  275.  
  276. export default {
  277. mode: "development",
  278. entry: [
  279. "webpack-hot-middleware/client",
  280. path.join(__dirname, "client/index.js")
  281. ],
  282. output: {
  283. filename: "bundle.js",
  284. path: "/",
  285. publicPath: "/"
  286. },
  287. plugins: [
  288. new webpack.NoEmitOnErrorsPlugin(),
  289. new webpack.optimize.OccurrenceOrderPlugin(),
  290. new webpack.HotModuleReplacementPlugin()
  291. ],
  292. module: {
  293. rules: [
  294. {
  295. test: /.js$/,
  296. include: [
  297. path.join(__dirname, "client"),
  298. path.join(__dirname, "server/shared")
  299. ],
  300. loaders: ["react-hot-loader/webpack", "babel-loader"]
  301. },
  302. {
  303. test: /.css$/,
  304. loader: "style-loader!css-loader"
  305. },
  306. {
  307. test: /.s[a|c]ss$/,
  308. loader: "sass-loader!style-loader!css-loader"
  309. },
  310. {
  311. test: /.(jpg|png|gif|jpeg|woff|woff2|eot|ttf|svg)$/,
  312. loader: "url-loader?limit=100000"
  313. }
  314. ]
  315. },
  316. resolve: {
  317. extensions: [".js"]
  318. }
  319. };
Add Comment
Please, Sign In to add comment