File size: 2,024 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
import { combineReducers } from 'redux';
import * as types from '../action-types';

export const allNotes = ( state = {}, { type, notes, noteIds } ) => {
	if ( types.NOTES_ADD === type ) {
		return {
			...state,
			// Transform the notes array into an object with IDs as the keys
			...Object.fromEntries( notes.map( ( note ) => [ note.id, note ] ) ),
		};
	}

	if ( types.NOTES_REMOVE === type ) {
		const nextState = { ...state };
		noteIds.forEach( ( id ) => delete nextState[ id ] );
		return nextState;
	}

	return state;
};

export const hiddenNoteIds = ( state = {}, { type, noteId } ) => {
	if ( types.TRASH_NOTE === type || types.SPAM_NOTE === type ) {
		return { ...state, [ noteId ]: true };
	}

	if ( types.UNDO_ACTION === type ) {
		const nextState = { ...state };
		delete nextState[ noteId ];
		return nextState;
	}

	return state;
};

export const noteApprovals = ( state = {}, { type, noteId, isApproved } ) => {
	if ( types.APPROVE_NOTE === type ) {
		return { ...state, [ noteId ]: isApproved };
	}

	if ( types.RESET_LOCAL_APPROVAL === type ) {
		const nextState = { ...state };
		delete nextState[ noteId ];
		return nextState;
	}

	return state;
};

export const noteLikes = ( state = {}, { type, noteId, isLiked } ) => {
	if ( types.LIKE_NOTE === type ) {
		return { ...state, [ noteId ]: isLiked };
	}

	if ( types.RESET_LOCAL_LIKE === type ) {
		const nextState = { ...state };
		delete nextState[ noteId ];
		return nextState;
	}

	return state;
};

export const noteReads = ( state = {}, { type, noteId } ) => {
	if ( ( types.READ_NOTE === type || types.SELECT_NOTE === type ) && noteId ) {
		return { ...state, [ noteId ]: true };
	}

	return state;
};

export const filteredNoteReads = ( state = [], { type, noteId } ) => {
	if ( types.SELECT_NOTE === type ) {
		return [ ...state, noteId ];
	}

	if ( types.SET_FILTER === type ) {
		return [];
	}

	return state;
};

export default combineReducers( {
	allNotes,
	hiddenNoteIds,
	noteApprovals,
	noteLikes,
	noteReads,
	filteredNoteReads,
} );