1+ import _ from 'lodash'
12import React from 'react'
23import PropTypes from 'prop-types'
34import { FormGroup , ControlLabel , FormControl , Button } from 'react-bootstrap'
@@ -7,41 +8,74 @@ export default class Category extends React.Component {
78
89 componentDidMount ( ) {
910 return new AV . Query ( 'Category' )
10- . get ( this . props . params . id )
11- . then ( ( category ) => {
12- this . setState ( {
13- category
11+ . doesNotExist ( 'parent' )
12+ . find ( )
13+ . then ( categories => {
14+ const categoryId = this . props . params . id
15+ return Promise . resolve ( )
16+ . then ( ( ) => {
17+ if ( categoryId == '_new' ) {
18+ return new AV . Object ( 'Category' , {
19+ name : '' ,
20+ qTemplate : '' ,
21+ } )
22+ }
23+
24+ const category = _ . find ( categories , { id : categoryId } )
25+ if ( category ) {
26+ return category
27+ }
28+
29+ return new AV . Query ( 'Category' ) . get ( categoryId )
30+ } )
31+ . then ( category => {
32+ this . setState ( {
33+ name : category . get ( 'name' ) ,
34+ qTemplate : category . get ( 'qTemplate' ) ,
35+ category,
36+ parentCategory : null ,
37+ categories,
38+ } )
39+ return
1440 } )
1541 } )
1642 }
1743
1844 handleNameChange ( e ) {
19- const category = this . state . category
20- category . set ( 'name' , e . target . value )
21- this . setState ( { category} )
45+ this . setState ( { name : e . target . value } )
46+ }
47+
48+ handleParentChange ( e ) {
49+ const category = this . state . categories . find ( c => c . id === e . target . value )
50+ this . setState ( { parentCategory : category } )
2251 }
2352
2453 handleQTemplateChange ( e ) {
25- const category = this . state . category
26- category . set ( 'qTemplate' , e . target . value )
27- this . setState ( { category} )
54+ this . setState ( { qTemplate : e . target . value } )
2855 }
2956
3057 handleSubmit ( e ) {
3158 e . preventDefault ( )
3259 const category = this . state . category
33- category . save ( )
60+ return category . save ( {
61+ name : this . state . name ,
62+ parent : this . state . parentCategory ,
63+ qTemplate : this . state . qTemplate ,
64+ } )
3465 . then ( ( ) => {
35- this . setState ( { category} )
66+ this . context . router . push ( '/settings/categories' )
67+ return
3668 } )
69+ . then ( this . context . addNotification )
3770 }
3871
3972 handleDelete ( ) {
40- const result = confirm ( '确认要删除分类 :' + this . state . category . get ( 'name' ) )
73+ const result = confirm ( '确认要停用分类 :' + this . state . category . get ( 'name' ) )
4174 if ( result ) {
42- this . state . category . destroy ( )
75+ return this . state . category . destroy ( )
4376 . then ( ( ) => {
4477 this . context . router . push ( '/settings/categories' )
78+ return
4579 } )
4680 . catch ( this . context . addNotification )
4781 }
@@ -52,26 +86,41 @@ export default class Category extends React.Component {
5286 return < div > 数据读取中……</ div >
5387 }
5488
89+ const categorieOptions = this . state . categories . map ( c => {
90+ return < option key = { c . id } value = { c . id } > { c . get ( 'name' ) } </ option >
91+ } )
92+
5593 return (
5694 < div >
57- < h1 > 分类修改</ h1 >
5895 < form onSubmit = { this . handleSubmit . bind ( this ) } >
59- < FormGroup controlId = "qTemplateTextarea " >
96+ < FormGroup controlId = "nameText " >
6097 < ControlLabel > 分类名称</ ControlLabel >
61- < FormControl type = "text" value = { this . state . category . get ( 'name' ) } onChange = { this . handleNameChange . bind ( this ) } />
98+ < FormControl type = "text" value = { this . state . name } onChange = { this . handleNameChange . bind ( this ) } />
99+ </ FormGroup >
100+ < FormGroup controlId = "parentSelect" >
101+ < ControlLabel > 父分类(可选)</ ControlLabel >
102+ < FormControl componentClass = 'select'
103+ value = { this . state . category . get ( 'parent' ) && this . state . category . get ( 'parent' ) . id }
104+ onChange = { this . handleParentChange . bind ( this ) } >
105+ < option value = '' > </ option >
106+ { categorieOptions }
107+ </ FormControl >
62108 </ FormGroup >
63- < FormGroup >
109+ < FormGroup controlId = "qTemplateTextarea" >
64110 < ControlLabel > 问题描述模板</ ControlLabel >
65111 < FormControl
66112 componentClass = "textarea"
67113 placeholder = "用户新建该分类工单时,问题描述默认显示这里的内容。"
68114 rows = '8'
69- value = { this . state . category . get ( ' qTemplate' ) }
115+ value = { this . state . qTemplate }
70116 onChange = { this . handleQTemplateChange . bind ( this ) } />
71117 </ FormGroup >
72- < Button type = 'submit' > 保存</ Button >
118+ < Button type = 'submit' bsStyle = 'success' > 保存</ Button >
73119 { ' ' }
74- < Button type = 'button' bsStyle = "danger" onClick = { this . handleDelete . bind ( this ) } > 删除</ Button >
120+ { this . state . category . id
121+ && < Button type = 'button' bsStyle = "danger" onClick = { this . handleDelete . bind ( this ) } > 删除</ Button >
122+ || < Button type = 'button' onClick = { ( ) => this . context . router . push ( '/settings/categories' ) } > 取消</ Button >
123+ }
75124 </ form >
76125 </ div >
77126 )
0 commit comments