Skip to content

Commit

Permalink
Merge pull request #5 from TK-data/Frontend_display_registered_cars
Browse files Browse the repository at this point in the history
Frontend display registered cars
  • Loading branch information
emilps authored May 11, 2018
2 parents c88e4e7 + 2730ea9 commit 37c436d
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 10 deletions.
44 changes: 44 additions & 0 deletions bpdashboard/src/actions/cars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { API_ADDRESS } from '../config/connections';

export const GET_CARS_REQUEST = '_CARS_REQUEST';
export const GET_CARS_SUCCESS = 'GET_CARS_SUCCESS';
export const GET_CARS_FAILURE = 'GET_CARS_FAILURE';

const axios = require('axios');

axios.defaults.withCredentials = true;

export function getCarsFailure(message) {
return {
type: 'GET_CARS_FAILURE',
hasErrored: message,
};
}

export function getCarsLoading(bool){
return {
type: 'GET_CARS_REQUEST',
isLoading: bool,
}
}

export function getCarsSuccess(carsObj) {
return {
type: 'GET_CARS_SUCCESS',
cars: carsObj,
};
}

export function getCars() {
return (dispatch) => {
dispatch(getCarsLoading(true));
return axios.get(API_ADDRESS + '/api/car')
.then((response) => {
if (!response.ok && !response.data) {
dispatch(getCarsFailure('oi, Noe gikk galt...'));
}
dispatch(getCarsSuccess(JSON.stringify(response.data)));
})
.catch(() => dispatch(getCarsFailure('oi, Noe gikk galt...!')));
};
}
45 changes: 37 additions & 8 deletions bpdashboard/src/components/cars/Cars.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,55 @@
import React from 'react';
import { push } from 'react-router-redux'
import { bindActionCreators } from 'redux'
// import { bindActionCreators } from 'redux'
import { getCars } from '../../actions/cars';
import { connect } from 'react-redux'
import './../../styles/login.css';
import './../../styles/cars.css';


class Cars extends React.Component {
async componentWillMount(){
//console.log(this.props.carFetched);
await this.props.getCars();
}

render(){
let carFetched = this.props.carFetched;
if (carFetched.length !== 0) {
carFetched = JSON.parse(this.props.carFetched)
}
return(
<div>
<p>Innlogging vellyket</p>
<div className="car-container">
<div className="car-intro">
<p>Registrerte biler</p>
</div>
<div className="cars-elements">
{carFetched.map((data, key) => (<div key= {key} className="car-box">
<div className="car-text"><b>RegNr</b>: {data.RegNr}</div>
<div className="car-text"><b>Model</b>: {data.Brand}</div>
</div>)
)}
</div>
</div>
)
}
}

const mapDispatchToProps = dispatch => bindActionCreators({
changePage: () => push('/')
}, dispatch)
const mapStateToProps = (state) => {
return {
user: state.auth.user,
hasErrored: state.auth.hasErrored,
carFetched: state.carsFetched.cars,
};
};

const mapDispatchToProps = dispatch => {
return {
changePage: () => push('/'),
getCars: () => dispatch(getCars()),
}
};

export default connect(
null,
mapStateToProps,
mapDispatchToProps
)(Cars)
25 changes: 25 additions & 0 deletions bpdashboard/src/reducers/cars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { GET_CARS_REQUEST, GET_CARS_SUCCESS, GET_CARS_FAILURE } from '../actions/cars';

function carsFetched(state = {cars: []}, action) {
switch (action.type) {
case GET_CARS_FAILURE:
return {
hasErrored: action.hasErrored,
cars: [],
};
case GET_CARS_REQUEST:
return {
hasErrored: '',
cars: [],
isLoading: action.isLoading,
};
case GET_CARS_SUCCESS:
return {
hasErrored: '',
cars: action.cars,
};
default:
return state;
}
}
export default carsFetched;
4 changes: 3 additions & 1 deletion bpdashboard/src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { combineReducers } from 'redux'
import { routerReducer } from 'react-router-redux'
import auth from './auth';
import carsFetched from './cars';

export default combineReducers({
routing: routerReducer,
auth
auth,
carsFetched
})
33 changes: 33 additions & 0 deletions bpdashboard/src/styles/cars.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.car-container{
display: flex;
flex-direction: column;
align-items: center;
}
.car-intro{
font-size: 50px;
color: white;
font-family: Roboto, sans-serif;
font-weight: bold;
margin-top: 20px;
}
.cars-elements{
width: 100vw;
height: 60vh;
display: flex;
flex-direction: row;
flex-flow: wrap;
justify-content: center;
align-items: center;
}
.car-box{
width: 250px;
height: 200px;
background-color: white;
display: flex;
justify-content: center;
flex-direction: column;
margin: 6px;
}
.car-text{
margin-left: 40px;
}
3 changes: 2 additions & 1 deletion bpdashboard/src/styles/index.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
body {
body, html {
margin: 0;
padding: 0;
font-family: sans-serif;
background-color: rgb(000,039,118);
height: 100vh;
}

0 comments on commit 37c436d

Please sign in to comment.