create.js 8.19 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
import React, { useState, useEffect } from 'react'
import {
    ValidatorForm,
    TextValidator,
    SelectValidator,
} from 'react-material-ui-form-validator'
import { Button, Grid, InputLabel, MenuItem } from '@material-ui/core'

import { createPageStatic } from 'app/apis/Functions/pageStatic'
import { showLoading, hideLoading } from 'app/redux/actions/loadingAction'
import { toast } from 'react-toastify'
import { Breadcrumb, SimpleCard } from 'app/components'
import { Link, useHistory, useLocation } from 'react-router-dom'
import { trimObject, convertDate } from 'app/config/Function'
import { connect } from 'react-redux'
import { CKEditor } from '@ckeditor/ckeditor5-react'
import ClassicEditor from '@ckeditor/ckeditor5-build-classic'
// import ClassicEditor from 'ckeditor5-build-classic'
import PickerImage from '../../../components/Input/PickerImage'
import {
    MuiPickersUtilsProvider,
    KeyboardDatePicker,
} from '@material-ui/pickers'

import { apiUploadFile } from 'app/apis/Functions/Upload.js'

const SimpleForm = (props) => {
    const [state, setState] = useState({})
    const history = useHistory()

    const [date, setDate] = useState(new Date())

    const handleSubmit = async (event) => {
        const newValue = trimObject(state)
        console.log('newValue', newValue)
        props.showLoading()
        const res = await createPageStatic({
            ...newValue,
            realease_package_id: 1,
            orders: 1,
            status: 1,
            date_publish: convertDate(date),
        })
        props.hideLoading()
        if (res.data.code == 200) {
            history.push('/system/page-static')
            if (res.data.code == 200) {
                toast.success('Tạo tin tức thành công!', {
                    theme: 'colored',
                })
            }
        } else {
            toast.error('Tạo tin tức thất bại!', {
                theme: 'colored',
            })
        }
    }

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

    function uploadPlugin(editor) {
        editor.plugins.get('FileRepository').createUploadAdapter = (loader) => {
            return uploadAdapter(loader)
        }
    }
    function uploadAdapter(loader) {
        return {
            upload: () => {
                return new Promise((resolve, reject) => {
                    const body = new FormData()
                    loader.file.then((file) => {
                        body.append('files', file)
                        apiUploadFile(body)
                            .then((res) => {
                                resolve({
                                    default: res.data.data[0].url,
                                })
                            })
                            .catch((err) => {
                                reject(err)
                            })
                    })
                })
            },
        }
    }

    return (
        <div className="m-sm-30">
            <div className="mb-sm-30">
                <div className="mb-sm-30">
                    <Breadcrumb
                        routeSegments={[
                            {
                                name: 'Danh sách trang tĩnh',
                                path: '/system/page-static',
                            },
                            { name: 'Thêm mới trang tĩnh' },
                        ]}
                    />
                </div>
                <SimpleCard>
                    <ValidatorForm onSubmit={handleSubmit} onError={() => null}>
                        <Grid container spacing={3}>
                            <Grid item lg={6} md={6} sm={12} xs={12}>
                                <TextValidator
                                    variant="outlined"
                                    className="mb-4 w-full"
                                    label="Code *"
                                    onChange={handleChange}
                                    type="text"
                                    name="code"
                                    value={code || ''}
                                    validators={['required']}
                                    errorMessages={[
                                        'Không được để trống trường này',
                                    ]}
                                />
                            </Grid>
                            <Grid item lg={6} md={6} sm={12} xs={12}>
                                <TextValidator
                                    variant="outlined"
                                    className="mb-4 w-full"
                                    label="Tên trang tĩnh *"
                                    onChange={handleChange}
                                    type="text"
                                    name="name"
                                    value={name || ''}
                                    validators={['required']}
                                    errorMessages={[
                                        'Không được để trống trường này',
                                    ]}
                                />
                            </Grid>

                            <Grid item xs={12} sm={12}>
                                <InputLabel style={{ paddingBottom: '5px' }}>
                                    Ni dung
                                </InputLabel>

                                <CKEditor
                                    style={{
                                        height: 200,
                                    }}
                                    config={{
                                        extraPlugins: [uploadPlugin],
                                    }}
                                    editor={ClassicEditor}
                                    content
                                    onReady={(editor) => {
                                        // You can store the "editor" and use when it is needed.
                                    }}
                                    // value={content}
                                    onChange={(event, editor) => {
                                        const content = editor.getData()
                                        setState({
                                            ...state,
                                            description: content,
                                        })
                                    }}
                                    onBlur={(event, editor) => {}}
                                    onFocus={(event, editor) => {}}
                                />
                            </Grid>
                        </Grid>
                        <Grid
                            container
                            justify={'flex-end'}
                            style={{ marginTop: 20 }}
                        >
                            <Link to="/system/page-static">
                                <Button
                                    style={{
                                        marginRight: 20,
                                    }}
                                    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 })(
    SimpleForm
)