Commit 4c152553 authored by Tamás Danis's avatar Tamás Danis

implemented settings

parent 73db7369
......@@ -13,7 +13,7 @@ class HomeController extends Controller
*/
public function __construct()
{
$this->middleware('auth');
// $this->middleware('auth');
}
/**
......
......@@ -98,6 +98,23 @@ class UserController extends Controller {
}
return response()->json( $response, 201 );
}
public function update( Request $request ) {
$user = User::find( $request['userID'] );
$user->name = $request['username'];
$user->first_name = $request['first_name'];
$user->last_name = $request['last_name'];
$user->max_limit = $request['max_limit'];
$user->save();
$response = [
'success' => true,
'data' => $user
];
return response()->json( $response, 201 );
}
}
......@@ -4,6 +4,8 @@ export const REGISTER_SUCCESS = 'REGISTER_SUCCESS';
export const REGISTER_ERROR = 'REGISTER_ERROR';
export const LOGOUT = 'LOGOUT';
export const UPDATE_USER = 'UPDATE_USER';
export const FETCH_TRANSACTIONS = 'FETCH_TRANSACTIONS';
export const FETCH_TRANSACTIONS_SUCCESS = 'FETCH_TRANSACTIONS_SUCCESS';
export const FETCH_TRANSACTIONS_ERROR = 'FETCH_TRANSACTIONS_ERROR';
......
import API from '../api/API';
import * as types from './actionTypes';
export function updateSettings(values) {
return dispatch =>
API.updateUser(values)
.then(data => {
if (data.success) {
dispatch({ type: types.UPDATE_USER, user: data.data });
}
})
.catch(error => {
throw new Error(error);
});
}
......@@ -18,7 +18,6 @@ export function saveTransaction(data) {
return dispatch =>
API.saveTransaction(data)
.then(data => {
console.log(data)
if (data.success)
dispatch({ type: types.SAVE_TRANSACTION_SUCCESS, data: data.data });
else throw new Error(data.data);
......
......@@ -2,6 +2,7 @@ import axios from 'axios';
const loginUrl = '/api/user/login';
const registerUrl = '/api/user/register';
const updateUserUrl = 'api/user/update?token=';
const saveTransactionUrl = `/api/transaction/save?token=`;
const fetchTransactionUrl = `/api/transactions?token=`;
......@@ -36,13 +37,30 @@ class API {
});
}
static updateUser(userData) {
const url = `${updateUserUrl}${getToken()}`;
const payload = JSON.stringify({
...userData,
userID: getUserId()
});
return new Promise(resolve => {
axios
.post(url, payload)
.then(response => {
resolve(response.data);
})
.catch(error => {
resolve(error);
});
});
}
static saveTransaction(data) {
const payload = JSON.stringify({
...data,
userId: getUserId()
});
const url = `${saveTransactionUrl}${getToken()}`;
// console.log(url, payload);
return new Promise(resolve => {
axios.post(url, payload)
.then(response => {
......
......@@ -6,7 +6,6 @@ import TransactionListItem from './TransactionList/TransactionListItem';
import * as transactionActions from '../../../../actions/transactionActions';
const TransactionList = ({ actions, transactions }) => {
// console.log(transactions)
useEffect(() => {
actions.fetchTransactions();
......
import React from 'react';
import SettingsForm from './Settings/SettingsForm';
import UserInfo from './Settings/UserInfo';
const Settings = () => (
<>
<h1>Settings</h1>
<h1 className="pt-3">Settings</h1>
<div className="row">
<div className="col-md-8">
<SettingsForm/>
</div>
<div className="col-md-4">
<UserInfo/>
</div>
</div>
</>
);
......
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Field, reduxForm } from 'redux-form';
import { updateSettings } from '../../../../actions/settingsActions';
import ButtonField from '../../../common/form/ButtonField';
import { maxLength15, minLength2, required } from '../../../../validation/validation';
import InputField from '../../../common/form/InputField';
import InputPriceField from '../../../common/form/InputPriceField';
const SettingsForm = ({ handleSubmit, valid }) => {
return (
<>
<h3>Update your preferences</h3>
<form onSubmit={handleSubmit}>
<div className="row">
<div className="col-md-12">
<Field
label="Username"
type="text"
name="username"
component={InputField}
validate={[required, minLength2, maxLength15]}
/>
<div className="row">
<div className="col-md-6">
<Field
label="First name"
type="text"
name="first_name"
component={InputField}
validate={[required, minLength2, maxLength15]}
/>
</div>
<div className="col-md-6">
<Field
label="Last name"
type="text"
name="last_name"
component={InputField}
validate={[required, minLength2, maxLength15]}
/>
</div>
</div>
<Field
label="Max limit"
type="number"
name="max_limit"
component={InputPriceField}
validate={[required]}
/>
</div>
</div>
<div className='row'>
<div className="col-md-12">
<ButtonField value="Update" disabled={!valid}/>
</div>
</div>
</form>
</>
);
};
SettingsForm.propTypes = {
handleSubmit: PropTypes.func.isRequired,
valid: PropTypes.bool.isRequired
};
const mapStateToProps = state => {
// eslint-disable-next-line camelcase
const { name, first_name, last_name, max_limit } = state.auth.user;
return {
initialValues: {
username: name,
first_name,
last_name,
max_limit
}
};
};
const mapDispatchToProps = dispatch => ({
onSubmit: values => dispatch(updateSettings(values))
}
);
export default connect(mapStateToProps, mapDispatchToProps)(reduxForm({
form: 'settingsForm'
})(SettingsForm));
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
const UserInfo = ({ user }) => (
<aside>
<div className="card">
<div className="card-body">
<h5 className="card-title">User info</h5>
</div>
<ul className="list-group list-group-flush">
<li className="list-group-item"><b>Username:</b> {user.name}</li>
<li className="list-group-item"><b>Full name:</b> {
`${user.first_name} ${user.last_name}`
}</li>
<li className="list-group-item"><b>Max limit:</b> {user.max_limit}&euro;</li>
</ul>
</div>
</aside>
);
UserInfo.propTypes = {
user: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
user: state.auth.user
});
export default connect(mapStateToProps)(UserInfo);
......@@ -2,6 +2,7 @@ import * as types from '../actions/actionTypes';
import initialState from './initialState';
const auth = (state = initialState.auth, action) => {
let user;
switch (action.type) {
case types.LOGIN_SUCCESS:
localStorage.setItem('auth_user', JSON.stringify(action.user));
......@@ -29,6 +30,16 @@ const auth = (state = initialState.auth, action) => {
user: {},
loggedIn: false
};
case types.UPDATE_USER:
user = {
...state.user,
...action.user
};
localStorage.setItem('auth_user', JSON.stringify(action.user));
return {
...state,
user
};
default:
return state;
}
......
import * as types from '../actions/actionTypes';
import initialState from './initialState';
const transaction = (state = initialState.transactions, action) => {
switch (action.type) {
case types.FETCH_TRANSACTIONS:
return action.transactions;
// case types.FETCH_TRANSACTIONS_SUCCESS:
// return {
// ...state
// };
case types.FETCH_TRANSACTIONS_ERROR:
return [
...state
];
case types.SAVE_TRANSACTION_SUCCESS:
return [
...state,
action.data
];
default:
return state;
}
};
export default transaction;
import { combineReducers } from 'redux';
import { reducer as formReducer } from 'redux-form';
import auth from './authReducer';
import transaction from './transactionReducer'
import transaction from './transactionReducer';
const rootReducer = combineReducers({
auth,
form: formReducer,
transactions:transaction
auth,
form: formReducer,
transactions: transaction
});
export default rootReducer;
......@@ -9,5 +9,5 @@ export default {
redirect: false
},
form: {},
transactions: []
transactions: [],
};
......@@ -5,16 +5,14 @@ const transaction = (state = initialState.transactions, action) => {
switch (action.type) {
case types.FETCH_TRANSACTIONS:
return action.transactions;
case types.FETCH_TRANSACTIONS_SUCCESS:
return {
...state
};
case types.FETCH_TRANSACTIONS_ERROR:
return action.data;
return [
...state
];
case types.SAVE_TRANSACTION_SUCCESS:
return [
action.data,
...state
...state,
];
default:
return state;
......
......@@ -27,6 +27,8 @@ Route::group( [ 'middleware' => [ 'jwt.auth', 'api-header' ] ], function () {
return response()->json( $response, 201 );
} );
Route::post('user/update', 'UserController@update');
Route::post( 'transaction/save', 'TransactionController@save' );
Route::get( 'transactions', 'TransactionController@get' );
......@@ -38,6 +40,6 @@ Route::group( [ 'middleware' => 'api-header' ], function () {
// Therefore the jwtMiddleware will be exclusive of them
Route::post( 'user/login', 'UserController@login' );
Route::post( 'user/register', 'UserController@register' );
Route::post( 'product', 'ProductController@save' );
Route::get( 'products', 'ProductController@get' );
// Route::post( 'product', 'ProductController@save' );
// Route::get( 'products', 'ProductController@get' );
} );
......@@ -11,7 +11,9 @@
|
*/
Route::get('/', 'HomeController@index');
Route::get( '/', 'HomeController@index' );
Route::get( '/settings', 'HomeController@index' );
Route::get( '/groups', 'HomeController@index' );
//Route::get('/home', 'HomeController@index')->name('home');
Auth::routes();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment