// LIBRARIES
import React, { Component } from 'react';
import { Menu, Dropdown, Row, Col, Modal, Radio,DatePicker, notification, Button, Select, Input } from "antd"
import { Formik , Field} from 'formik'
import { connect } from "react-redux"
import { customAction } from "actions";
import { locationDetailsSchema } from '../validationSchema'
import AddUserManagementForm from '../components/stationForm'
import {API_UNI_OIL, API_PUT_BRANCH} from 'utils/StationApi'
import moment from 'moment'
import { DeleteFilled } from '@ant-design/icons';
import * as Yup from 'yup'
import { secretbox } from '../../../../../../node_modules/tweetnacl/nacl';
const styles = {
':select': {
backgroundColor: 'yellow',
color: 'red'
}
};
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 4 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 18 },
},
};
class EditStation extends Component {
state= {
updating: false,
isGenerated: false,
loading: false,
latitude: '',
longitude: '',
urlMap: '',
name: '',
location: '',
contactNum: '',
branchId: '',
fuels: [],
isSched: 2,
date: null,
latLong: {},
fuelModal: false,
newFuel: [],
fuelsData: [],
value: [],
removeModal: false,
fuelName: '',
fuelIndex: '',
stationCode: '',
selectMenu: false,
}
ref;
async componentDidMount(){
const { match } = this.props;
try {
let response = await API_UNI_OIL.get(`stations/${match.params.id}`)
console.log('Data Fetched',response.data)
let data = {lat: parseFloat(response.data.lat), lng: parseFloat(response.data.long)}
this.setState({
name: response.data.name,
location: response.data.location,
contactNum: response.data.contact_number,
branchId: response.data.BranchId,
urlMap: response.data.map_Url,
latitude: response.data.lat,
longitude: response.data.long,
fuels: this.sortFuels(response.data.StationFuels),
mounted: true,
latLong: data,
date: response.data.Schedule,
stationCode: response.data.code,
isSched: response.data.Schedule == null ? 2 : 1
})
} catch ({response: error}) {
notification.error({
message: "Error",
description:
Something went wrong loading data.
- { error && error.data && error.data.message }
,
duration: 3,
});
if(error.status == 404) {
if(this.props.location.pathname)
this.props.history.push(`${this.props.location.pathname}/404`); return
}
this.setState({ mounted: false })
}
this.getFuels()
}
getFuels = async() => {
try {
const response = await API_UNI_OIL.get('fuels', '');
if(response) {
this.setState({ newFuel: response.data.data, fuelsData: response.data.data });
let data = this.state.fuels
data.map(fuel => {
let newArray = this.state.newFuel.filter(function(element) {
return element.id !== fuel.FuelId;
});
let allFuels = newArray.sort((a, b) => {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
})
this.setState({newFuel: allFuels})
})
}
} catch (error) {
console.log(error)
}
}
handleSubmit = async (data, actions) => {
const { setErrors, setSubmitting } = actions;
let { history, match } = this.props;
let _self = this;
const result = this.state.fuels.map(({ FuelId, price }) => ({FuelId: FuelId, price}));
let id = match.params.id
let user = localStorage.getItem('user')
let values = {
'name': data.name,
'location': data.location,
'map_Url': data.map_URL,
'lat': data.lat,
'long':data.long,
'contact_number': data.contact_number.toString(),
'modified_by': user,
'BranchId': data.BranchId.toString(),
'Schedule': this.state.isSched === 1 ? this.state.date : this.state.isSched === 0 ? null : null,
'NewStationFuel': result,
'code': data.code.toString()
}
let inv = false;
this.state.fuels.map((value) => {
if(value.price == 0 || value.price == 0.00 || value.price == "0.00" || value.price == "00.00" || value.id == undefined){
inv = true
}
})
if(inv){
notification.error({
message: "Error",
description: ,
duration: 3,
});
return
}
if(this.state.fuels == 0 && this.state.isSched == 1){
notification.error({
message: "Error",
description:
Can't schedule a station without Fuel.
,
duration: 3,
});
return;
}
if(this.state.isSched == 2) {
notification.error({
message: "Error",
description:
Please select a schedule update value.
,
duration: 3,
});
}
else {
this.props.customAction({
type: "STATION_UPDATE_REQUEST",
payload: {
id,
values,
setSubmitting,
setErrors,
history,
_self
}
});
}
console.log('Data Submitted', values)
}
getMapdata = (url, lat, long) => {
this.setState({urlMap: url, latitude: lat, longitude: long})
}
updateFuels = (e, id, bool) => {
if(bool){
const newState = this.state.fuels.map(obj =>
obj.FuelId === id ? { ...obj, price: e, FuelId: id } : obj
);
this.setState({fuels: newState})
}else{
const newState = this.state.fuels.map(obj =>
obj.id === id ? { ...obj, price: e, FuelId: id } : obj
);
this.setState({fuels: newState})
}
}
handleChangeRadio = (e) => {
this.setState({isSched: e.target.value})
}
onDateChange = (date, dateString) => {
console.log('date', dateString)
this.setState({date: dateString})
}
getAddedFuel = (select, data) => {
console.log(select, data)
const obj = [];
const result = this.state.fuelsData.filter(({id}) => data.includes(id),{...obj, FuelId: data});
this.setState({value: result, selectMenu: false});
select.blur()
}
addToStationFuel = () => {
let data = this.state.fuels
let val = this.state.value
let nf = this.state.newFuel
let arr1 = val.filter(id => !data.includes(id));
let sa = data.concat(arr1)
let arr2 = nf.filter(id => !val.includes(id))
this.setState({fuelModal: false, fuels: sa, newFuel:arr2, selectMenu:false, })
}
handleFuelModal = () => {
const Option = Select.Option;
return(
{
this.addToStationFuel()
this.ref.rcSelect.fireChange([])
}}>
Add fuel
,
]}
>
)
}
handleShowFuelAdd = () => {
this.setState({fuelModal: !this.state.fuelModal, selectMenu:false})
}
handleShowConfirmationModal = () => {
return(
this.removeFuel()}>
Remove
,
]}
>
Are you sure you want to remove {this.state.fuelName} ?
)
}
removeFuel = () => {
let data = this.state.fuels
data.splice(this.state.fuelIndex, 1)
console.log(data)
this.setState({fuels: data, removeModal: false})
}
disabledDate(current) {
// Can not select days before today and today
return current && current < moment().startOf('day');
}
onOk(value) {
console.log('onOk: ', value);
}
sortFuels(data){
let tosort = [];
for(var x=0;x {
return a.val - b.val
})
let fts = ttss.map((item, i) => {
return item.data
})
return fts
}
render() {
const { match, history } = this.props;
let { isGenerated,loading } = this.state;
return (
(this.form = node)}
enableReinitialize={true}
validationSchema={locationDetailsSchema}
onSubmit={this.handleSubmit }
render = {(props)=>
}
/>
{
Scheduled Update:
this.handleChangeRadio(e)}>
No
Yes
{
this.state.isSched == true ?
Scheduled Date and Time:
:
''
}
}
{
this.state.fuels.map((item,index)=>{
let name = item.Fuel ? item.Fuel.name : item.name
return (
{name}
this.setState({fuelIndex:index, fuelName: name, removeModal: !this.state.removeModal})}/>}
type="number"
value={item.FuelId != 'null' || item.FuelId != '' ? item.price == "0.00" ? "0" + item.price : item.price : 0}
label={name}
placeholder={name}
onChange={(e)=> this.updateFuels(e.target.value, item.Fuel ? item.FuelId : item.id, item.Fuel ? true : false)}
/>
)
})
}
{this.handleFuelModal()}
{this.handleShowConfirmationModal()}
);
}
}
EditStation = connect(
state => ({
//userInfo: state
stationUpdate: state.stationUpdate,
}),
{ customAction }
)(EditStation);
export default EditStation;