-
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic redux integration for thread component
- Loading branch information
1 parent
dcd97a9
commit 89e5033
Showing
11 changed files
with
171 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { LS_COLLAPSE_KEY } from 'common/constants'; | ||
|
||
const getCollapsedComments = () => JSON.parse(localStorage.getItem(LS_COLLAPSE_KEY) || '[]'); | ||
|
||
export default getCollapsedComments; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
import { collapsedThreads } from './thread.reducers'; | ||
|
||
export const threadReducers = { collapsedThreads }; | ||
|
||
export { default } from './thread'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { LS_COLLAPSE_KEY } from 'common/constants'; | ||
|
||
const saveCollapsedComments = comments => localStorage.setItem(LS_COLLAPSE_KEY, JSON.stringify(comments)); | ||
|
||
export default saveCollapsedComments; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const THREAD_SET_COLLAPSE = 'THREAD/COLLAPSE_SET'; | ||
export const setCollapse = (comment, collapsed) => ({ | ||
type: THREAD_SET_COLLAPSE, | ||
comment, | ||
collapsed, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import store from 'common/store'; | ||
|
||
export const getThreadIsCollapsed = (state, comment) => { | ||
let collapsed = state.collapsedThreads[comment.id]; | ||
|
||
if (collapsed !== null && collapsed !== undefined) { | ||
return collapsed; | ||
} | ||
|
||
const config = store.get('config') || {}; | ||
const score = comment.score || 0; | ||
|
||
return score <= config.critical_score; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { THREAD_SET_COLLAPSE } from './thread.actions'; | ||
import getCollapsedComments from './getCollapsedComments'; | ||
import saveCollapsedComments from './saveCollapsedComments'; | ||
import { siteId, url } from 'common/settings'; | ||
|
||
const collapsedCommentIds = getCollapsedComments() | ||
.map(comment => comment.split('_')) | ||
.filter(components => components[0] === siteId && components[1] === url) | ||
.map(component => component[2]); | ||
|
||
const initialState = collapsedCommentIds.reduce((acc, id) => ({ ...acc, [id]: true }), {}); | ||
|
||
const persistCollapsedComments = (comment, collapsed) => { | ||
const lsCollapsedID = `${siteId}_${url}_${comment.id}`; | ||
let collapsedComments = getCollapsedComments(); | ||
|
||
if (collapsed) { | ||
collapsedComments = [...new Set(collapsedComments.concat(lsCollapsedID))]; | ||
} else { | ||
collapsedComments = collapsedComments.filter(id => id !== lsCollapsedID); | ||
} | ||
|
||
saveCollapsedComments(collapsedComments); | ||
}; | ||
|
||
export const collapsedThreads = (state = initialState, action) => { | ||
switch (action.type) { | ||
case THREAD_SET_COLLAPSE: { | ||
const currentCollapsed = state[action.comment.id]; | ||
|
||
if (action.collapsed !== currentCollapsed) { | ||
persistCollapsedComments(action.comment, action.collapsed); | ||
} | ||
|
||
return { | ||
...state, | ||
[action.comment.id]: action.collapsed, | ||
}; | ||
} | ||
default: | ||
return state; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { setCollapse } from './thread.actions'; | ||
import { collapsedThreads } from './thread.reducers'; | ||
import { getThreadIsCollapsed } from './thread.getters'; | ||
import getCollapsedComments from './getCollapsedComments'; | ||
|
||
describe('collapsedThreads', () => { | ||
const comment = { id: 1 }; | ||
|
||
it('should set collapsed to true', () => { | ||
const collapsed = true; | ||
const action = setCollapse(comment, collapsed); | ||
|
||
const newState = { | ||
collapsedThreads: collapsedThreads({}, action), | ||
}; | ||
|
||
expect(getThreadIsCollapsed(newState, comment)).toEqual(collapsed); | ||
expect(getCollapsedComments().length).toEqual(1); | ||
}); | ||
|
||
it('should set collapsed to false', () => { | ||
const collapsed = false; | ||
const action = setCollapse(comment, collapsed); | ||
|
||
const newState = { | ||
collapsedThreads: collapsedThreads({}, action), | ||
}; | ||
|
||
expect(getThreadIsCollapsed(newState, comment)).toEqual(collapsed); | ||
expect(getCollapsedComments().length).toEqual(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { createStore } from 'redux'; | ||
import { combineReducers } from 'redux'; | ||
|
||
import { threadReducers } from './components/thread'; | ||
|
||
const rootReducer = combineReducers({ | ||
...threadReducers, | ||
}); | ||
|
||
const store = createStore(rootReducer); | ||
|
||
export default store; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters