Rewrite actions/app.ts and reducers/missed_updates.ts with createAction (#24801)

lolsob-rspec
fusagiko / takayamaki 2023-05-03 22:28:39 +09:00 committed by GitHub
parent a4e3f98fc7
commit 9ed91c444e
6 changed files with 45 additions and 42 deletions

View File

@ -1,17 +0,0 @@
export const APP_FOCUS = 'APP_FOCUS';
export const APP_UNFOCUS = 'APP_UNFOCUS';
export const focusApp = () => ({
type: APP_FOCUS,
});
export const unfocusApp = () => ({
type: APP_UNFOCUS,
});
export const APP_LAYOUT_CHANGE = 'APP_LAYOUT_CHANGE';
export const changeLayout = layout => ({
type: APP_LAYOUT_CHANGE,
layout,
});

View File

@ -0,0 +1,10 @@
import { createAction } from '@reduxjs/toolkit';
export const focusApp = createAction('APP_FOCUS');
export const unfocusApp = createAction('APP_UNFOCUS');
type ChangeLayoutPayload = {
layout: 'mobile' | 'single-column' | 'multi-column';
};
export const changeLayout =
createAction<ChangeLayoutPayload>('APP_LAYOUT_CHANGE');

View File

@ -362,7 +362,7 @@ class UI extends React.PureComponent {
if (layout !== this.props.layout) {
this.handleLayoutChange.cancel();
this.props.dispatch(changeLayout(layout));
this.props.dispatch(changeLayout({ layout }));
} else {
this.handleLayoutChange();
}

View File

@ -1,5 +1,5 @@
import { STORE_HYDRATE } from 'mastodon/actions/store';
import { APP_LAYOUT_CHANGE } from 'mastodon/actions/app';
import { changeLayout } from 'mastodon/actions/app';
import { Map as ImmutableMap } from 'immutable';
import { layoutFromWindow } from 'mastodon/is_mobile';
@ -14,8 +14,8 @@ export default function meta(state = initialState, action) {
switch(action.type) {
case STORE_HYDRATE:
return state.merge(action.state.get('meta')).set('permissions', action.state.getIn(['role', 'permissions']));
case APP_LAYOUT_CHANGE:
return state.set('layout', action.layout);
case changeLayout.type:
return state.set('layout', action.payload.layout);
default:
return state;
}

View File

@ -1,21 +0,0 @@
import { Map as ImmutableMap } from 'immutable';
import { NOTIFICATIONS_UPDATE } from 'mastodon/actions/notifications';
import { APP_FOCUS, APP_UNFOCUS } from 'mastodon/actions/app';
const initialState = ImmutableMap({
focused: true,
unread: 0,
});
export default function missed_updates(state = initialState, action) {
switch(action.type) {
case APP_FOCUS:
return state.set('focused', true).set('unread', 0);
case APP_UNFOCUS:
return state.set('focused', false);
case NOTIFICATIONS_UPDATE:
return state.get('focused') ? state : state.update('unread', x => x + 1);
default:
return state;
}
}

View File

@ -0,0 +1,31 @@
import { Record } from 'immutable';
import type { Action } from 'redux';
import { NOTIFICATIONS_UPDATE } from '../actions/notifications';
import { focusApp, unfocusApp } from '../actions/app';
type MissedUpdatesState = {
focused: boolean;
unread: number;
};
const initialState = Record<MissedUpdatesState>({
focused: true,
unread: 0,
})();
export default function missed_updates(
state = initialState,
action: Action<string>,
) {
switch (action.type) {
case focusApp.type:
return state.set('focused', true).set('unread', 0);
case unfocusApp.type:
return state.set('focused', false);
case NOTIFICATIONS_UPDATE:
return state.get('focused')
? state
: state.update('unread', (x) => x + 1);
default:
return state;
}
}