Create.js 10.2 KB
Newer Older
Giang Tran committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
import {
    Button,
    Checkbox,
    FormControlLabel,
    Grid,
    Typography,
} from '@material-ui/core'
import { dropdownPermission } from 'app/apis/Functions/dropdown'
import { createGroup } from 'app/apis/Functions/GroupRole'
import { Breadcrumb, SimpleCard } from 'app/components'
import { trimObject } from 'app/config/Function'
import useAuth from 'app/hooks/useAuth'
import { hideLoading, showLoading } from 'app/redux/actions/loadingAction'
import React, { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { TextValidator, ValidatorForm } from 'react-material-ui-form-validator'
import { connect } from 'react-redux'
import { Link, useHistory } from 'react-router-dom'
import { toast } from 'react-toastify'

function CreateDeparment(props) {
    const { user } = useAuth()

    const history = useHistory()
    const [state, setState] = useState({})
    const [listMethod, setListMethod] = useState([])
    const [listChecked, setListChecked] = useState([])

    const { t } = useTranslation()
    useEffect(() => {
        getData()
    }, [])

    const getData = async () => {
        const res = await dropdownPermission()
        if (res.data.code == 200 && res.data.data && res.data.data.data) {
            setListMethod(res.data.data.data)
        }
    }

    const handleSubmit = async (value) => {
        if (state) {
            const newValue = trimObject(state)
            const newList = listChecked.map((e) => {
                return { action_id: e }
            })
            props.showLoading()
            const res = await createGroup({
                ...newValue,
                userGroupPermissions: newList,
                status: 1,
            })
            props.hideLoading()

            if (res.data.code == 200) {
                history.push('/group-role')
                toast.success('Thêm mới thành công!', {
                    theme: 'colored',
                })
            } else {
                toast.error(t(res.data.error), {
                    theme: 'colored',
                })
            }
        }
    }

    const handleChange = (event) => {
        event.persist()
        setState({
            ...state,
            [event.target.name]: event.target.value,
        })
    }

    const onPicker = (value) => {
        if (value.target.checked) {
            const newList = listChecked.concat(value.target.name)
            setListChecked(newList)
        } else {
            const newList = listChecked.filter((e) => {
                return e != value.target.name
            })
            setListChecked(newList)
        }
    }

    const { name, description } = state

    return (
        <div className="m-sm-30">
            <div className="mb-sm-30">
                <div className="mb-sm-30">
                    <Breadcrumb
                        routeSegments={[
                            {
                                name: 'Quản lý nhóm',
                                path: '/group-role',
                            },
                            { name: 'Thêm mới' },
                        ]}
                    />
                </div>

                <SimpleCard>
                    <ValidatorForm onSubmit={handleSubmit} onError={() => null}>
                        <Grid container spacing={3}>
                            <Grid item lg={6} md={6} sm={12} xs={12}>
                                <TextValidator
                                    className="mb-4 w-full"
                                    label="Tên nhóm *"
                                    onChange={handleChange}
                                    variant="outlined"
                                    type="text"
                                    name="name"
                                    value={name || ''}
                                    validators={['required']}
                                    errorMessages={[
                                        'Không được để trống trường này',
                                    ]}
                                />
                            </Grid>
                            <Grid item lg={6} md={6} sm={12} xs={12}>
                                <TextValidator
                                    className="mb-4 w-full"
                                    label="Mô tả *"
                                    onChange={handleChange}
                                    variant="outlined"
                                    type="text"
                                    name="description"
                                    value={description || ''}
                                    validators={['required']}
                                    errorMessages={[
                                        'Không được để trống trường này',
                                    ]}
                                />
                            </Grid>
                            <Grid item xs={12} sm={12}>
                                <Typography variant={'h6'}>
                                    Phân quyn
                                </Typography>
                                {listMethod.length > 0 ? (
                                    <Grid lg={12} md={12} sm={12} xs={12} item>
                                        {listMethod.map((items) => {
                                            const { name, list_actions } = items
                                            return (
                                                <>
                                                    <Typography variant={'h6'}>
                                                        {name}
                                                    </Typography>
                                                    <Grid
                                                        row
                                                        container
                                                        item
                                                        xs={12}
                                                        sm={12}
                                                    >
                                                        {list_actions &&
                                                            list_actions.map(
                                                                (action) => (
                                                                    <Grid
                                                                        item
                                                                        xs={3}
                                                                    >
                                                                        <FormControlLabel
                                                                            label={
                                                                                action.name
                                                                            }
                                                                            control={
                                                                                <Checkbox
                                                                                    size="small"
                                                                                    color="primary"
                                                                                    name={
                                                                                        action.id
                                                                                    }
                                                                                    onChange={
                                                                                        onPicker
                                                                                    }
                                                                                    checked={listChecked.includes(
                                                                                        action.id
                                                                                    )}
                                                                                />
                                                                            }
                                                                        />
                                                                    </Grid>
                                                                )
                                                            )}
                                                    </Grid>
                                                </>
                                            )
                                        })}
                                    </Grid>
                                ) : null}
                            </Grid>
                        </Grid>
                        <Grid container justify={'flex-end'}>
                            <Link to="/group-role">
                                <Button
                                    style={{
                                        marginRight: 20,
                                        boxShadow: 'none',
                                    }}
                                    color="inherit"
                                    variant="contained"
                                    onClick={() => {}}
                                >
                                    <span className="capitalize">Quay li</span>
                                </Button>
                            </Link>
                            <Button
                                color="primary"
                                variant="contained"
                                type="submit"
                            >
                                <span className="capitalize">Thêm mi</span>
                            </Button>
                        </Grid>
                    </ValidatorForm>
                </SimpleCard>
            </div>
        </div>
    )
}

const mapStateToProps = (state) => {
    return {}
}
export default connect(mapStateToProps, { showLoading, hideLoading })(
    CreateDeparment
)