From c6b7a7ca8a9a4e2f450558b9cf8dcdbe4c9a1a39 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 15 Mar 2021 17:29:48 -0500 Subject: [PATCH] Store admin log in reducer --- app/soapbox/features/admin/moderation_log.js | 20 ++++++--- app/soapbox/reducers/admin_log.js | 43 ++++++++++++++++++++ app/soapbox/reducers/index.js | 2 + 3 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 app/soapbox/reducers/admin_log.js diff --git a/app/soapbox/features/admin/moderation_log.js b/app/soapbox/features/admin/moderation_log.js index e84fd6ed6..3c6d5d7fe 100644 --- a/app/soapbox/features/admin/moderation_log.js +++ b/app/soapbox/features/admin/moderation_log.js @@ -1,41 +1,49 @@ import React from 'react'; import { defineMessages, injectIntl } from 'react-intl'; import { connect } from 'react-redux'; +import ImmutablePropTypes from 'react-immutable-proptypes'; import ImmutablePureComponent from 'react-immutable-pure-component'; import PropTypes from 'prop-types'; import Column from '../ui/components/column'; import ScrollableList from 'soapbox/components/scrollable_list'; import { fetchModerationLog } from 'soapbox/actions/admin'; -import { List as ImmutableList, fromJS } from 'immutable'; const messages = defineMessages({ heading: { id: 'column.admin.moderation_log', defaultMessage: 'Moderation Log' }, emptyMessage: { id: 'admin.moderation_log.empty_message', defaultMessage: 'You have not performed any moderation actions yet. When you do, a history will be shown here.' }, }); -export default @connect() +const mapStateToProps = state => ({ + items: state.getIn(['admin_log', 'index']).map(i => state.getIn(['admin_log', 'items', String(i)])), +}); + +export default @connect(mapStateToProps) @injectIntl class ModerationLog extends ImmutablePureComponent { static propTypes = { intl: PropTypes.object.isRequired, + list: ImmutablePropTypes.list, }; state = { isLoading: true, - items: ImmutableList(), + lastPage: 0, } componentDidMount() { const { dispatch } = this.props; dispatch(fetchModerationLog()) - .then(data => this.setState({ isLoading: false, items: fromJS(data.items) })) + .then(data => this.setState({ + isLoading: false, + lastPage: 1, + })) .catch(() => {}); } render() { - const { intl } = this.props; - const { isLoading, items } = this.state; + const { intl, items } = this.props; + const { isLoading } = this.state; const showLoading = isLoading && items.count() === 0; return ( diff --git a/app/soapbox/reducers/admin_log.js b/app/soapbox/reducers/admin_log.js new file mode 100644 index 000000000..874cab373 --- /dev/null +++ b/app/soapbox/reducers/admin_log.js @@ -0,0 +1,43 @@ +import { ADMIN_LOG_FETCH_SUCCESS } from 'soapbox/actions/admin'; +import { + Map as ImmutableMap, + OrderedSet as ImmutableOrderedSet, + fromJS, +} from 'immutable'; + +const initialState = ImmutableMap({ + items: ImmutableMap(), + index: ImmutableOrderedSet(), + total: 0, +}); + +const parseItems = items => { + let ids = []; + let map = {}; + + items.forEach(item => { + ids.push(item.id); + map[item.id] = item; + }); + + return { ids: ids, map: map }; +}; + +const importItems = (state, items, total) => { + const { ids, map } = parseItems(items); + + return state.withMutations(state => { + state.update('index', v => v.union(ids)); + state.update('items', v => v.merge(fromJS(map))); + state.set('total', total); + }); +}; + +export default function admin_log(state = initialState, action) { + switch(action.type) { + case ADMIN_LOG_FETCH_SUCCESS: + return importItems(state, action.items, action.total); + default: + return state; + } +}; diff --git a/app/soapbox/reducers/index.js b/app/soapbox/reducers/index.js index e8cb4821b..45ab583d0 100644 --- a/app/soapbox/reducers/index.js +++ b/app/soapbox/reducers/index.js @@ -50,6 +50,7 @@ import chat_messages from './chat_messages'; import chat_message_lists from './chat_message_lists'; import profile_hover_card from './profile_hover_card'; import backups from './backups'; +import admin_log from './admin_log'; const appReducer = combineReducers({ dropdown_menu, @@ -101,6 +102,7 @@ const appReducer = combineReducers({ chat_message_lists, profile_hover_card, backups, + admin_log, }); // Clear the state (mostly) when the user logs out