application/redux/actions/assignment-actions.js
import BaseService from './../../services/base-service';
import {parseSrn} from './../../util/strings';
import Immutable from 'immutable';
class AssignmentService extends BaseService {
fetch(opts) {
return this.get('/v3/assignment', opts);
}
fetchOne(id) {
return this.get('/v3/assignment/' + id);
}
create(data) {
return this.post('/v3/assignment', data);
}
update(id, data) {
return this.put('/v3/assignment/' + id, data);
}
remove(id) {
return this.delete('/v3/assignment/' + id);
}
checkout(srn) {
return this.post('/v3/lock-request', {'srn': srn});
}
checkin(uuid, lockUuid) {
return this.delete('/v3/lock-request/' + lockUuid);
}
clearLock(userUuid, lockUuid) {
return this.delete('/v3/lock-request/' + lockUuid, {
'force_override': userUuid
});
}
}
const service = new AssignmentService;
/**
* Action types
*/
export const ASSIGNMENTS_REQUEST = 'ASSIGNMENTS_REQUEST';
export const ASSIGNMENTS_RECEIVE = 'ASSIGNMENTS_RECEIVE';
export const ASSIGNMENTS_RECEIVE_ONE = 'ASSIGNMENTS_RECEIVE_ONE';
export const ASSIGNMENTS_RECEIVE_ONE_FROM_SOCKET = 'ASSIGNMENTS_RECEIVE_ONE_FROM_SOCKET';
export const ASSIGNMENTS_SELECTED = 'ASSIGNMENTS_SELECTED';
export const ASSIGNMENTS_INVALIDATE = 'ASSIGNMENTS_INVALIDATE';
export const ASSIGNMENTS_RECEIVE_PAGINATION = 'ASSIGNMEN_RECEIVE_PAGINATION';
/**
* Action creators
*/
export function assignmentsReceivePagination(pagination) {
return {
type: ASSIGNMENTS_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 assignmentsRequest(params = {}) {
return {
type: ASSIGNMENTS_REQUEST,
payload: {
params: params
}
};
}
export function assignmentsReceive(items) {
return {
type: ASSIGNMENTS_RECEIVE,
payload: {
assignments: items ? items : []
}
};
}
export function assignmentsReceiveOne(item) {
return {
type: ASSIGNMENTS_RECEIVE_ONE,
payload: {
assignment: item ? item : {}
}
};
}
export function assignmentsReceiveOneFromSocket(item) {
return {
type: ASSIGNMENTS_RECEIVE_ONE_FROM_SOCKET,
payload: {
assignment: item ? item : {}
}
};
}
export function assignmentsSetSelected(assignment) {
return {
type: ASSIGNMENTS_SELECTED,
payload: {
selected: assignment ? assignment : false
}
};
}
export function assignmentsInvalidate() {
return {
type: ASSIGNMENTS_INVALIDATE,
payload: {
}
};
}
export function assignmentsShouldFetch(state) {
const assignments = state.assignments;
if (!assignments.items.length) {
return true;
} else if (assignments.isFetching) {
return false;
}
return assignments.didInvalidate;
}
export function assignmentsMaybeFetch(params = {}) {
return function(dispatch, getState) {
const assignments = getState().assignments;
if (!assignments.items || !assignments.items.size) {
return dispatch(assignmentsFetch(params));
}
return Promise.resolve();
}
};
export function assignmentsFetch(params = {}) {
return function(dispatch, getState) {
dispatch(assignmentsRequest(params));
params.per_page = params.per_page ? params.per_page : 20;
params.order = params.order ? params.order : "due_at";
params.dir = params.dir ? params.dir : "desc";
return service.fetch(params)
.then(response => {
dispatch(assignmentsReceivePagination(response));
return dispatch(assignmentsReceive(response.get('items')));
});
};
}
export function assignmentsMaybeFetchCache() {
return function(dispatch, getState) {
const assignments = getState().assignments;
if (!assignments.items || !assignments.items.size) {
return dispatch(assignmentsFetch({
'order': 'due_at',
'dir': 'desc',
'limit': 500,
'per_page': 500
}));
}
return Promise.resolve();
}
}
export function assignmentsFetchOne(id) {
return function (dispatch, getState) {
dispatch(assignmentsRequest({}));
return service.fetchOne(id)
.then(response => {
return dispatch(assignmentsReceiveOne(response.first()));
});
};
}
export function assignmentsReceiveWebSocket(assignments = {}) {
return function(dispatch, getState) {
console.log(assignments.toJS());
return Promise.all([
() => dispatch(assignmentsReceiveOneFromSocket(assignments.first()))
]);
};
}
export function assignmentsCreate(assignment = {}) {
return function(dispatch, getState) {
return service.create(assignment)
.then(response => {
dispatch(assignmentsSetSelected(response.first().get('uuid')));
return dispatch(assignmentsReceiveOne(response.first()));
});
}
}
export function assignmentsUpdate(id, assignment = {}) {
return function(dispatch, getState) {
return service.update(id, assignment)
.then(response => {
return dispatch(assignmentsReceiveOne(response.first()));
});
}
}
export function assignmentsRemove(uuids) {
return function(dispatch, getState) {
dispatch(assignmentsInvalidate());
return service.remove(uuids);
}
}
export function assignmentsCheckOut(srn) {
return function(dispatch, getState) {
return service.checkout(srn)
.then((data) => {
return parseSrn(srn);
});
}
}
export function assignmentsCheckIn(uuid, lockUuid) {
return function(dispatch, getState) {
return service.checkin(uuid, lockUuid);
}
}
export function assignmentsClearLock(userUuid, lockUuid) {
return function (dispatch, getState) {
return service.clearLock(userUuid, lockUuid);
}
}