import React, { Component } from 'react';
import { Typography, AppBar, Tabs, Tab, TextField, Snackbar, Collapse, withStyles, Checkbox, InputAdornment, IconButton } from '@material-ui/core';
import Visibility from '@material-ui/icons/Visibility';
import VisibilityOff from '@material-ui/icons/VisibilityOff';
import MuiAlert from '@material-ui/lab/Alert';
import Images from '../../assets/Images';
import api from '../../api';
import Constant from '../../library/Constant';
import RemoveIcon from '@material-ui/icons/Remove';
import AddIcon from '@material-ui/icons/Add';
import { titleCase } from '../../library/Utils';
import * as R from 'ramda'
import ImageUploader from 'react-images-upload';
const CustomCheckbox = withStyles({
root: {
color: '#51c6ea',
'&$checked': {
color: '#51c6ea',
},
},
checked: {},
})((props) => );
const Alert = withStyles({
})((props) => );
export default class Profile extends Component {
constructor(props) {
super(props)
this.state = {
tab: 0,
oldPassword: '',
password: '',
confirmPassword: '',
errorOldPassword: false,
errorPassword: false,
errorConfirmPassword: false,
msgOldPassword: 'Consists of 8 Characters with a Combination of Numbers.',
msgPassword: 'Consists of 8 Characters with a Combination of Numbers.',
msgConfirmPassword: 'Consists of 8 Characters with a Combination of Numbers.',
company: [],
listCompany: [],
selectedIndex: 0,
showPass: false,
showPass2: false,
showPass3: false,
pictures: [],
name: "",
alert: false,
tipeAlert: '',
messageAlert: ''
}
this.onDrop = this.onDrop.bind(this);
}
componentDidMount() {
this.getPerusahaan()
this.getUser()
}
selectTab = (event, newEvent) => {
this.setState({ tab: newEvent })
}
handleChange(e) {
let data = this.state
this.setState({ ...data, [e.target.name]: e.target.value })
// console.log(e.target.name);
if (e.target.name === "password") {
this.setState({ errorPassword: false, msgPassword: 'Consists of 8 Characters with a Combination of Numbers.' })
} else if (e.target.name === "confirmPassword") {
this.setState({ errorConfirmPassword: false, msgConfirmPassword: 'Consists of 8 Characters with a Combination of Numbers.' })
} else if (e.target.name === "oldPassword") {
this.setState({ errorOldPassword: false, msgOldPassword: 'Consists of 8 Characters with a Combination of Numbers.' })
}
}
validasi() {
if (this.state.oldPassword === "") {
this.setState({ errorOldPassword: true, msgOldPassword: 'Old Password Cannot be Empty.' })
} else if (this.state.oldPassword.length < 8) {
this.setState({ errorOldPassword: true, msgOldPassword: 'Old Password Minimum 8 Characters.' })
} else if (this.isEmail(this.state.oldPassword)) {
this.setState({ errorOldPassword: true, msgOldPassword: 'Old Password Format Should Not Use Email.' })
} else if (!this.isRegex(this.state.oldPassword)) {
this.setState({ errorOldPassword: true, msgOldPassword: 'Old Password Must be a Combination of Characters, Letters and Numbers.' })
} else if (this.state.password.trim() === "") {
this.setState({ errorPassword: true, msgPassword: 'Password Cannot be Empty.' })
} else if (this.state.password.length < 8) {
this.setState({ errorPassword: true, msgPassword: 'Password Minimum 8 Characters.' })
} else if (this.isEmail(this.state.password)) {
this.setState({ errorPassword: true, msgPassword: 'Password Format Should Not Use Email.' })
} else if (!this.isRegex(this.state.password)) {
this.setState({ errorPassword: true, msgPassword: 'Password Must be a Combination of Characters, Letters and Numbers.' })
} else if (this.state.confirmPassword.trim() === "") {
this.setState({ errorConfirmPassword: true, msgConfirmPassword: 'Repeat Password Cannot be Empty.' })
} else if (this.state.password !== this.state.confirmPassword) {
this.setState({ errorConfirmPassword: true, msgConfirmPassword: 'Password Does Not Match.' })
} else {
this.confirmPassword()
}
}
confirmPassword() {
let body = {
"old_password": this.state.oldPassword,
"new_password": this.state.password,
"confirm_password": this.state.confirmPassword
}
api.create().changePassword(body).then(response => {
// console.log(response);
if (response.data) {
if (response.ok) {
if (response.data.status === "success") {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'success' })
this.setState({ oldPassword: "", password: "", confirmPassword: "" })
localStorage.removeItem(Constant.TOKEN)
setTimeout(() => {
window.location.reload();
}, 1000);
} else if (response.data.message === "Old Password is Not Correct") {
this.setState({ errorOldPassword: true, msgOldPassword: 'Incorrect password.' })
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
}
})
}
isRegex(value) {
// const re = /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/;
const re = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{1,}$/;
return re.test(String(value));
// return(value)
}
isEmail(email) {
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
getUser() {
let userId = localStorage.getItem(Constant.USER)
api.create().getDetailUser(userId).then((response) => {
// console.log(response);
if (response.data) {
if (response.ok) {
if (response.data.status === 'success') {
this.setState({ company: response.data.data.company, photo: response.data.data.photo, name: response.data.data.fullname })
// console.log(response)
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
}
})
}
getPerusahaan() {
api.create().getPerusahaanHierarki().then((response) => {
// console.log(response);
if (response.data) {
if (response.ok) {
if (response.data.status === 'success') {
this.setState({ listCompany: response.data.data })
// console.log(response.data.data)
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
}
})
}
handleItemChecked(item) {
let indexID = this.state.company.findIndex((val) => val === item.id)
return indexID === -1 ? false : true
}
renderChildren = (item, pad) => {
let padding = null
if (pad !== undefined) {
padding = pad
} else {
padding = 20
}
return (
{item.children.length > 0 && (
{item.children.map((data, index) => {
return (
// -
{R.isNil(data.children) ?
null
:
data.children.length < 1 ?
null
:
this.handleCollapse(data)} style={{ marginLeft: 7, marginRight: 2 }}>
{data.collapse ? : }
}
null}
/>
{titleCase(data.company_name)}
{!R.isNil(data.children) && this.renderChildren(data, padding + 20)}
//
)
})}
)}
)
}
handleCollapse(item) {
let path = this.searchIt({ children: this.state.listCompany }, item.id)
let listCompany = this.state.listCompany
let arrayPath = []
if (path.length > 1) {
arrayPath = path.split('-');
arrayPath = arrayPath.map((item) => { return item })
} else {
arrayPath.push(path)
}
let pathSelect = null
if (arrayPath.length === 1) {
pathSelect = listCompany[arrayPath[0]]
} else if (arrayPath.length === 2) {
pathSelect = listCompany[arrayPath[0]].children[arrayPath[1]]
} else if (arrayPath.length === 3) {
pathSelect = listCompany[arrayPath[0]].children[arrayPath[1]].children[arrayPath[2]]
} else if (arrayPath.length === 4) {
pathSelect = listCompany[arrayPath[0]].children[arrayPath[1]].children[arrayPath[2]].children[arrayPath[3]]
} else if (arrayPath.length === 5) {
pathSelect = listCompany[arrayPath[0]].children[arrayPath[1]].children[arrayPath[2]].children[arrayPath[3]].children[arrayPath[4]]
} else if (arrayPath.length === 6) {
pathSelect = listCompany[arrayPath[0]].children[arrayPath[1]].children[arrayPath[2]].children[arrayPath[3]].children[arrayPath[4]].children[arrayPath[5]]
} else if (arrayPath.length === 7) {
pathSelect = listCompany[arrayPath[0]].children[arrayPath[1]].children[arrayPath[2]].children[arrayPath[3]].children[arrayPath[4]].children[arrayPath[5]].children[arrayPath[6]]
}
pathSelect.collapse = !pathSelect.collapse
// console.log(pathSelect.collapse)
this.setState({ listCompany }, () => console.log(pathSelect))
}
searchIt = (node, search, path = '', position = 0) => {
if (node.id && node.id === search) { return path !== '' ? `${path}-${position}` : position; }
if (!node.children) { return false }
const index = node.children.findIndex((x) => x.id && x.id === search);
if (index >= 0) {
return path !== '' ? `${path}-${index}` : index;
}
for (let i = 0; i < node.children.length; i++) {
const result = this.searchIt(node.children[i], search, path !== '' ? `${path}-${i}` : i, i);
if (result) {
return result;
}
}
return false;
};
async onDrop(pictureFiles) {
console.log(pictureFiles);
this.setState({ pictures: pictureFiles })
// console.log(response);
// console.log(pictureFiles);
// console.log(pictureDataURLs);
// this.setState({
// pictures: this.state.pictures.concat(pictureFiles),
// });
}
async uploadFoto() {
let formData = new FormData()
formData.append('file', this.state.pictures[0])
api.create().uploadFoto(formData).then(response => {
// console.log(response)
if (response.data) {
if (response.ok) {
if (response.data.status === "success") {
this.setState({ uploadVisible: false }, () => {
this.getUser()
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'success' }, () => {
setTimeout(() => {
window.location.reload()
}, 1000);
})
})
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning' }, () => {
if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
setTimeout(() => {
localStorage.removeItem(Constant.TOKEN)
window.location.reload();
}, 1000);
}
})
}
} else {
this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'error' })
}
} else {
this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error' })
}
})
}
closeAlert() {
this.setState({ alert: false })
}
render() {
return (
this.closeAlert()}>
this.closeAlert()} severity={this.state.tipeAlert}>
{this.state.messageAlert}
{this.state.tab === 0 ?
:
Authorization
{/*
Agro PersadaTriputra
Dharma Group
Dharma Polimetal
Dharma Poliplast
*/}
{this.state.listCompany.map((item, index) => {
return (
{/*
- */}
{item.children.length > 0 &&
this.handleCollapse(item)} style={{ marginLeft: 7, marginRight: 2 }}>
{item.collapse ? : }
}
null}
/>
{titleCase(item.company_name)}
{!R.isNil(item.children) && this.renderChildren(item)}
{/*
*/}
)
})}
}
{this.state.uploadVisible && (
0 ? { display: 'none' } : null}
buttonText={'Select Picture'}
onChange={this.onDrop}
imgExtension={['.jpg', '.gif', '.png', '.gif', '.jpeg']}
maxFileSize={1000000}
label={"Max file size: 1 Mb, accepted: jpg or png"}
/>
{this.state.pictures.length > 0 ?
this.uploadFoto()}>Upload
: null
}
)}
);
}
}