@ -10,14 +10,14 @@ import {
ANNOUNCEMENTS _REACTION _REMOVE _FAIL ,
ANNOUNCEMENTS _REACTION _REMOVE _FAIL ,
ANNOUNCEMENTS _TOGGLE _SHOW ,
ANNOUNCEMENTS _TOGGLE _SHOW ,
ANNOUNCEMENTS _DELETE ,
ANNOUNCEMENTS _DELETE ,
ANNOUNCEMENTS _DISMISS _SUCCESS ,
} from '../actions/announcements' ;
} from '../actions/announcements' ;
import { Map as ImmutableMap , List as ImmutableList , Set as ImmutableSet , fromJS } from 'immutable' ;
import { Map as ImmutableMap , List as ImmutableList , fromJS } from 'immutable' ;
const initialState = ImmutableMap ( {
const initialState = ImmutableMap ( {
items : ImmutableList ( ) ,
items : ImmutableList ( ) ,
isLoading : false ,
isLoading : false ,
show : false ,
show : false ,
unread : ImmutableSet ( ) ,
} ) ;
} ) ;
const updateReaction = ( state , id , name , updater ) => state . update ( 'items' , list => list . map ( announcement => {
const updateReaction = ( state , id , name , updater ) => state . update ( 'items' , list => list . map ( announcement => {
@ -42,24 +42,11 @@ const addReaction = (state, id, name) => updateReaction(state, id, name, x => x.
const removeReaction = ( state , id , name ) => updateReaction ( state , id , name , x => x . set ( 'me' , false ) . update ( 'count' , y => y - 1 ) ) ;
const removeReaction = ( state , id , name ) => updateReaction ( state , id , name , x => x . set ( 'me' , false ) . update ( 'count' , y => y - 1 ) ) ;
const addUnread = ( state , items ) => {
if ( state . get ( 'show' ) ) {
return state ;
}
const newIds = ImmutableSet ( items . map ( x => x . get ( 'id' ) ) ) ;
const oldIds = ImmutableSet ( state . get ( 'items' ) . map ( x => x . get ( 'id' ) ) ) ;
return state . update ( 'unread' , unread => unread . union ( newIds . subtract ( oldIds ) ) ) ;
} ;
const sortAnnouncements = list => list . sortBy ( x => x . get ( 'starts_at' ) || x . get ( 'published_at' ) ) ;
const sortAnnouncements = list => list . sortBy ( x => x . get ( 'starts_at' ) || x . get ( 'published_at' ) ) ;
const updateAnnouncement = ( state , announcement ) => {
const updateAnnouncement = ( state , announcement ) => {
const idx = state . get ( 'items' ) . findIndex ( x => x . get ( 'id' ) === announcement . get ( 'id' ) ) ;
const idx = state . get ( 'items' ) . findIndex ( x => x . get ( 'id' ) === announcement . get ( 'id' ) ) ;
state = addUnread ( state , [ announcement ] ) ;
if ( idx > - 1 ) {
if ( idx > - 1 ) {
// Deep merge is used because announcements from the streaming API do not contain
// Deep merge is used because announcements from the streaming API do not contain
// personalized data about which reactions have been selected by the given user,
// personalized data about which reactions have been selected by the given user,
@ -74,7 +61,6 @@ export default function announcementsReducer(state = initialState, action) {
switch ( action . type ) {
switch ( action . type ) {
case ANNOUNCEMENTS _TOGGLE _SHOW :
case ANNOUNCEMENTS _TOGGLE _SHOW :
return state . withMutations ( map => {
return state . withMutations ( map => {
if ( ! map . get ( 'show' ) ) map . set ( 'unread' , ImmutableSet ( ) ) ;
map . set ( 'show' , ! map . get ( 'show' ) ) ;
map . set ( 'show' , ! map . get ( 'show' ) ) ;
} ) ;
} ) ;
case ANNOUNCEMENTS _FETCH _REQUEST :
case ANNOUNCEMENTS _FETCH _REQUEST :
@ -83,10 +69,6 @@ export default function announcementsReducer(state = initialState, action) {
return state . withMutations ( map => {
return state . withMutations ( map => {
const items = fromJS ( action . announcements ) ;
const items = fromJS ( action . announcements ) ;
map . set ( 'unread' , ImmutableSet ( ) ) ;
addUnread ( map , items ) ;
map . set ( 'items' , items ) ;
map . set ( 'items' , items ) ;
map . set ( 'isLoading' , false ) ;
map . set ( 'isLoading' , false ) ;
} ) ;
} ) ;
@ -102,8 +84,10 @@ export default function announcementsReducer(state = initialState, action) {
case ANNOUNCEMENTS _REACTION _REMOVE _REQUEST :
case ANNOUNCEMENTS _REACTION _REMOVE _REQUEST :
case ANNOUNCEMENTS _REACTION _ADD _FAIL :
case ANNOUNCEMENTS _REACTION _ADD _FAIL :
return removeReaction ( state , action . id , action . name ) ;
return removeReaction ( state , action . id , action . name ) ;
case ANNOUNCEMENTS _DISMISS _SUCCESS :
return updateAnnouncement ( state , fromJS ( { 'id' : action . id , 'read' : true } ) ) ;
case ANNOUNCEMENTS _DELETE :
case ANNOUNCEMENTS _DELETE :
return state . update ( 'unread' , set => set . delete ( action . id ) ) . update ( 'items' , list => {
return state . update ( 'items' , list => {
const idx = list . findIndex ( x => x . get ( 'id' ) === action . id ) ;
const idx = list . findIndex ( x => x . get ( 'id' ) === action . id ) ;
if ( idx > - 1 ) {
if ( idx > - 1 ) {