CafrmDocument.js 13.6 KB
Newer Older
syadziy's avatar
syadziy committed
1 2 3
import React, { Component } from 'react'
import { withStyles } from '@material-ui/core/styles';
import { Paper, Snackbar, Typography } from '@material-ui/core';
4 5 6 7 8 9
import MuiAlert from '@material-ui/lab/Alert';
import Constant from '../../library/Constant';
import Images from '../../assets/Images';
import ReactTooltip from 'react-tooltip';
import api from '../../api';
import PropagateLoader from "react-spinners/PropagateLoader"
syadziy's avatar
syadziy committed
10
import TableDocument from "./TableCafrmDocument";
11
import CreateCafrmDoc from "./CreateCafrmDocument";
12 13 14

const Alert = withStyles({})((props) => <MuiAlert elevation={6} variant="filled" {...props} />);

15
export default class CafrmDocument extends Component {
16 17 18 19 20 21 22 23 24 25

    constructor(props) {
        super(props);
        this.state = {
            alert: false,
            listData: [],
            id: 0,
            load: false,
            name: '',
            userCompActive: [],
26 27
            submenu_id : 0,
            submenu_name: "",
muhammad ridwan's avatar
muhammad ridwan committed
28
            menu_title: "",
syadziy's avatar
syadziy committed
29
            loading: false
30 31 32
        }
    }

syadziy's avatar
syadziy committed
33 34
    componentDidMount() {
        this.getMenuName();
35 36
    }

syadziy's avatar
syadziy committed
37 38
    getMenuName(){
        api.create().getMenuByUser().then(
39 40 41 42 43 44
            response => {
                if(response.data){
                    if(response.data.status === 'success'){
                        response.data.data.map(
                            (item) => {
                                let arr = window.location.href.split('/');
syadziy's avatar
syadziy committed
45
                                if (item.class_id == arr.slice(-1)[0]){
46 47 48 49
                                    this.setState({
                                        submenu_id : item.menu_id,
                                        submenu_name : item.menu_name.toLowerCase(),
                                        menu_title : item.menu_name.replace(/\b\w/g, c => c.toUpperCase())
syadziy's avatar
syadziy committed
50 51
                                    }, () => {
                                        this.getPermission(this.state.submenu_name)
syadziy's avatar
syadziy committed
52
                                        // this.getDataCarfm();
syadziy's avatar
syadziy committed
53
                                        this.getDetailUser();
54 55 56 57 58 59 60 61 62 63
                                    })
                                }
                            }
                        )
                    }
                }
            }
        )
    }

syadziy's avatar
syadziy committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    // getDataCarfm(create){
    //     let payload = {
    //         submenu_id : this.state.submenu_id
    //     }
    //     api.create().getCarfmDocumentBySubmenu(payload).then(
    //         response => {
    //             if(response){
    //                 if(response.data.status === 'success'){
    //                     let dataList = []
    //                     response.data.data.map((item, index) => {
    //                         dataList.push(item)
    //                     })

