Skip to content
Snippets Groups Projects
Commit 66e8c52e authored by Ismail's avatar Ismail
Browse files

Shift Management fixed and updated.

parent 4e809214
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,7 @@ import { connect } from 'react-redux'; ...@@ -6,6 +6,7 @@ import { connect } from 'react-redux';
import { Redirect } from 'react-router-dom'; import { Redirect } from 'react-router-dom';
import { firestoreConnect } from 'react-redux-firebase'; import { firestoreConnect } from 'react-redux-firebase';
import { compose } from 'redux'; import { compose } from 'redux';
import moment from 'moment';
class Dashboard extends Component { class Dashboard extends Component {
render() { render() {
...@@ -16,11 +17,17 @@ class Dashboard extends Component { ...@@ -16,11 +17,17 @@ class Dashboard extends Component {
}) })
) : null ) : null
const todayList = list && list.length ? (
list.filter(l => {
return l.Date === moment().format('MM/DD/YYYY')
})
) : null
if( !auth.uid ) return <Redirect to='/signin' /> if( !auth.uid ) return <Redirect to='/signin' />
return ( return (
<div id="content"> <div id="content">
<Navbar profile = { profile } /> <Navbar profile = { profile } />
<ManageShift /> <ManageShift uid={ auth.uid } todayList={ todayList } />
<ShiftReport workList={ list } schedule={ schedule } /> <ShiftReport workList={ list } schedule={ schedule } />
</div> </div>
) )
......
import React, { Component } from 'react' import React, { Component } from 'react';
import moment from 'moment';
import { connect } from 'react-redux';
import { createWork, updateWork } from '../../store/actions/shiftAction';
class ManageShift extends Component { class ManageShift extends Component {
state = {
date: null,
empId: this.props.uid,
startShift: '',
startBreak: '',
endBreak: '',
endShift: '',
stateSet: false
}
componentDidMount() {
//console.log(this.props.todayList)
}
UNSAFE_componentWillReceiveProps() {
if (this.props.todayList && !this.state.stateSet) {
this.setState(this.props.todayList[0], () => {
this.setState({stateSet:true})
//console.log(this.state)
})
}
}
handleCreatEvent = (e) => {
const r = window.confirm("Do you really want to " + e.target.id + "?"); if (r == true) {
this.setState({
[e.target.id]: moment().format('MM/DD/YYYY HH:mm:ss')
}, () => {
this.props.createWork(this.state);
})
}
}
handleUpdateEvent = (e) => {
const r = window.confirm("Do you really want to " + e.target.id + "?"); if (r == true) {
this.setState({
[e.target.id]: moment().format('MM/DD/YYYY HH:mm:ss'),
}, () => {
this.props.updateWork(this.state);
})
}
}
render() { render() {
return ( return (
<div className="container"> <div className="row">
<div className="row"> <div className="col-3 text-center">
<div className="col-3 text-center"><button onClick={this.handleCreatEvent} id="startWorkTime" type="button" className="btn btn-success btn-sm btn-special">Start Work</button></div> <button
<div className="col-3 text-center"><button disabled type="button" className="btn btn-success btn-sm btn-special">Start Lunch Break</button></div> onClick={this.handleCreatEvent}
<div className="col-3 text-center"><button disabled type="button" className="btn btn-success btn-sm btn-special">Lunch Break Finish</button></div> disabled={this.state.startShift + "" != ""}
<div className="col-3 text-center"><button disabled type="button" className="btn btn-success btn-sm btn-special">Close Work</button></div> id="startShift"
type="button"
className="btn btn-success btn-sm btn-special">
Start Work
</button>
</div>
<div className="col-3 text-center">
<button
type="button"
className="btn btn-success btn-sm btn-special"
disabled={this.state.startShift + "" == "" || this.state.startBreak + "" != ""}
onClick={this.handleUpdateEvent}
id="startBreak">
Start Lunch Break
</button>
</div>
<div className="col-3 text-center">
<button
disabled={this.state.startShift + "" == "" || this.state.startBreak + "" == "" || this.state.endBreak + "" != ""}
type="button"
className="btn btn-success btn-sm btn-special"
onClick={this.handleUpdateEvent}
id="endBreak">
Lunch Break Finish
{this.state.endBreak == ""}
</button>
</div>
<div className="col-3 text-center">
<button
disabled={this.state.startShift + "" == "" || this.state.startBreak + "" == "" || this.state.endBreak + "" == "" || this.state.endShift + "" != ""}
type="button"
onClick={this.handleUpdateEvent}
className="btn btn-success btn-sm btn-special"
id="endShift">
Close Work
{this.state.endShift == ""}
</button>
</div> </div>
</div> </div>
) );
}
}
const mapStateToProps = (dispatc) => {
return {
createWork: (work) => dispatc(createWork(work)),
updateWork: (work) => dispatc(updateWork(work))
} }
} }
export default ManageShift export default connect(null, mapStateToProps)(ManageShift);
import moment from 'moment';
export const createWork = (shift) => {
return (dispatch, getState, { getFirebase, getFirestore }) => {
const firestore = getFirestore();
const authuId = getState().firebase.auth.uid;
firestore.collection('Shifts').add({
...shift,
empId: authuId,
date: moment().format('MM/DD/YYYY')
}).then(() => {
dispatch({ type: 'ADD_SHIFT', shift })
}).catch((err) => {
dispatch({ type: 'ADD_SHIFT_ERROR', err });
})
}
}
export const updateWork = (shift) => {
return (dispatch, getState, { getFirebase, getFirestore }) => {
const firestore = getFirestore();
firestore.collection("Shifts")
.where("empId", "==", shift.empId)
.get()
.then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
let data = doc.data();
if (new Date(data.date).toLocaleDateString() == new Date().toLocaleDateString()) {
shift.date = data.date;
firestore.collection('Shifts').doc(doc.id).update(shift)
.then(() => {
dispatch({ type: 'UPDATE_SHIFT', shift })
}).catch((err) => {
dispatch({ type: 'UPDATE_SHIFT_ERROR', err });
})
}
})
})
}
}
\ No newline at end of file
const initState = {} const initState = {}
const shiftReducer = (state = initState, action) => { const shiftReducer = (state = initState, action) => {
return state switch (action.type) {
case 'ADD_SHIFT':
console.log('New shift added', action.shift);
return state;
case 'ADD_SHIFT_ERROR':
console.log('New shift error', action.shift);
return state;
case 'UPDATE_SHIFT':
console.log('update shift', action.shift);
return state;
case 'UPDATE_SHIFT_ERROR':
console.log('update shift error', action.shift);
return state;
default:
return state;
}
} }
export default shiftReducer export default shiftReducer
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment