Unverified Commit d8f9baae authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #1674 from hypothesis/convert-sidebar-store-to-es-modules

Convert src/sidebar/store to ES modules
parents db679988 25e5e7fe
const redux = require('redux'); import * as redux from 'redux';
const { default: thunk } = require('redux-thunk'); import thunk from 'redux-thunk';
const { createReducer, bindSelectors } = require('./util'); import { createReducer, bindSelectors } from './util';
/** /**
* Create a Redux store from a set of _modules_. * Create a Redux store from a set of _modules_.
...@@ -22,7 +22,7 @@ const { createReducer, bindSelectors } = require('./util'); ...@@ -22,7 +22,7 @@ const { createReducer, bindSelectors } = require('./util');
* @param {any[]} initArgs - Arguments to pass to each state module's `init` function * @param {any[]} initArgs - Arguments to pass to each state module's `init` function
* @param [any[]] middleware - List of additional Redux middlewares to use. * @param [any[]] middleware - List of additional Redux middlewares to use.
*/ */
function createStore(modules, initArgs = [], middleware = []) { export default function createStore(modules, initArgs = [], middleware = []) {
// Create the initial state and state update function. // Create the initial state and state update function.
// Namespaced objects for initial states. // Namespaced objects for initial states.
...@@ -74,5 +74,3 @@ function createStore(modules, initArgs = [], middleware = []) { ...@@ -74,5 +74,3 @@ function createStore(modules, initArgs = [], middleware = []) {
return store; return store;
} }
module.exports = createStore;
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* to the console, along with the application state before and after the action * to the console, along with the application state before and after the action
* was handled. * was handled.
*/ */
function debugMiddleware(store) { export default function debugMiddleware(store) {
/* eslint-disable no-console */ /* eslint-disable no-console */
let serial = 0; let serial = 0;
...@@ -34,5 +34,3 @@ function debugMiddleware(store) { ...@@ -34,5 +34,3 @@ function debugMiddleware(store) {
}; };
/* eslint-enable no-console */ /* eslint-enable no-console */
} }
module.exports = debugMiddleware;
...@@ -29,21 +29,20 @@ ...@@ -29,21 +29,20 @@
* 3. Checking that the UI correctly presents a given state. * 3. Checking that the UI correctly presents a given state.
*/ */
const createStore = require('./create-store'); import createStore from './create-store';
const debugMiddleware = require('./debug-middleware'); import debugMiddleware from './debug-middleware';
import activity from './modules/activity';
const activity = require('./modules/activity'); import annotations from './modules/annotations';
const annotations = require('./modules/annotations'); import directLinked from './modules/direct-linked';
const directLinked = require('./modules/direct-linked'); import drafts from './modules/drafts';
const drafts = require('./modules/drafts'); import frames from './modules/frames';
const frames = require('./modules/frames'); import groups from './modules/groups';
const links = require('./modules/links'); import links from './modules/links';
const groups = require('./modules/groups'); import realTimeUpdates from './modules/real-time-updates';
const realTimeUpdates = require('./modules/real-time-updates'); import selection from './modules/selection';
const selection = require('./modules/selection'); import session from './modules/session';
const session = require('./modules/session'); import sidebarPanels from './modules/sidebar-panels';
const sidebarPanels = require('./modules/sidebar-panels'); import viewer from './modules/viewer';
const viewer = require('./modules/viewer');
/** /**
* Redux middleware which triggers an Angular change-detection cycle * Redux middleware which triggers an Angular change-detection cycle
...@@ -79,7 +78,7 @@ function angularDigestMiddleware($rootScope) { ...@@ -79,7 +78,7 @@ function angularDigestMiddleware($rootScope) {
* passing the current state of the store. * passing the current state of the store.
*/ */
// @ngInject // @ngInject
function store($rootScope, settings) { export default function store($rootScope, settings) {
const middleware = [ const middleware = [
debugMiddleware, debugMiddleware,
angularDigestMiddleware.bind(null, $rootScope), angularDigestMiddleware.bind(null, $rootScope),
...@@ -101,5 +100,3 @@ function store($rootScope, settings) { ...@@ -101,5 +100,3 @@ function store($rootScope, settings) {
]; ];
return createStore(modules, [settings], middleware); return createStore(modules, [settings], middleware);
} }
module.exports = store;
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* need to be reflected in the UI. * need to be reflected in the UI.
*/ */
const { actionTypes } = require('../util'); import { actionTypes } from '../util';
function init() { function init() {
return { return {
...@@ -93,7 +93,7 @@ function isFetchingAnnotations(state) { ...@@ -93,7 +93,7 @@ function isFetchingAnnotations(state) {
return state.activity.activeAnnotationFetches > 0; return state.activity.activeAnnotationFetches > 0;
} }
module.exports = { export default {
init, init,
update, update,
namespace: 'activity', namespace: 'activity',
......
...@@ -3,15 +3,15 @@ ...@@ -3,15 +3,15 @@
* sidebar. * sidebar.
*/ */
const { createSelector } = require('reselect'); import { createSelector } from 'reselect';
const arrayUtil = require('../../util/array'); import uiConstants from '../../ui-constants';
const metadata = require('../../util/annotation-metadata'); import * as metadata from '../../util/annotation-metadata';
const uiConstants = require('../../ui-constants'); import * as arrayUtil from '../../util/array';
import * as util from '../util';
const selection = require('./selection'); import drafts from './drafts';
const drafts = require('./drafts'); import selection from './selection';
const util = require('../util');
/** /**
* Return a copy of `current` with all matching annotations in `annotations` * Return a copy of `current` with all matching annotations in `annotations`
...@@ -461,7 +461,7 @@ const isWaitingToAnchorAnnotations = createSelector( ...@@ -461,7 +461,7 @@ const isWaitingToAnchorAnnotations = createSelector(
annotations => annotations.some(metadata.isWaitingToAnchor) annotations => annotations.some(metadata.isWaitingToAnchor)
); );
module.exports = { export default {
init: init, init: init,
namespace: 'annotations', namespace: 'annotations',
update: update, update: update,
......
const util = require('../util'); import * as util from '../util';
function init(settings) { function init(settings) {
return { return {
...@@ -125,7 +125,7 @@ function clearDirectLinkedIds() { ...@@ -125,7 +125,7 @@ function clearDirectLinkedIds() {
}; };
} }
module.exports = { export default {
init, init,
namespace: 'directLinked', namespace: 'directLinked',
update, update,
......
const metadata = require('../../util/annotation-metadata'); import * as metadata from '../../util/annotation-metadata';
const util = require('../util'); import * as util from '../util';
/** /**
* The drafts store provides temporary storage for unsaved edits to new or * The drafts store provides temporary storage for unsaved edits to new or
...@@ -23,7 +23,7 @@ function init() { ...@@ -23,7 +23,7 @@ function init() {
* which are the user's draft changes to the annotation. These are returned * which are the user's draft changes to the annotation. These are returned
* from the drafts store selector by `drafts.getDraft()`. * from the drafts store selector by `drafts.getDraft()`.
*/ */
class Draft { export class Draft {
constructor(annotation, changes) { constructor(annotation, changes) {
this.annotation = { id: annotation.id, $tag: annotation.$tag }; this.annotation = { id: annotation.id, $tag: annotation.$tag };
this.isPrivate = changes.isPrivate; this.isPrivate = changes.isPrivate;
...@@ -94,7 +94,8 @@ function createDraft(annotation, changes) { ...@@ -94,7 +94,8 @@ function createDraft(annotation, changes) {
*/ */
function deleteNewAndEmptyDrafts() { function deleteNewAndEmptyDrafts() {
const annotations = require('./annotations'); const { default: annotations } = require('./annotations');
return (dispatch, getState) => { return (dispatch, getState) => {
const newDrafts = getState().drafts.filter(draft => { const newDrafts = getState().drafts.filter(draft => {
return ( return (
...@@ -183,7 +184,7 @@ function unsavedAnnotations(state) { ...@@ -183,7 +184,7 @@ function unsavedAnnotations(state) {
.map(draft => draft.annotation); .map(draft => draft.annotation);
} }
module.exports = { export default {
init, init,
namespace: 'drafts', namespace: 'drafts',
update, update,
...@@ -200,5 +201,4 @@ module.exports = { ...@@ -200,5 +201,4 @@ module.exports = {
getDraftIfNotEmpty, getDraftIfNotEmpty,
unsavedAnnotations, unsavedAnnotations,
}, },
Draft,
}; };
const { createSelector } = require('reselect'); import { createSelector } from 'reselect';
const util = require('../util'); import * as util from '../util';
function init() { function init() {
// The list of frames connected to the sidebar app // The list of frames connected to the sidebar app
...@@ -113,7 +113,7 @@ function searchUris(state) { ...@@ -113,7 +113,7 @@ function searchUris(state) {
}, []); }, []);
} }
module.exports = { export default {
init: init, init: init,
namespace: 'frames', namespace: 'frames',
update: update, update: update,
......
const { createSelector } = require('reselect'); import { createSelector } from 'reselect';
const util = require('../util'); import * as util from '../util';
const session = require('./session');
import session from './session';
function init() { function init() {
return { return {
...@@ -189,7 +190,7 @@ const getInScopeGroups = createSelector( ...@@ -189,7 +190,7 @@ const getInScopeGroups = createSelector(
groups => groups.filter(g => g.isScopedToUri) groups => groups.filter(g => g.isScopedToUri)
); );
module.exports = { export default {
init, init,
namespace: 'groups', namespace: 'groups',
update, update,
......
...@@ -22,7 +22,7 @@ function updateLinksAction(newLinks) { ...@@ -22,7 +22,7 @@ function updateLinksAction(newLinks) {
return { type: 'UPDATE_LINKS', newLinks: newLinks }; return { type: 'UPDATE_LINKS', newLinks: newLinks };
} }
module.exports = { export default {
init: init, init: init,
namespace: 'links', namespace: 'links',
update: { UPDATE_LINKS: updateLinks }, update: { UPDATE_LINKS: updateLinks },
......
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
* WebSocket connection to h's real-time API. * WebSocket connection to h's real-time API.
*/ */
const { createSelector } = require('reselect'); import { createSelector } from 'reselect';
const { actionTypes } = require('../util'); import { actionTypes } from '../util';
const annotations = require('./annotations'); import annotations from './annotations';
const groups = require('./groups'); import groups from './groups';
const viewer = require('./viewer'); import viewer from './viewer';
function init() { function init() {
return { return {
...@@ -172,7 +172,7 @@ function hasPendingDeletion(state, id) { ...@@ -172,7 +172,7 @@ function hasPendingDeletion(state, id) {
return state.realTimeUpdates.pendingDeletions.hasOwnProperty(id); return state.realTimeUpdates.pendingDeletions.hasOwnProperty(id);
} }
module.exports = { export default {
init, init,
namespace: 'realTimeUpdates', namespace: 'realTimeUpdates',
update, update,
......
...@@ -14,15 +14,14 @@ ...@@ -14,15 +14,14 @@
* @property {string} displayName - User's display name * @property {string} displayName - User's display name
*/ */
const { createSelector } = require('reselect'); import { createSelector } from 'reselect';
const immutable = require('seamless-immutable'); import immutable from 'seamless-immutable';
const arrayUtil = require('../../util/array'); import uiConstants from '../../ui-constants';
const metadata = require('../../util/annotation-metadata'); import * as metadata from '../../util/annotation-metadata';
const { toSet } = require('../../util/array'); import * as arrayUtil from '../../util/array';
const uiConstants = require('../../ui-constants'); import { toSet } from '../../util/array';
import * as util from '../util';
const util = require('../util');
/** /**
* Default starting tab. * Default starting tab.
...@@ -500,7 +499,7 @@ function focusModeUserPrettyName(state) { ...@@ -500,7 +499,7 @@ function focusModeUserPrettyName(state) {
} }
} }
module.exports = { export default {
init: init, init: init,
namespace: 'selection', namespace: 'selection',
update: update, update: update,
......
const util = require('../util'); import * as util from '../util';
function init() { function init() {
/** /**
...@@ -65,7 +65,7 @@ function profile(state) { ...@@ -65,7 +65,7 @@ function profile(state) {
return state.session; return state.session;
} }
module.exports = { export default {
init, init,
namespace: 'session', namespace: 'session',
update, update,
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* may be "active" (open) at one time. * may be "active" (open) at one time.
*/ */
const util = require('../util'); import * as util from '../util';
function init() { function init() {
return { return {
...@@ -111,7 +111,7 @@ function isSidebarPanelOpen(state, panelName) { ...@@ -111,7 +111,7 @@ function isSidebarPanelOpen(state, panelName) {
return state.sidebarPanels.activePanelName === panelName; return state.sidebarPanels.activePanelName === panelName;
} }
module.exports = { export default {
namespace: 'sidebarPanels', namespace: 'sidebarPanels',
init: init, init: init,
update: update, update: update,
......
const createStore = require('../../create-store'); import createStore from '../../create-store';
const activity = require('../activity'); import activity from '../activity';
describe('sidebar/store/modules/activity', () => { describe('sidebar/store/modules/activity', () => {
let store; let store;
......
const annotations = require('../annotations'); import * as fixtures from '../../../test/annotation-fixtures';
const createStore = require('../../create-store'); import uiConstants from '../../../ui-constants';
const drafts = require('../drafts'); import * as metadata from '../../../util/annotation-metadata';
const fixtures = require('../../../test/annotation-fixtures'); import createStore from '../../create-store';
import annotations from '../annotations';
const metadata = require('../../../util/annotation-metadata'); import drafts from '../drafts';
import groups from '../groups';
const groups = require('../groups'); import selection from '../selection';
const selection = require('../selection'); import session from '../session';
const session = require('../session'); import viewer from '../viewer';
const viewer = require('../viewer');
const uiConstants = require('../../../ui-constants');
const { actions, selectors } = annotations; const { actions, selectors } = annotations;
......
const createStore = require('../../create-store'); import createStore from '../../create-store';
const directLinked = require('../direct-linked'); import directLinked from '../direct-linked';
describe('sidebar/store/modules/direct-linked', () => { describe('sidebar/store/modules/direct-linked', () => {
let store; let store;
......
const immutable = require('seamless-immutable'); import immutable from 'seamless-immutable';
const drafts = require('../drafts'); import createStore from '../../create-store';
const annotations = require('../annotations'); import annotations from '../annotations';
const selection = require('../selection'); import drafts from '../drafts';
const { Draft } = require('../drafts'); import { Draft } from '../drafts';
const createStore = require('../../create-store'); import selection from '../selection';
const fixtures = immutable({ const fixtures = immutable({
draftWithText: { draftWithText: {
......
const frames = require('../frames'); import createStore from '../../create-store';
const createStore = require('../../create-store'); import frames from '../frames';
describe('sidebar/store/modules/frames', function() { describe('sidebar/store/modules/frames', function() {
let store; let store;
......
const immutable = require('seamless-immutable'); import immutable from 'seamless-immutable';
const createStore = require('../../create-store'); import createStore from '../../create-store';
const groups = require('../groups'); import groups from '../groups';
const session = require('../session'); import session from '../session';
describe('sidebar/store/modules/groups', () => { describe('sidebar/store/modules/groups', () => {
const publicGroup = immutable({ const publicGroup = immutable({
......
const links = require('../links'); import links from '../links';
const init = links.init; const init = links.init;
const update = links.update.UPDATE_LINKS; const update = links.update.UPDATE_LINKS;
......
const createStore = require('../../create-store'); import createStore from '../../create-store';
import annotations from '../annotations';
const annotations = require('../annotations'); import groups from '../groups';
const groups = require('../groups'); import realTimeUpdates from '../real-time-updates';
const realTimeUpdates = require('../real-time-updates'); import { $imports } from '../real-time-updates';
const { $imports } = require('../real-time-updates'); import selection from '../selection';
const selection = require('../selection');
const { removeAnnotations } = annotations.actions; const { removeAnnotations } = annotations.actions;
const { focusGroup } = groups.actions; const { focusGroup } = groups.actions;
...@@ -28,13 +27,19 @@ describe('sidebar/store/modules/real-time-updates', () => { ...@@ -28,13 +27,19 @@ describe('sidebar/store/modules/real-time-updates', () => {
$imports.$mock({ $imports.$mock({
'./annotations': { './annotations': {
selectors: { annotationExists: fakeAnnotationExists }, default: {
selectors: { annotationExists: fakeAnnotationExists },
},
}, },
'./groups': { './groups': {
selectors: { focusedGroupId: fakeFocusedGroupId }, default: {
selectors: { focusedGroupId: fakeFocusedGroupId },
},
}, },
'./viewer': { './viewer': {
selectors: { isSidebar: fakeIsSidebar }, default: {
selectors: { isSidebar: fakeIsSidebar },
},
}, },
}); });
}); });
......
const annotations = require('../annotations'); import uiConstants from '../../../ui-constants';
const createStore = require('../../create-store'); import createStore from '../../create-store';
const selection = require('../selection'); import annotations from '../annotations';
const uiConstants = require('../../../ui-constants'); import selection from '../selection';
describe('sidebar/store/modules/selection', () => { describe('sidebar/store/modules/selection', () => {
let store; let store;
......
const createStore = require('../../create-store'); import createStore from '../../create-store';
const session = require('../session'); import session from '../session';
const { init } = session; const { init } = session;
......
const createStore = require('../../create-store'); import createStore from '../../create-store';
const sidebarPanels = require('../sidebar-panels'); import sidebarPanels from '../sidebar-panels';
describe('sidebar/store/modules/sidebar-panels', () => { describe('sidebar/store/modules/sidebar-panels', () => {
let store; let store;
......
const viewer = require('../viewer'); import createStore from '../../create-store';
const createStore = require('../../create-store'); import viewer from '../viewer';
describe('store/modules/viewer', function() { describe('store/modules/viewer', function() {
let store; let store;
......
const util = require('../util'); import * as util from '../util';
/** /**
* This module defines actions and state related to the display mode of the * This module defines actions and state related to the display mode of the
...@@ -50,7 +50,7 @@ function isSidebar(state) { ...@@ -50,7 +50,7 @@ function isSidebar(state) {
return state.viewer.isSidebar; return state.viewer.isSidebar;
} }
module.exports = { export default {
init: init, init: init,
namespace: 'viewer', namespace: 'viewer',
update: update, update: update,
......
const createStore = require('../create-store'); import createStore from '../create-store';
const A = 0; const A = 0;
......
/* eslint-disable no-console */ /* eslint-disable no-console */
const redux = require('redux'); import * as redux from 'redux';
const debugMiddleware = require('../debug-middleware'); import debugMiddleware from '../debug-middleware';
function id(state) { function id(state) {
return state; return state;
......
const immutable = require('seamless-immutable'); import immutable from 'seamless-immutable';
const storeFactory = require('../index'); import * as annotationFixtures from '../../test/annotation-fixtures';
const annotationFixtures = require('../../test/annotation-fixtures'); import uiConstants from '../../ui-constants';
const uiConstants = require('../../ui-constants'); import storeFactory from '../index';
const defaultAnnotation = annotationFixtures.defaultAnnotation; const defaultAnnotation = annotationFixtures.defaultAnnotation;
const newAnnotation = annotationFixtures.newAnnotation; const newAnnotation = annotationFixtures.newAnnotation;
......
const { mount } = require('enzyme'); import { mount } from 'enzyme';
const { createStore } = require('redux'); import { createElement } from 'preact';
const { createElement } = require('preact'); import { act } from 'preact/test-utils';
const { act } = require('preact/test-utils'); import { createStore } from 'redux';
const useStore = require('../use-store'); import useStore, { $imports } from '../use-store';
const { $imports } = useStore;
const initialState = { value: 10, otherValue: 20 }; const initialState = { value: 10, otherValue: 20 };
const reducer = (state = initialState, action) => { const reducer = (state = initialState, action) => {
......
const util = require('../util'); import * as util from '../util';
const fixtures = { const fixtures = {
update: { update: {
......
/* global process */ /* global process */
const shallowEqual = require('shallowequal'); import { useEffect, useRef, useReducer } from 'preact/hooks';
const { useEffect, useRef, useReducer } = require('preact/hooks'); import shallowEqual from 'shallowequal';
const { useService } = require('../util/service-context'); import warnOnce from '../../shared/warn-once';
const warnOnce = require('../../shared/warn-once'); import { useService } from '../util/service-context';
/** /**
* Hook for accessing state or actions from the store inside a component. * Hook for accessing state or actions from the store inside a component.
...@@ -35,7 +35,7 @@ const warnOnce = require('../../shared/warn-once'); ...@@ -35,7 +35,7 @@ const warnOnce = require('../../shared/warn-once');
* and/or actions extracted from the store. * and/or actions extracted from the store.
* @return {T} - The result of `callback(store)` * @return {T} - The result of `callback(store)`
*/ */
function useStore(callback) { export default function useStore(callback) {
const store = useService('store'); const store = useService('store');
// Store the last-used callback in a ref so we can access it in the effect // Store the last-used callback in a ref so we can access it in the effect
...@@ -84,5 +84,3 @@ function useStore(callback) { ...@@ -84,5 +84,3 @@ function useStore(callback) {
return lastResult.current; return lastResult.current;
} }
module.exports = useStore;
/** /**
* Return an object where each key in `updateFns` is mapped to the key itself. * Return an object where each key in `updateFns` is mapped to the key itself.
*/ */
function actionTypes(updateFns) { export function actionTypes(updateFns) {
return Object.keys(updateFns).reduce(function(types, key) { return Object.keys(updateFns).reduce(function(types, key) {
types[key] = key; types[key] = key;
return types; return types;
...@@ -15,7 +15,7 @@ function actionTypes(updateFns) { ...@@ -15,7 +15,7 @@ function actionTypes(updateFns) {
* @param {Object} actionToUpdateFn - Object mapping action names to update * @param {Object} actionToUpdateFn - Object mapping action names to update
* functions. * functions.
*/ */
function createReducer(actionToUpdateFn) { export function createReducer(actionToUpdateFn) {
return (state = {}, action) => { return (state = {}, action) => {
const fn = actionToUpdateFn[action.type]; const fn = actionToUpdateFn[action.type];
if (!fn) { if (!fn) {
...@@ -39,7 +39,7 @@ function createReducer(actionToUpdateFn) { ...@@ -39,7 +39,7 @@ function createReducer(actionToUpdateFn) {
* level. The keys to this object are functions that call the original * level. The keys to this object are functions that call the original
* selectors with the `state` argument set to the current value of `getState()`. * selectors with the `state` argument set to the current value of `getState()`.
*/ */
function bindSelectors(namespaces, getState) { export function bindSelectors(namespaces, getState) {
const totalSelectors = {}; const totalSelectors = {};
Object.keys(namespaces).forEach(namespace => { Object.keys(namespaces).forEach(namespace => {
const selectors = namespaces[namespace].selectors; const selectors = namespaces[namespace].selectors;
...@@ -53,9 +53,3 @@ function bindSelectors(namespaces, getState) { ...@@ -53,9 +53,3 @@ function bindSelectors(namespaces, getState) {
}); });
return totalSelectors; return totalSelectors;
} }
module.exports = {
actionTypes,
bindSelectors,
createReducer,
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment