orb-ui-firebase
Version:
Firebase with redux
190 lines (167 loc) • 5.23 kB
JavaScript
import * as types from './types';
import * as selectors from './selectors';
import * as initSelectors from '../initialization/selectors';
import { logError } from '../errors/actions';
import { logLoading, clearLoading } from '../loadings/actions';
export var initialize = function initialize(list, location, path, locationValue, append) {
return {
type: types.INIIALIZE,
payload: list,
path: path,
location: location,
append: append,
locationValue: locationValue
};
};
export var childAdded = function childAdded(child, location) {
return {
type: types.CHILD_ADDED,
payload: child,
location: location
};
};
export var childChanged = function childChanged(child, location) {
return {
type: types.CHILD_CHANGED,
payload: child,
location: location
};
};
export var childRemoved = function childRemoved(child, location) {
return {
type: types.CHILD_REMOVED,
payload: child,
location: location
};
};
export var destroy = function destroy(location) {
return {
type: types.DESTROY,
location: location
};
};
export var unWatch = function unWatch(path) {
return {
type: types.UNWATCH,
path: path
};
};
var getPath = function getPath(firebaseApp, ref) {
return ref._query.path.segments.join('/');
};
export var getRef = function getRef(firebaseApp, path) {
if (typeof path === 'string' || path instanceof String) {
return firebaseApp.firestore().collection(path);
} else {
return path;
}
};
export var getLocation = function getLocation(firebaseApp, path) {
if (typeof path === 'string' || path instanceof String) {
return path;
} else {
return getPath(firebaseApp, path);
}
};
export function watchCol(firebaseApp, firebasePath) {
var reduxPath = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var append = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var ref = getRef(firebaseApp, firebasePath);
var path = getLocation(firebaseApp, firebasePath);
var location = reduxPath || path;
return function (dispatch, getState) {
var isInitialized = initSelectors.isInitialised(getState(), path, location);
var initialized = false;
if (!isInitialized) {
dispatch(logLoading(location));
var unsub = ref.onSnapshot(function (snapshot) {
if (snapshot.size === 0) {
dispatch(clearLoading(location));
}
snapshot.docChanges().forEach(function (change) {
if (change.type === 'added') {
if (initialized) {
dispatch(childAdded({
id: change.doc.id,
data: change.doc.data()
}, location));
} else {
initialized = true;
dispatch(initialize([{
id: change.doc.id,
data: change.doc.data()
}], location, path, unsub, append));
}
}
if (change.type === 'modified') {
dispatch(childChanged({
id: change.doc.id,
data: change.doc.data()
}, location));
}
if (change.type === 'removed') {
dispatch(childRemoved({
id: change.doc.id,
data: change.doc.data()
}, location));
}
});
}, function (err) {
console.error(err);
dispatch(logError(location, err));
});
}
};
}
export function unwatchCol(firebaseApp, firebasePath) {
return function (dispatch, getState) {
var location = firebasePath;
var allInitializations = selectors.getAllInitializations(getState());
var unsubs = allInitializations[location];
if (unsubs) {
Object.keys(unsubs).map(function (key) {
var unsub = unsubs[key];
if (typeof unsub === 'function') {
unsub();
}
dispatch(unWatch(location));
});
}
};
}
export function destroyCol(firebaseApp, firebasePath) {
var reduxPath = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
return function (dispatch, getState) {
var location = reduxPath || getLocation(firebaseApp, firebasePath);
var locations = getState().initialization[location];
dispatch(unWatch(location));
dispatch(destroy(location));
if (reduxPath) {
dispatch(destroy(reduxPath));
unwatchCol(firebaseApp, reduxPath);
} else if (locations) {
Object.keys(locations).forEach(function (location) {
unwatchCol(firebaseApp, location);
dispatch(destroy(location));
});
}
};
}
export function unwatchAllCol(firebaseApp, path) {
return function (dispatch, getState) {
var allLists = selectors.getAllCols(getState());
Object.keys(allLists).forEach(function (key, index) {
unwatchCol(firebaseApp, key);
dispatch(unWatch(key));
});
};
}
export function unwatchAllCols(firebaseApp, path) {
return function (dispatch, getState) {
var allColls = selectors.getAllCols(getState());
Object.keys(allColls).forEach(function (key, index) {
unwatchCol(firebaseApp, key);
dispatch(destroyCol(firebaseApp, allColls[index]));
});
};
}