application/services/user-service.js
import Immutable from 'immutable';
import myAlt from './../myalt';
import Alt from 'alt';
import BaseService from './base-service';
import {ErrorService} from './error-service';
import {parseSrn} from './../util/strings';
import {info} from './../util/console';
/**
* Interact with user API service
*/
class UserService extends BaseService {
updateUsers(users) {
return users;
}
reset() {
return (dispatch) => {
setTimeout(() => {
this.updateUsers(Immutable.fromJS([]));
}, 1);
}
}
/**
* Run a query on the search endpoint. See search docs for query info
* @param {object} opts Query has
*/
search(opts) {
if (opts && !opts.type) {
opts.type = 'user';
}
return (dispatch) => {
return this.get('/v3/search', opts)
.then((data) => {
info('Search returned', data);
this.updateUsers(data.get('items'))
});
};
}
}
const service = myAlt.createActions(UserService);
/**
* User Flux store. Tracks
* <ul>
* <li>user</li>
* </ul>
*/
class UsersStore {
constructor() {
this.state = {
'users': Immutable.Map(),
};
this.bindListeners({
'handleUpdateUsers': service.UPDATE_USERS
});
}
handleUpdateUsers(users) {
this.setState({'users': users});
}
}
const store = myAlt.createStore(UsersStore, 'UsersStore');
/**
* Instace wrapper if you don't or can't use the singleton action and classes.
*
* Actions and store are both under the 'users' key.
*/
class UserFlux extends Alt {
constructor(config = {}) {
super(config);
this.addActions('users', UserService);
this.addStore('users', UsersStore);
}
}
export {UserFlux as default, service as UserService, store as UsersStore};