Skip to content

Commit a173049

Browse files
committed
Rewrite notifications reducer in RTK
1 parent cca0e17 commit a173049

File tree

5 files changed

+23
-22
lines changed

5 files changed

+23
-22
lines changed

ui/frontend/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ module.exports = {
7777
'reducers/crates.ts',
7878
'reducers/featureFlags.ts',
7979
'reducers/globalConfiguration.ts',
80+
'reducers/notifications.ts',
8081
'reducers/output/assembly.ts',
8182
'reducers/output/clippy.ts',
8283
'reducers/output/execute.ts',

ui/frontend/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ node_modules
2828
!reducers/crates.ts
2929
!reducers/featureFlags.ts
3030
!reducers/globalConfiguration.ts
31+
!reducers/notifications.ts
3132
!reducers/output/assembly.ts
3233
!reducers/output/clippy.ts
3334
!reducers/output/execute.ts

ui/frontend/Notifications.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { useDispatch, useSelector } from 'react-redux';
44

55
import { Close } from './Icon';
66

7-
import * as actions from './actions';
87
import * as selectors from './selectors';
8+
import { seenRustSurvey2022 } from './reducers/notifications';
99

1010
import styles from './Notifications.module.css';
1111

@@ -25,7 +25,7 @@ const RustSurvey2022Notification: React.FC = () => {
2525
const showRustSurvey2022 = useSelector(selectors.showRustSurvey2022Selector);
2626

2727
const dispatch = useDispatch();
28-
const seenRustSurvey2021 = useCallback(() => dispatch(actions.seenRustSurvey2022()), [dispatch]);
28+
const seenRustSurvey2021 = useCallback(() => dispatch(seenRustSurvey2022()), [dispatch]);
2929

3030
return showRustSurvey2022 ? (
3131
<Notification onClose={seenRustSurvey2021}>

ui/frontend/actions.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
Edition,
1515
Editor,
1616
Mode,
17-
Notification,
1817
Orientation,
1918
PairCharacters,
2019
PrimaryAction,
@@ -58,7 +57,6 @@ export enum ActionType {
5857
ChangeEdition = 'CHANGE_EDITION',
5958
ChangeBacktrace = 'CHANGE_BACKTRACE',
6059
SelectText = 'SELECT_TEXT',
61-
NotificationSeen = 'NOTIFICATION_SEEN',
6260
}
6361

6462
export const initializeApplication = () => createAction(ActionType.InitializeApplication);
@@ -194,11 +192,6 @@ export const performCompileToWasm =
194192
export const selectText = (start: Position, end: Position) =>
195193
createAction(ActionType.SelectText, { start, end });
196194

197-
const notificationSeen = (notification: Notification) =>
198-
createAction(ActionType.NotificationSeen, { notification });
199-
200-
export const seenRustSurvey2022 = () => notificationSeen(Notification.RustSurvey2022);
201-
202195
function parseChannel(s?: string): Channel | null {
203196
switch (s) {
204197
case 'stable':
@@ -301,7 +294,6 @@ export type Action =
301294
| ReturnType<typeof changeAceTheme>
302295
| ReturnType<typeof changeMonacoTheme>
303296
| ReturnType<typeof selectText>
304-
| ReturnType<typeof notificationSeen>
305297
| ReturnType<typeof editCode>
306298
| ReturnType<typeof addCrateType>
307299
| ReturnType<typeof navigateToIndex>

ui/frontend/reducers/notifications.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Action, ActionType } from '../actions';
1+
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
2+
23
import { Notification } from '../types';
34

45
interface State {
@@ -11,7 +12,7 @@ interface State {
1112
seenRustSurvey2022: boolean;
1213
}
1314

14-
const DEFAULT: State = {
15+
const initialState: State = {
1516
seenRustSurvey2018: true,
1617
seenRust2018IsDefault: true,
1718
seenRustSurvey2020: true,
@@ -21,16 +22,22 @@ const DEFAULT: State = {
2122
seenRustSurvey2022: false,
2223
};
2324

24-
export default function notifications(state = DEFAULT, action: Action): State {
25-
switch (action.type) {
26-
case ActionType.NotificationSeen: {
27-
switch (action.notification) {
25+
const slice = createSlice({
26+
name: 'notifications',
27+
initialState,
28+
reducers: {
29+
notificationSeen: (state, action: PayloadAction<Notification>) => {
30+
switch (action.payload) {
2831
case Notification.RustSurvey2022: {
29-
return { ...state, seenRustSurvey2022: true };
32+
state.seenRustSurvey2022 = true;
3033
}
3134
}
32-
}
33-
default:
34-
return state;
35-
}
36-
}
35+
},
36+
},
37+
});
38+
39+
const { notificationSeen } = slice.actions;
40+
41+
export const seenRustSurvey2022 = () => notificationSeen(Notification.RustSurvey2022);
42+
43+
export default slice.reducer;

0 commit comments

Comments
 (0)