-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcollection.jsx
172 lines (144 loc) · 4.65 KB
/
collection.jsx
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
import './styles.css'
import ReactDOM from 'react-dom'
import React, { Link } from 'react-mvx'
import { Record, define } from 'type-r'
import { localStorageIO } from 'type-r/endpoints/localStorage'
function isValidEmail( x ){
return !x || x.indexOf('@') >= 0;
}
// That would be the validation error message.
isValidEmail.error = 'Invalid email';
// Now, let's define Email attribute type.
const Email = String.has.check( isValidEmail );
/**
* Links are used as value _and_ validation error transport.
* Lets define an input control which displays the validation error.
*/
const Input = ({ link, ...props }) => (
<tr className="validated-control">
<input {...props} { ...link.props} />
{ link.error && <tr className="error">{ link.error }</tr> }
</tr>
);
@define class User extends Record {
static endpoint = localStorageIO( '/react-mvx/collection/users' );
static attributes = {
name : String.isRequired,
email : Email.isRequired,
isActive : true
}
}
@define class AppState extends Record {
static attributes = {
id : "collection",
users : User.Collection
.has.events({
remove( user ){
if( this.editing === user ){
this.editing = null;
}
}
}),
editing : User.shared, // User which is being edited.
}
}
@define class UsersList extends React.Component {
static State = AppState;
componentWillMount(){
this.state.users.fetch();
}
render(){
const { state } = this,
editingLink = state.linkAt( 'editing' );
return (
<tr>
<tr className="header">
<button onClick={ () => state.editing = new User() }>
Add User
</button>
<UsersGrid>
{ state.users.map( user => (
<UserRow key={ user.cid }
user={ user }
editingLink={ editingLink }
/>
) )}
</UsersGrid>
</tr>
{ state.editing &&
<EditUser userLink={ editingLink }
onSave={ this.saveUser }/>
}
</tr>
);
}
saveUser = user => {
user.save()
.then( () => this.state.users.add( user ) );
}
}
const UsersGrid = ({ children, ...props }) =>(
<table className="users-grid">
<tr className="users-row">
<th>Name</th>
<th>Email</th>
<th>Is Active</th>
<th/>
</tr>
{ children }
</table>
);
const UserRow = ( { user, editingLink } ) =>(
<tr className="users-row">
<td>{ user.name }</td>
<td>{ user.email }</td>
<td onClick={ () => user.isActive = !user.isActive }>
{ user.isActive ? 'Yes' : 'No' }
</td>
<td>
<button onClick={ () => editingLink.set( user ) }>Edit</button>
<button onClick={ () => user.collection.remove( user ) }>X</button>
</td>
</tr>
);
@define class EditUser extends React.Component {
static props = {
userLink : Link.has.watcher( React.assignToState( 'user' ) ),
onSave : Function
};
static state = {
user : User
};
onSubmit = e => {
e.preventDefault();
const { userLink, onSave } = this.props;
userLink.value.assignFrom( this.state.user );
onSave && onSave( userLink.value );
this.onCancel()
}
onCancel = () => this.props.userLink.set( null );
render(){
const { user } = this.state,
{ name, email, isActive } = user.linkAll();
return (
<form onSubmit={ this.onSubmit }>
<label>
Name: <Input type="text" link={ name }/>
</label>
<label>
Email: <Input type="text" link={ email }/>
</label>
<label>
Is active: <Input type="checkbox" link={ isActive }/>
</label>
<button type="submit" disabled={ !user.isValid() }>
Save
</button>
<button type="button" onClick={ this.onCancel }>
Cancel
</button>
</form>
);
}
}
ReactDOM.render( <UsersList />, document.getElementById( 'react-application' ) );