    //                     this.setState({
    //                         listData: dataList,
    //                         refresh: create == undefined ? '' : create,
    //                         loading: false
    //                     })
    //                 } else {
    //                     this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning', loading: false }, () => {
    //                         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.problem, tipeAlert: 'error', loading: false })
    //             }
    //         }
    //     )
    // }
98

syadziy's avatar
syadziy committed
99
    getPermission(subMenu) {
100 101 102
        let payload = {
            menu: subMenu
        }
syadziy's avatar
syadziy committed
103
        api.create().getPermission(payload).then(response => {
104
            if (response.data) {
muhammad ridwan's avatar
muhammad ridwan committed
105
                console.log(`status get permission ${response.data.status}`);
106 107 108 109 110
                if (response.data.status === "success") {
                    this.setState({
                        btncreate: response.data.data.create,
                        btnview: response.data.data.view,
                        btndownload: response.data.data.download,
muhammad ridwan's avatar
muhammad ridwan committed
111
                        load: true,
112 113 114 115 116 117 118 119 120 121 122 123 124
                    })
                    console.log("btn create " + this.state.btncreate);
                } else {
                    this.setState({
                        load: true
                    })
                }
            } else {
                this.setState({ load: true })
            }
        })
    }

syadziy's avatar
syadziy committed
125
    getDetailUser() {
126
        let userId = localStorage.getItem(Constant.USER)
syadziy's avatar
syadziy committed
127
        api.create().getDetailUser(userId).then((response) => {
128 129 130
            if (response.data) {
                if (response.ok) {
                    if (response.data.status === 'success') {
syadziy's avatar
syadziy committed
131
                        this.setState({ userCompany: response.data.data.company, visibleTrue: false }, () => {
132 133 134 135 136 137 138 139 140
                            this.getCompanyActive()
                            console.log(this.state.userCompany);
                        })
                    }
                }
            }
        })
    }

syadziy's avatar
syadziy committed
141 142
    getCompanyActive() {
        api.create().getPerusahaanActive().then((response) => {
143
            console.log("getcompanyactive start");
muhammad ridwan's avatar
muhammad ridwan committed
144
            console.log(response.data.data);
145 146 147 148 149 150 151
            console.log("getcompanyactive stop");
            if (response.data) {
                if (response.data.status === 'success') {
                    let data = response.data.data
                    let compActive = []
                    let userCompActive = []
                    this.state.userCompany.map((item, index) => {
muhammad ridwan's avatar
muhammad ridwan committed
152
                        if (index !== -1 && String(data[index].status).toLocaleLowerCase() == 'active'){
153 154 155 156 157 158 159 160 161 162 163 164 165 166
                            userCompActive.push(item)
                        }
                    })
                    data.map(( item, index ) => {
                        if (String(item.status).toLocaleLowerCase() == 'active') {
                            compActive.push(item)
                        }
                    })
                    if (compActive.length == userCompActive.length){
                        this.setState({ btnadd: true})
                    } else {
                        this.setState({ btnadd: false})
                    }
                    this.setState({ userCompActive, visibleTrue: true })
muhammad ridwan's avatar
muhammad ridwan committed
167 168
                    console.log("usercompactive")
                     console.log(userCompActive)
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
                } else {
                    this.setState({ alert: true, messageAlert: response.data.message, tipeAlert: 'warning', visibleTrue: true }, () => {
                        if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
                            setTimeout(() => {
                                localStorage.removeItem(Constant.TOKEN)
                                window.location.reload();
                            }, 1000);
                        }
                    })
                }
            } else {
                console.log("error di getcompany active")
                this.setState({ alert: true, messageAlert: response.problem, tipeAlert: 'error', listCompany: null, company: null, visibleTrue: true })
            }
        })
    }

