Skip to content

Commit 81f38f2

Browse files
committed
Rewrite page reducer in RTK
1 parent 8f6fa1a commit 81f38f2

File tree

6 files changed

+28
-25
lines changed

6 files changed

+28
-25
lines changed

ui/frontend/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ module.exports = {
8787
'reducers/output/mir.ts',
8888
'reducers/output/miri.ts',
8989
'reducers/output/wasm.ts',
90+
'reducers/page.ts',
9091
'reducers/versions.ts',
9192
'reducers/websocket.ts',
9293
'websocketActions.ts',

ui/frontend/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ node_modules
3838
!reducers/output/mir.ts
3939
!reducers/output/miri.ts
4040
!reducers/output/wasm.ts
41+
!reducers/page.ts
4142
!reducers/versions.ts
4243
!reducers/websocket.ts
4344
!websocketActions.ts

ui/frontend/Header.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import ToolsMenu from './ToolsMenu';
2020
import * as actions from './actions';
2121
import { useAppDispatch } from './configureStore';
2222
import { performGistSave } from './reducers/output/gist';
23+
import { navigateToHelp } from './reducers/page';
2324
import * as selectors from './selectors';
2425

2526
import styles from './Header.module.css';
@@ -195,7 +196,7 @@ const ConfigMenuButton: React.FC<PortalProps> = ({ menuContainer }) => {
195196
};
196197

197198
const HelpButton: React.FC = () => (
198-
<IconLink title="View help" action={actions.navigateToHelp}>
199+
<IconLink title="View help" action={navigateToHelp}>
199200
<HelpIcon />
200201
</IconLink>
201202
);

ui/frontend/Help.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
22

3-
import * as actions from './actions';
43
import Example from './HelpExample';
54
import Link from './uss-router/Link';
65

76
import styles from './Help.module.css';
87

98
import integer32Logo from './assets/integer32-logo.svg';
9+
import { navigateToIndex } from './reducers/page';
1010

1111
const ACE_URL = 'https://github.com/ajaxorg/ace';
1212
const CLIPPY_URL = 'https://github.com/rust-lang/rust-clippy';
@@ -79,7 +79,7 @@ const Help: React.FC = () => {
7979
return (
8080
<section className={styles.container}>
8181
<h1>The Rust Playground</h1>
82-
<Link action={actions.navigateToIndex}>Return to the playground</Link>
82+
<Link action={navigateToIndex}>Return to the playground</Link>
8383

8484
<LinkableSection id="about" header="About" level="h2">
8585
<p>

ui/frontend/actions.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
Mode,
1717
Notification,
1818
Orientation,
19-
Page,
2019
PairCharacters,
2120
PrimaryAction,
2221
PrimaryActionAuto,
@@ -33,6 +32,7 @@ import { performCompileToHirOnly } from './reducers/output/hir';
3332
import { performCompileToLlvmIrOnly } from './reducers/output/llvmIr';
3433
import { performCompileToMirOnly } from './reducers/output/mir';
3534
import { performCompileToWasmOnly } from './reducers/output/wasm';
35+
import { navigateToHelp, navigateToIndex } from './reducers/page';
3636

3737
export type ThunkAction<T = void> = ReduxThunkAction<T, State, {}, Action>;
3838
export type SimpleThunkAction<T = void> = ReduxThunkAction<T, State, {}, AnyAction>;
@@ -43,7 +43,6 @@ const createAction = <T extends string, P extends {}>(type: T, props?: P) => (
4343

4444
export enum ActionType {
4545
InitializeApplication = 'INITIALIZE_APPLICATION',
46-
SetPage = 'SET_PAGE',
4746
ChangeEditor = 'CHANGE_EDITOR',
4847
ChangeKeybinding = 'CHANGE_KEYBINDING',
4948
ChangeAceTheme = 'CHANGE_ACE_THEME',
@@ -71,12 +70,6 @@ export enum ActionType {
7170

7271
export const initializeApplication = () => createAction(ActionType.InitializeApplication);
7372

74-
const setPage = (page: Page) =>
75-
createAction(ActionType.SetPage, { page });
76-
77-
export const navigateToIndex = () => setPage('index');
78-
export const navigateToHelp = () => setPage('help');
79-
8073
export const changeEditor = (editor: Editor) =>
8174
createAction(ActionType.ChangeEditor, { editor });
8275

@@ -310,9 +303,7 @@ export function indexPageLoad({
310303
};
311304
}
312305

313-
export function helpPageLoad() {
314-
return navigateToHelp();
315-
}
306+
export const helpPageLoad = navigateToHelp;
316307

317308
export function showExample(code: string): ThunkAction {
318309
return function(dispatch) {
@@ -323,7 +314,6 @@ export function showExample(code: string): ThunkAction {
323314

324315
export type Action =
325316
| ReturnType<typeof initializeApplication>
326-
| ReturnType<typeof setPage>
327317
| ReturnType<typeof changePairCharacters>
328318
| ReturnType<typeof changeAssemblyFlavor>
329319
| ReturnType<typeof changeBacktrace>
@@ -347,5 +337,6 @@ export type Action =
347337
| ReturnType<typeof selectText>
348338
| ReturnType<typeof notificationSeen>
349339
| ReturnType<typeof browserWidthChanged>
340+
| ReturnType<typeof navigateToIndex>
350341
| ReturnType<typeof wsExecuteRequest>
351342
;

ui/frontend/reducers/page.ts

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

4-
export type State = Page;
5+
const initialState = 'index' as Page;
6+
7+
const slice = createSlice({
8+
name: 'page',
9+
initialState,
10+
reducers: {
11+
setPage: (_state, action: PayloadAction<Page>) => {
12+
return action.payload;
13+
},
14+
},
15+
});
16+
17+
export const { setPage } = slice.actions;
18+
19+
export const navigateToIndex = () => setPage('index');
20+
export const navigateToHelp = () => setPage('help');
521

6-
export default function page(state: State = 'index', action: Action) {
7-
switch (action.type) {
8-
case ActionType.SetPage:
9-
return action.page;
10-
default:
11-
return state;
12-
}
13-
}
22+
export default slice.reducer;

0 commit comments

Comments
 (0)