Commit dda46a72 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate activity store module to TypeScript

parent fc5b7603
...@@ -2,51 +2,46 @@ ...@@ -2,51 +2,46 @@
* Store module which tracks activity happening in the application that may * Store module which tracks activity happening in the application that may
* need to be reflected in the UI. * need to be reflected in the UI.
*/ */
import type { Annotation } from '../../../types/api';
import { createStoreModule, makeAction } from '../create-store'; import { createStoreModule, makeAction } from '../create-store';
/** @typedef {import('../../../types/api').Annotation} Annotation */ export type State = {
const initialState = {
/** /**
* Annotation `$tag`s that correspond to annotations with active API requests * Annotation `$tag`s that correspond to annotations with active API requests
*
* @type {string[]}
*/ */
activeAnnotationSaveRequests: [], activeAnnotationSaveRequests: string[];
/** The number of API requests that have started and not yet completed. */
activeApiRequests: number;
/** The number of annotation fetches that have started and not yet completed. */
activeAnnotationFetches: number;
/** Have annotations ever been fetched? */
hasFetchedAnnotations: boolean;
/** /**
* The number of API requests that have started and not yet completed. * The number of total annotation results the service reported as
* matching the most recent load/search request
*/ */
annotationResultCount: number | null;
};
const initialState: State = {
activeAnnotationSaveRequests: [],
activeApiRequests: 0, activeApiRequests: 0,
/**
* The number of annotation fetches that have started and not yet completed.
*/
activeAnnotationFetches: 0, activeAnnotationFetches: 0,
/**
* Have annotations ever been fetched?
*/
hasFetchedAnnotations: false, hasFetchedAnnotations: false,
/**
* The number of total annotation results the service reported as
* matching the most recent load/search request
*
* @type {number|null}
*/
annotationResultCount: null, annotationResultCount: null,
}; };
/** @typedef {typeof initialState} State */
const reducers = { const reducers = {
/** @param {State} state */ API_REQUEST_STARTED(state: State) {
API_REQUEST_STARTED(state) {
return { return {
...state, ...state,
activeApiRequests: state.activeApiRequests + 1, activeApiRequests: state.activeApiRequests + 1,
}; };
}, },
/** @param {State} state */ API_REQUEST_FINISHED(state: State) {
API_REQUEST_FINISHED(state) {
if (state.activeApiRequests === 0) { if (state.activeApiRequests === 0) {
throw new Error( throw new Error(
'API_REQUEST_FINISHED action when no requests were active' 'API_REQUEST_FINISHED action when no requests were active'
...@@ -59,12 +54,8 @@ const reducers = { ...@@ -59,12 +54,8 @@ const reducers = {
}; };
}, },
/** ANNOTATION_SAVE_STARTED(state: State, action: { annotation: Annotation }) {
* @param {State} state const addToStarted = [];
* @param {{ annotation: Annotation }} action
*/
ANNOTATION_SAVE_STARTED(state, action) {
let addToStarted = [];
if ( if (
action.annotation.$tag && action.annotation.$tag &&
!state.activeAnnotationSaveRequests.includes(action.annotation.$tag) !state.activeAnnotationSaveRequests.includes(action.annotation.$tag)
...@@ -79,11 +70,7 @@ const reducers = { ...@@ -79,11 +70,7 @@ const reducers = {
}; };
}, },
/** ANNOTATION_SAVE_FINISHED(state: State, action: { annotation: Annotation }) {
* @param {State} state
* @param {{ annotation: Annotation }} action
*/
ANNOTATION_SAVE_FINISHED(state, action) {
const updatedSaves = state.activeAnnotationSaveRequests.filter( const updatedSaves = state.activeAnnotationSaveRequests.filter(
$tag => $tag !== action.annotation.$tag $tag => $tag !== action.annotation.$tag
); );
...@@ -93,16 +80,14 @@ const reducers = { ...@@ -93,16 +80,14 @@ const reducers = {
}; };
}, },
/** @param {State} state */ ANNOTATION_FETCH_STARTED(state: State) {
ANNOTATION_FETCH_STARTED(state) {
return { return {
...state, ...state,
activeAnnotationFetches: state.activeAnnotationFetches + 1, activeAnnotationFetches: state.activeAnnotationFetches + 1,
}; };
}, },
/** @param {State} state */ ANNOTATION_FETCH_FINISHED(state: State) {
ANNOTATION_FETCH_FINISHED(state) {
if (state.activeAnnotationFetches === 0) { if (state.activeAnnotationFetches === 0) {
throw new Error( throw new Error(
'ANNOTATION_FETCH_FINISHED action when no annotation fetches were active' 'ANNOTATION_FETCH_FINISHED action when no annotation fetches were active'
...@@ -116,11 +101,7 @@ const reducers = { ...@@ -116,11 +101,7 @@ const reducers = {
}; };
}, },
/** SET_ANNOTATION_RESULT_COUNT(state: State, action: { resultCount: number }) {
* @param {State} state
* @param {{ resultCount: number }} action
*/
SET_ANNOTATION_RESULT_COUNT(state, action) {
return { return {
annotationResultCount: action.resultCount, annotationResultCount: action.resultCount,
}; };
...@@ -135,17 +116,11 @@ function annotationFetchFinished() { ...@@ -135,17 +116,11 @@ function annotationFetchFinished() {
return makeAction(reducers, 'ANNOTATION_FETCH_FINISHED', undefined); return makeAction(reducers, 'ANNOTATION_FETCH_FINISHED', undefined);
} }
/** function annotationSaveStarted(annotation: Annotation) {
* @param {Annotation} annotation — annotation object with a `$tag` property
*/
function annotationSaveStarted(annotation) {
return makeAction(reducers, 'ANNOTATION_SAVE_STARTED', { annotation }); return makeAction(reducers, 'ANNOTATION_SAVE_STARTED', { annotation });
} }
/** function annotationSaveFinished(annotation: Annotation) {
* @param {Annotation} annotation — annotation object with a `$tag` property
*/
function annotationSaveFinished(annotation) {
return makeAction(reducers, 'ANNOTATION_SAVE_FINISHED', { annotation }); return makeAction(reducers, 'ANNOTATION_SAVE_FINISHED', { annotation });
} }
...@@ -157,39 +132,32 @@ function apiRequestFinished() { ...@@ -157,39 +132,32 @@ function apiRequestFinished() {
return makeAction(reducers, 'API_REQUEST_FINISHED', undefined); return makeAction(reducers, 'API_REQUEST_FINISHED', undefined);
} }
/** @param {number} resultCount */ function setAnnotationResultCount(resultCount: number) {
function setAnnotationResultCount(resultCount) {
return makeAction(reducers, 'SET_ANNOTATION_RESULT_COUNT', { resultCount }); return makeAction(reducers, 'SET_ANNOTATION_RESULT_COUNT', { resultCount });
} }
/** Selectors */ /** Selectors */
/** @param {State} state */ function annotationResultCount(state: State) {
function annotationResultCount(state) {
return state.annotationResultCount; return state.annotationResultCount;
} }
/** @param {State} state */ function hasFetchedAnnotations(state: State) {
function hasFetchedAnnotations(state) {
return state.hasFetchedAnnotations; return state.hasFetchedAnnotations;
} }
/** /**
* Return true when annotations are actively being fetched. * Return true when annotations are actively being fetched.
*
* @param {State} state
*/ */
function isFetchingAnnotations(state) { function isFetchingAnnotations(state: State) {
return state.activeAnnotationFetches > 0; return state.activeAnnotationFetches > 0;
} }
/** /**
* Return true when any activity is happening in the app that needs to complete * Return true when any activity is happening in the app that needs to complete
* before the UI is ready for interactivity with annotations. * before the UI is ready for interactivity with annotations.
*
* @param {State} state
*/ */
function isLoading(state) { function isLoading(state: State) {
return state.activeApiRequests > 0 || !state.hasFetchedAnnotations; return state.activeApiRequests > 0 || !state.hasFetchedAnnotations;
} }
...@@ -197,11 +165,8 @@ function isLoading(state) { ...@@ -197,11 +165,8 @@ function isLoading(state) {
* Return `true` if `$tag` exists in the array of annotation `$tag`s that * Return `true` if `$tag` exists in the array of annotation `$tag`s that
* have in-flight save requests, i.e. the annotation in question is actively * have in-flight save requests, i.e. the annotation in question is actively
* being saved to a remote service. * being saved to a remote service.
*
* @param {State} state
* @param {Annotation} annotation
*/ */
function isSavingAnnotation(state, annotation) { function isSavingAnnotation(state: State, annotation: Annotation) {
if (!annotation.$tag) { if (!annotation.$tag) {
return false; return false;
} }
......
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