File size: 3,073 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { get, includes, map, without } from 'lodash';
import {
	COMMENTS_CHANGE_STATUS,
	COMMENTS_DELETE,
	COMMENTS_LIST_REQUEST,
	COMMENTS_QUERY_UPDATE,
} from 'calypso/state/action-types';
import { getFiltersKey } from 'calypso/state/comments/ui/utils';
import { getRequestKey } from 'calypso/state/data-layer/wpcom-http/utils';
import { combineReducers, keyedReducer } from 'calypso/state/utils';

const deepUpdateComments = ( state, comments, query ) => {
	const { page = 1, postId } = query;
	const parent = postId || 'site';
	const filter = getFiltersKey( query );

	const parentObject = get( state, parent, {} );
	const filterObject = get( parentObject, filter, {} );

	return {
		...state,
		[ parent ]: {
			...parentObject,
			[ filter ]: {
				...filterObject,
				[ page ]: comments,
			},
		},
	};
};

const sortDescending = function ( a, b ) {
	if ( a < b ) {
		return 1;
	}
	if ( a > b ) {
		return -1;
	}
	return 0;
};

const sortAscending = function ( a, b ) {
	if ( a < b ) {
		return -1;
	}
	if ( a > b ) {
		return 1;
	}
	return 0;
};

export const queries = ( state = {}, action ) => {
	switch ( action.type ) {
		case COMMENTS_CHANGE_STATUS:
		case COMMENTS_DELETE: {
			const query = action.refreshCommentListQuery;
			if ( ! query ) {
				return state;
			}
			const { page, postId, status } = query;
			const parent = postId || 'site';
			const filter = getFiltersKey( query );
			const comments = get( state, [ parent, filter, page ] );

			if (
				COMMENTS_CHANGE_STATUS === action.type &&
				'all' === status &&
				includes( comments, action.commentId ) && // if the comment is not in the current view this is an undo
				[ 'approved', 'unapproved' ].includes( action.status )
			) {
				// No-op when status changes from `approved` or `unapproved` in the All tab
				return state;
			}

			if (
				status === action.status ||
				( status === 'all' && [ 'approved', 'unapproved' ].includes( action.status ) )
			) {
				//with undo, we should add this back to the list
				const sortedList = [ action.commentId ]
					.concat( comments )
					.sort( query.order === 'DESC' ? sortDescending : sortAscending );
				return deepUpdateComments( state, sortedList, query );
			}
			return deepUpdateComments( state, without( comments, action.commentId ), query );
		}
		case COMMENTS_QUERY_UPDATE:
			return typeof get( action, 'query.page' ) === 'undefined'
				? state
				: deepUpdateComments( state, map( action.comments, 'ID' ), action.query );
		default:
			return state;
	}
};

export const pendingActions = function ( state = [], action ) {
	switch ( action.type ) {
		case COMMENTS_CHANGE_STATUS:
		case COMMENTS_DELETE: {
			const key = getRequestKey( action );
			if ( action.meta?.dataLayer?.trackRequest && ! state.includes( key ) ) {
				return [ ...state, key ];
			}
			return state;
		}
		case COMMENTS_LIST_REQUEST:
			//ignore pending requests if we're looking at a fresh view
			return [];
		default:
			return state;
	}
};

export default combineReducers( {
	queries: keyedReducer( 'siteId', queries ),
	pendingActions,
} );