syadziy's avatar
syadziy committed
186
    createDocument(payload) { //upload apinya disini
syadziy's avatar
syadziy committed
187
        this.setState({ loading: true, visibleTrue: false })
syadziy's avatar
syadziy committed
188
        api.create().uploadCarfmDocument(payload).then(response => {
189 190 191
            // console.log(response)
            if (response.data) {
                if (response.ok) {
192
                    if (response.data.message === "Data was Saved Successfully") {
syadziy's avatar
syadziy committed
193 194 195
                        this.setState({ visibleCreate: false, visibleTrue: true, loading: false, alert: true, messageAlert: response.data.message, tipeAlert: 'success'}, () => {
                            // window.location.reload();
                        })
syadziy's avatar
syadziy committed
196
                        // this.getDataCarfm('create')
197 198
                    }
                    else {
syadziy's avatar
syadziy committed
199
                        this.setState({ visibleCreate: false, visibleTrue: true, alert: true, messageAlert: response.data.message, tipeAlert: 'warning', loading: false }, () => {
200 201 202 203 204 205 206 207 208
                            if (response.data.message.includes("Someone Logged In") || response.data.message.includes("Token Expired")) {
                                setTimeout(() => {
                                    localStorage.removeItem(Constant.TOKEN)
                                    window.location.reload();
                                }, 1000);
                            }
                        })
                    }
                } else {
syadziy's avatar
syadziy committed
209
                    this.setState({ visibleCreate: false, visibleTrue: true, alert: true, messageAlert: response.data.message, tipeAlert: 'error', loading: false })
210 211
                }
            } else {
syadziy's avatar
syadziy committed
212
                this.setState({ visibleCreate: false, visibleTrue: true, alert: true, messageAlert: response.problem, tipeAlert: 'error', loading: false })
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
            }
        })
    }


    closeAlert() {
        this.setState({alert: false})
    }

    handleLoading(tipe) {
        this.setState({loading: tipe == 0 ? false : true})
    }

    render() {
        const loadingComponent = (
            <div style={{
                position: 'absolute',
                zIndex: 1500,
                top: 0,
                left: 0,
                width: '100%',
                height: '100%',
                display: 'flex',
                justifyContent: 'center',
                alignItems: 'center',
                background: 'rgba(255,255,255,0.8)'
            }}>
                <PropagateLoader
                    // css={override}
                    size={20}
                    color={"#274B80"}
                    loading={this.state.loading}
                />
            </div>
        );
syadziy's avatar
syadziy committed
248

249 250 251 252 253 254 255 256
        return (
            <div style={{height: this.props.height, backgroundColor: '#f8f8f8'}}>
                <Snackbar open={this.state.alert} autoHideDuration={6000} onClose={() => this.closeAlert()}>
                    <Alert onClose={() => this.closeAlert()} severity={this.state.tipeAlert}>
                        {this.state.messageAlert}
                    </Alert>
                </Snackbar>

syadziy's avatar
syadziy committed
257 258
                <div className={"main-color"} style={{ height: 78, display: 'flex', alignItems: 'center', paddingLeft: 20 }}>
                    <Typography style={{ fontSize: '16px', color: 'white' }}>{this.state.menu_title}</Typography>
259 260 261
                </div>

                <div style={{padding: 20}}>
syadziy's avatar
syadziy committed
262
                    {this.state.loading && loadingComponent}
syadziy's avatar
syadziy committed
263
                    {this.state.btnview && (
muhammad ridwan's avatar
muhammad ridwan committed
264
                        <Paper style={{padding: 20}}>
syadziy's avatar
syadziy committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
                            <div style={{display: 'flex', justifyContent: 'flex-end'}}>
                                {/* {console.log("renderbtn" +  this.state.btncreate)} */}
                                {(this.state.btncreate) && (
                                    <span>
                                        <a data-tip={'Add'} data-for="add">
                                            <div style={{display: 'grid', justifyContent: 'flex-end'}}>
                                                <button
                                                    type="button"
                                                    onClick={() => this.setState({visibleCreate: true, refresh: ''})}
                                                    style={{marginRight: 25, background: 'transparent'}}
                                                >
                                                    <img src={Images.add}/>
                                                </button>
                                            </div>
                                        </a>
                                        <ReactTooltip border={true} id="add" place="bottom" type="light" effect="solid"/>
                                    </span>
                                )}
                            </div>

                            <div style={{display: 'flex'}}>
                                {this.state.visibleTrue && (
                                <TableDocument
                                    data={this.state.listData}
                                    refresh={this.state.refresh}
                                    load={this.state.load}
                                    userCompActive={this.state.userCompActive}
                                    handleLoading={this.handleLoading.bind(this)}
                                    name={this.state.name}
                                    submenu_id={this.state.submenu_id}
                                    btndownload = {this.state.btndownload}
                                />)}

                            </div>
299 300 301
                        </Paper>
                    )}
                </div>
syadziy's avatar
syadziy committed
302

syadziy's avatar
syadziy committed
303
                {this.state.visibleCreate && (
304
                    <CreateCafrmDoc
305 306 307 308 309
                        onClickClose={() => this.setState({ visibleCreate: false })}
                        // getDataDocument={this.getDataDocument.bind(this)}
                        createDocument={this.createDocument.bind(this)}
                        handleLoading={this.handleLoading.bind(this)}
                        submenu_id={this.state.submenu_id}
syadziy's avatar
syadziy committed
310
                        menuName={this.state.menu_title}
311
                    />
syadziy's avatar
syadziy committed
312
                )}
313 314 315 316 317 318 319

            </div>
        );
    }
}