From 277632a066351a6a0e7259d351ee5c74ca542b18 Mon Sep 17 00:00:00 2001
From: rlacko <rlacko@sch.bme.hu>
Date: Sun, 26 Jan 2020 00:47:33 +0100
Subject: [PATCH] #62 get the Deadline from database

---
 src/actions/auth.js             | 17 ++++++++++++++++-
 src/actions/types.js            |  1 +
 src/components/pages/Profile.js | 24 ++++++++++++++++--------
 src/reducers/UserReducer.js     |  3 +++
 4 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/src/actions/auth.js b/src/actions/auth.js
index fbe1664..5950df0 100644
--- a/src/actions/auth.js
+++ b/src/actions/auth.js
@@ -1,5 +1,5 @@
 import axios from './session';
-import { GET_USERDATA, PROFILE_CHANGE, GROUP_CHANGE } from './types';
+import { GET_USERDATA, PROFILE_CHANGE, GROUP_CHANGE, GET_DEADLINE } from './types';
 
 
 export const getUserData = () => (
@@ -45,6 +45,21 @@ export const getUserData = () => (
   }
 );
 
+export const getDeadline = () => (
+  async (dispatch) => {
+    try {
+      const response = await axios.get('/api/v1/profiles/deadline');
+
+      dispatch({
+        type: GET_DEADLINE,
+        payload: response.data,
+      });
+    } catch (e) {
+      console.log(e);
+    }
+  }
+);
+
 export const textChange = ({ target: { name, value } }) => (
   (dispatch) => {
     dispatch({ type: PROFILE_CHANGE, payload: value, target: name });
diff --git a/src/actions/types.js b/src/actions/types.js
index d298902..df9349e 100644
--- a/src/actions/types.js
+++ b/src/actions/types.js
@@ -1,5 +1,6 @@
 export const GET_USERDATA = 'get_userdata';
 export const GET_NEWS = 'get_news';
+export const GET_DEADLINE = 'get_deadline';
 
 export const PROFILE_CHANGE = 'profile_change';
 export const GROUP_CHANGE = 'group_change';
diff --git a/src/components/pages/Profile.js b/src/components/pages/Profile.js
index 719a4b9..be3166e 100644
--- a/src/components/pages/Profile.js
+++ b/src/components/pages/Profile.js
@@ -2,6 +2,7 @@ import React, { Component } from 'react';
 import { Container, Form, Dropdown, Divider, Segment } from 'semantic-ui-react';
 import { connect } from 'react-redux';
 import { textChange, submitRegistration, groupChange } from '../../actions';
+import { getDeadline } from '../../actions/auth'
 import HiddenForm from '../forms/HiddenForm'
 
 const options = [
@@ -14,6 +15,7 @@ const options = [
 
 class Profile extends Component {
   componentWillMount() {
+    this.props.getDeadline()
     if (!this.props.id) {
       this.props.history.push('/home');
     }
@@ -21,22 +23,25 @@ class Profile extends Component {
 
   render() {
     const {
-      nick, groups, motivationAbout, motivationProfession, motivationExercise, signed, id,
+      nick, groups, motivationAbout, motivationProfession, motivationExercise, signed, id, deadline, text: deadlineText
     } = this.props;
-    const endDate = new Date(2020, 2, 14, 23, 59, 59)
-    const endDateText = `februĂĄr 14. 23:59-ig`
+    const endDate = new Date(deadline)
+    const endDateText = deadlineText
     let canEdit = Date.now()<endDate
     return (
       <Container
         style={{
-          marginTop: '0.5em',
+          marginTop: '1em',
         }}
       >
+        {canEdit ?
         <Segment inverted color='red' tertiary>
           <p style={{ fontSize: '1.33em' }}>
             A profilod mentés után is módosítható a későbbiekben, egészen {endDateText}.
           </p>
         </Segment>
+        : ''}
+
         <Form>
           {canEdit ? 
             <Form.Input
@@ -194,11 +199,12 @@ class Profile extends Component {
             }
             checked={signed}
             readOnly={!canEdit}
+            style={ !canEdit ? { marginBottom: '5em' } : null}
           />
           {canEdit ? 
           <Form.Button
             primary
-            style={{ marginBottom: '10em' }}
+            style={{ marginBottom: '5em' }}
             onClick={() => this.props.submitRegistration({
               nick, motivationAbout, motivationProfession, motivationExercise, signed, groups, id,
             })}
@@ -214,8 +220,8 @@ class Profile extends Component {
 
 const mapStateToProps = ({
   user: {
-    nick, groups, motivationAbout, motivationProfession, motivationExercise, signed, id,
-  },
+    nick, groups, motivationAbout, motivationProfession, motivationExercise, signed, id, deadline, text
+  }
 }) => ({
   nick,
   groups,
@@ -224,6 +230,8 @@ const mapStateToProps = ({
   motivationExercise,
   signed,
   id,
+  deadline,
+  text
 });
 
-export default connect(mapStateToProps, { textChange, submitRegistration, groupChange })(Profile);
+export default connect(mapStateToProps, { textChange, submitRegistration, groupChange, getDeadline })(Profile);
diff --git a/src/reducers/UserReducer.js b/src/reducers/UserReducer.js
index 839a47e..bcf9492 100644
--- a/src/reducers/UserReducer.js
+++ b/src/reducers/UserReducer.js
@@ -2,6 +2,7 @@ import {
   GET_USERDATA,
   PROFILE_CHANGE,
   GROUP_CHANGE,
+  GET_DEADLINE,
 } from '../actions/types';
 
 const INITIAL_STATE = {
@@ -11,6 +12,8 @@ export default (state = INITIAL_STATE, action) => {
   switch (action.type) {
     case GET_USERDATA:
       return { ...state, ...action.payload };
+    case GET_DEADLINE:
+      return { ...state, ...action.payload };
     case PROFILE_CHANGE:
       return { ...state, [action.target]: action.payload };
     case GROUP_CHANGE:
-- 
GitLab