From 120719b19e55b267aab998d4cf2e557b8e2e99ac Mon Sep 17 00:00:00 2001 From: David Mason Date: Mon, 25 Sep 2017 13:02:43 +1000 Subject: [PATCH] refactor(actions): make PHRASE_SUGGESTIONS_UPDATED an FSA --- .../actions/suggestions-action-types.js | 2 +- .../app/editor/actions/suggestions-actions.js | 14 +--- .../editor/reducers/suggestions-reducer.js | 10 +-- .../reducers/suggestions-reducer.test.js | 80 ++++++++++--------- 4 files changed, 50 insertions(+), 56 deletions(-) diff --git a/server/zanata-frontend/src/frontend/app/editor/actions/suggestions-action-types.js b/server/zanata-frontend/src/frontend/app/editor/actions/suggestions-action-types.js index 5d306143784..c39fb3d8db0 100644 --- a/server/zanata-frontend/src/frontend/app/editor/actions/suggestions-action-types.js +++ b/server/zanata-frontend/src/frontend/app/editor/actions/suggestions-action-types.js @@ -12,7 +12,7 @@ export const PHRASE_SUGGESTION_FINISHED_COPYING = 'PHRASE_SUGGESTION_FINISHED_COPYING' export const TEXT_SUGGESTIONS_UPDATED = 'TEXT_SUGGESTIONS_UPDATED' export const SUGGESTION_SEARCH_TEXT_CHANGE = 'SUGGESTION_SEARCH_TEXT_CHANGE' -export const PHRASE_SUGGESTIONS_UPDATED = Symbol('PHRASE_SUGGESTIONS_UPDATED') +export const PHRASE_SUGGESTIONS_UPDATED = 'PHRASE_SUGGESTIONS_UPDATED' export const SUGGESTION_PANEL_HEIGHT_CHANGE = Symbol('SUGGESTION_PANEL_HEIGHT_CHANGE') export const SHOW_DETAIL_FOR_SUGGESTION_BY_INDEX = diff --git a/server/zanata-frontend/src/frontend/app/editor/actions/suggestions-actions.js b/server/zanata-frontend/src/frontend/app/editor/actions/suggestions-actions.js index 9d8af0f1cb2..322a1949e6c 100644 --- a/server/zanata-frontend/src/frontend/app/editor/actions/suggestions-actions.js +++ b/server/zanata-frontend/src/frontend/app/editor/actions/suggestions-actions.js @@ -235,6 +235,8 @@ export function findPhraseSuggestionsById (phraseId) { const PHRASE_SEARCH_STALE_AGE_MILLIS = 60000 +const phraseSuggestionsUpdated = createAction(PHRASE_SUGGESTIONS_UPDATED) + export function findPhraseSuggestions (phrase) { return (dispatch, getState) => { const phraseId = phrase.id @@ -282,18 +284,6 @@ export function findPhraseSuggestions (phrase) { } } -export function phraseSuggestionsUpdated ( - {phraseId, loading, searchStrings, suggestions, timestamp}) { - return { - type: PHRASE_SUGGESTIONS_UPDATED, - phraseId, - loading, - searchStrings, - suggestions, - timestamp - } -} - export function saveSuggestionPanelHeight (percentageHeight) { return { type: SUGGESTION_PANEL_HEIGHT_CHANGE, diff --git a/server/zanata-frontend/src/frontend/app/editor/reducers/suggestions-reducer.js b/server/zanata-frontend/src/frontend/app/editor/reducers/suggestions-reducer.js index 0848c498e25..42df6e7590a 100644 --- a/server/zanata-frontend/src/frontend/app/editor/reducers/suggestions-reducer.js +++ b/server/zanata-frontend/src/frontend/app/editor/reducers/suggestions-reducer.js @@ -69,11 +69,11 @@ const suggestions = (state = defaultState, action) => { return update({ searchByPhrase: { // must $set a new object because the key may not yet be defined - [action.phraseId]: {$set: { - loading: action.loading, - searchStrings: action.searchStrings, - suggestions: action.suggestions, - timestamp: action.timestamp + [action.payload.phraseId]: {$set: { + loading: action.payload.loading, + searchStrings: action.payload.searchStrings, + suggestions: action.payload.suggestions, + timestamp: action.payload.timestamp }} } }) diff --git a/server/zanata-frontend/src/frontend/app/editor/reducers/suggestions-reducer.test.js b/server/zanata-frontend/src/frontend/app/editor/reducers/suggestions-reducer.test.js index aabbd9f82b4..9aca654313b 100644 --- a/server/zanata-frontend/src/frontend/app/editor/reducers/suggestions-reducer.test.js +++ b/server/zanata-frontend/src/frontend/app/editor/reducers/suggestions-reducer.test.js @@ -48,25 +48,27 @@ describe('suggestions-reducer test', () => { it('can begin and end phrase copying', () => { const withPhraseSuggestions = suggestionsReducer(undefined, { type: PHRASE_SUGGESTIONS_UPDATED, - phraseId: 'p01', - loading: false, - searchStrings: [ 'source', 'sources' ], - suggestions: [ - { - sourceContents: [ 'source', 'sources' ], - targetContents: [ 'target', 'targets' ], - copying: false, - relevanceScore: 2, - similarityPercent: 100 - }, { - sourceContents: [ 'horse', 'horses' ], - targetContents: [ 'Pferd', 'Pferde' ], - copying: false, - relevanceScore: 1.5, - similarityPercent: 80 - } - ], - timestamp: 12345 + payload: { + phraseId: 'p01', + loading: false, + searchStrings: [ 'source', 'sources' ], + suggestions: [ + { + sourceContents: [ 'source', 'sources' ], + targetContents: [ 'target', 'targets' ], + copying: false, + relevanceScore: 2, + similarityPercent: 100 + }, { + sourceContents: [ 'horse', 'horses' ], + targetContents: [ 'Pferd', 'Pferde' ], + copying: false, + relevanceScore: 1.5, + similarityPercent: 80 + } + ], + timestamp: 12345 + } }) const copying = suggestionsReducer(withPhraseSuggestions, { type: PHRASE_SUGGESTION_STARTED_COPYING, @@ -90,25 +92,27 @@ describe('suggestions-reducer test', () => { it('can receive phrase suggestions', () => { const withPhraseSuggestions = suggestionsReducer(undefined, { type: PHRASE_SUGGESTIONS_UPDATED, - phraseId: 'p01', - loading: false, - searchStrings: [ 'source', 'sources' ], - suggestions: [ - { - sourceContents: [ 'source', 'sources' ], - targetContents: [ 'target', 'targets' ], - copying: false, - relevanceScore: 2, - similarityPercent: 100 - }, { - sourceContents: [ 'horse', 'horses' ], - targetContents: [ 'Pferd', 'Pferde' ], - copying: false, - relevanceScore: 1.5, - similarityPercent: 80 - } - ], - timestamp: 12345 + payload: { + phraseId: 'p01', + loading: false, + searchStrings: [ 'source', 'sources' ], + suggestions: [ + { + sourceContents: [ 'source', 'sources' ], + targetContents: [ 'target', 'targets' ], + copying: false, + relevanceScore: 2, + similarityPercent: 100 + }, { + sourceContents: [ 'horse', 'horses' ], + targetContents: [ 'Pferd', 'Pferde' ], + copying: false, + relevanceScore: 1.5, + similarityPercent: 80 + } + ], + timestamp: 12345 + } }) expect(withPhraseSuggestions.searchByPhrase['p01']).toEqual({ loading: false,