Skip to content

Commit

Permalink
Simplify state with useReducer() in container
Browse files Browse the repository at this point in the history
  • Loading branch information
tnajdek committed Jun 8, 2021
1 parent 019dc40 commit 89ad971
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 146 deletions.
45 changes: 45 additions & 0 deletions src/js/actions.js
@@ -0,0 +1,45 @@
import { CONFIRM_CURRENT_STYLE, REQUEST_FETCH_STYLE, RECEIVE_FETCH_STYLE, ERROR_FETCH_STYLE,
SELECT_CURRENT_STYLE } from './constants/actions';
import { fetchWithCachedFallback } from './utils';
import { getStyleProperties } from './get-style-properties';

const stylesCache = new Map();

const fetchAndSelectStyle = async (dispatch, styleName, opts = {}) => {
dispatch({ type: REQUEST_FETCH_STYLE, styleName });
let nextStyleName = styleName, styleXml, styleProps;

do {
if(stylesCache.has(styleName)) {
styleXml = stylesCache.get(styleName);
} else {
const url = `https://www.zotero.org/styles/${styleName}`;
try {
const response = await fetchWithCachedFallback(url);
if(!response.ok) {
throw new Error(`Failed to fetch ${styleName} from ${url}`);
}
styleXml = await response.text();
} catch(error) {
dispatch({ type: ERROR_FETCH_STYLE, styleName, error });
throw error;
}
stylesCache.set(styleName, styleXml);
}
styleProps = getStyleProperties(styleXml);
const { parentStyleName } = styleProps
nextStyleName = parentStyleName;
} while(nextStyleName);

dispatch({
type: RECEIVE_FETCH_STYLE, styleName, styleXml, styleProps, ...opts
});

dispatch({
type: SELECT_CURRENT_STYLE, styleName, styleXml, styleProps, ...opts
});
}

const confirmStyle = dispatch => dispatch({ type: CONFIRM_CURRENT_STYLE });

export { confirmStyle, fetchAndSelectStyle };

0 comments on commit 89ad971

Please sign in to comment.