application/redux/actions/user-actions.js
import BaseService from './../../services/base-service';
class UserService extends BaseService {
fetch(opts) {
return this.get('/v3/user', opts);
}
fetchOne(id) {
return this.get('/v3/user/' + id);
}
create(data) {
return this.post('/v3/user', data);
}
update(id, data) {
return this.put('/v3/user/' + id, data);
}
remove(uuid) {
return this.delete('/v3/user/' + uuid);
}
updateMe(data) {
return this.put('/v3/me', data);
}
search(query) {
return this.get('/v3/search', data);
}
}
const service = new UserService;
/**
* Action types
*/
export const USERS_REQUEST = 'USERS_REQUEST';
export const USERS_RECEIVE = 'USERS_RECEIVE';
export const USER_RECEIVE = 'USER_RECEIVE';
export const USER_SELECTED = 'USER_SELECTED';
export const USERS_INVALIDATE = 'USERS_INVALIDATE';
export const USERS_RECEIVE_PAGINATION = 'USERS_RECEIVE_PAGINATION';
/**
* Action creators
*/
export function usersReceivePagination(pagination) {
return {
type: USERS_RECEIVE_PAGINATION,
payload: {
before: pagination.get('before'),
current: pagination.get('current'),
first: pagination.get('first'),
last: pagination.get('last'),
limit: pagination.get('limit'),
next: pagination.get('next'),
total_items: pagination.get('total_items'),
total_pages: pagination.get('total_pages'),
}
};
}
export function usersRequest(params = {}) {
return {
type: USERS_REQUEST,
payload: {
params: params
}
};
}
export function usersReceive(items) {
return {
type: USERS_RECEIVE,
payload: {
users: items ? items : []
}
};
}
export function userReceive(user) {
return {
type: USER_RECEIVE,
payload: {
user: user ? user : {}
}
};
}
export function userSetSelected(user) {
return {
type: USER_SELECTED,
payload: {
selected: user ? user : false
}
};
}
export function usersInvalidate() {
return {
type: USERS_INVALIDATE,
payload: {
}
};
}
export function userUpdate(id, data) {
return function(dispatch, getState) {
dispatch(usersRequest({}));
return service.update(id, data)
.then(response => {
return dispatch(userReceive(response.first()));
});
}
}
export function userUpdateMe(data) {
return function(dispatch, getState) {
dispatch(usersRequest({}));
return service.updateMe(data)
.then(response => {
return response.first();
});
}
}
export function userCreate(data) {
return function(dispatch, getState) {
dispatch(usersRequest({}));
return service.create(data)
.then(response => {
return dispatch(userReceive(response.first()));
});
}
}
export function usersShouldFetch(state) {
const users = state.users;
if (!users.items.length) {
return true;
} else if (users.isFetching) {
return false;
}
return users.didInvalidate;
}
export function usersMaybeFetch(params = {}) {
return function(dispatch, getState) {
const users = getState().users.items;
if (getState().users.isFetching) {
return Promise.resolve();
}
if (!users.items || !users.items.size) {
return dispatch(usersFetch(params));
}
return Promise.resolve();
}
};
export function usersSearch(params = {}) {
return function(dispatch, getState) {
dispatch(usersRequest(params));
params.per_page = params.per_page ? params.per_page : 20;
params.order = params.order ? params.order : "name";
params.dir = params.dir ? params.dir : "asc";
// default page
let page = 0;
if (params.page) {
// specific page is requested
page = params.page;
} else if (getState().users.pagination.current) {
// existing pagination
page = getState().users.pagination.current;
}
if (page) {
params.page = page;
}
return service.search(params)
.then(response => {
dispatch(usersReceivePagination(response));
return dispatch(usersReceive(response.get('items')));
});
};
};
export function usersFetch(params = {}) {
return function(dispatch, getState) {
dispatch(usersRequest(params));
params.per_page = params.per_page ? params.per_page : 20;
params.order = params.order ? params.order : "name";
params.dir = params.dir ? params.dir : "asc";
// default page
let page = 0;
if (params.page) {
// specific page is requested
page = params.page;
} else if (getState().users.pagination.current) {
// existing pagination
page = getState().users.pagination.current;
}
if (page) {
params.page = page;
}
return service.fetch(params)
.then(response => {
dispatch(usersReceivePagination(response));
return dispatch(usersReceive(response.get('items')));
});
};
}
export function usersFetchOne(id) {
return function (dispatch, getState) {
dispatch(usersRequest({}));
return service.fetchOne(id)
.then(response => {
return dispatch(userReceive(response.first()));
});
}
}
export function usersRemove(uuid) {
return function (dispatch, getState) {
dispatch(usersInvalidate());
return service.remove(uuid);
}
}