From 37191e46e600cd9ac2839f0687efb347fc4ef7c1 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Thu, 27 Aug 2020 22:01:17 -0400 Subject: [PATCH] feat(redux-devtools): convert todomvc example to TypeScript (#618) --- package.json | 3 + .../react-json-tree/examples/tsconfig.json | 3 - .../examples/counter/tsconfig.json | 3 - .../redux-devtools/examples/todomvc/.babelrc | 7 +- .../examples/todomvc/.eslintrc.js | 21 +++++ .../examples/todomvc/actions/TodoActions.js | 42 ---------- .../examples/todomvc/actions/TodoActions.ts | 82 +++++++++++++++++++ .../components/{Footer.js => Footer.tsx} | 23 ++++-- .../components/{Header.js => Header.tsx} | 12 ++- .../{MainSection.js => MainSection.tsx} | 50 +++++++---- .../components/{TodoItem.js => TodoItem.tsx} | 34 +++++--- .../{TodoTextInput.js => TodoTextInput.tsx} | 50 +++++++---- .../{ActionTypes.js => ActionTypes.ts} | 0 .../{TodoFilters.js => TodoFilters.ts} | 5 ++ .../containers/{DevTools.js => DevTools.tsx} | 0 .../containers/{Root.dev.js => Root.dev.tsx} | 9 +- .../examples/todomvc/containers/Root.js | 5 -- .../{Root.prod.js => Root.prod.tsx} | 9 +- .../examples/todomvc/containers/Root.ts | 15 ++++ .../containers/{TodoApp.js => TodoApp.tsx} | 20 ++++- .../examples/todomvc/{index.js => index.tsx} | 1 + .../examples/todomvc/package.json | 49 ++++++----- .../todomvc/reducers/{index.js => index.ts} | 6 +- .../todomvc/reducers/{todos.js => todos.ts} | 13 ++- .../todomvc/store/configureStore.dev.js | 21 ----- .../todomvc/store/configureStore.dev.ts | 29 +++++++ .../examples/todomvc/store/configureStore.js | 5 -- .../todomvc/store/configureStore.prod.js | 6 -- .../todomvc/store/configureStore.prod.ts | 8 ++ .../examples/todomvc/store/configureStore.ts | 13 +++ .../examples/todomvc/tsconfig.json | 4 + .../examples/todomvc/tsconfig.webpack.json | 4 + .../{webpack.config.js => webpack.config.ts} | 13 +-- yarn.lock | 15 ---- 34 files changed, 382 insertions(+), 198 deletions(-) create mode 100644 packages/redux-devtools/examples/todomvc/.eslintrc.js delete mode 100644 packages/redux-devtools/examples/todomvc/actions/TodoActions.js create mode 100644 packages/redux-devtools/examples/todomvc/actions/TodoActions.ts rename packages/redux-devtools/examples/todomvc/components/{Footer.js => Footer.tsx} (74%) rename packages/redux-devtools/examples/todomvc/components/{Header.js => Header.tsx} (72%) rename packages/redux-devtools/examples/todomvc/components/{MainSection.js => MainSection.tsx} (65%) rename packages/redux-devtools/examples/todomvc/components/{TodoItem.js => TodoItem.tsx} (70%) rename packages/redux-devtools/examples/todomvc/components/{TodoTextInput.js => TodoTextInput.tsx} (52%) rename packages/redux-devtools/examples/todomvc/constants/{ActionTypes.js => ActionTypes.ts} (100%) rename packages/redux-devtools/examples/todomvc/constants/{TodoFilters.js => TodoFilters.ts} (56%) rename packages/redux-devtools/examples/todomvc/containers/{DevTools.js => DevTools.tsx} (100%) rename packages/redux-devtools/examples/todomvc/containers/{Root.dev.js => Root.dev.tsx} (65%) delete mode 100644 packages/redux-devtools/examples/todomvc/containers/Root.js rename packages/redux-devtools/examples/todomvc/containers/{Root.prod.js => Root.prod.tsx} (59%) create mode 100644 packages/redux-devtools/examples/todomvc/containers/Root.ts rename packages/redux-devtools/examples/todomvc/containers/{TodoApp.js => TodoApp.tsx} (55%) rename packages/redux-devtools/examples/todomvc/{index.js => index.tsx} (90%) rename packages/redux-devtools/examples/todomvc/reducers/{index.js => index.ts} (58%) rename packages/redux-devtools/examples/todomvc/reducers/{todos.js => todos.ts} (78%) delete mode 100644 packages/redux-devtools/examples/todomvc/store/configureStore.dev.js create mode 100644 packages/redux-devtools/examples/todomvc/store/configureStore.dev.ts delete mode 100644 packages/redux-devtools/examples/todomvc/store/configureStore.js delete mode 100644 packages/redux-devtools/examples/todomvc/store/configureStore.prod.js create mode 100644 packages/redux-devtools/examples/todomvc/store/configureStore.prod.ts create mode 100644 packages/redux-devtools/examples/todomvc/store/configureStore.ts create mode 100644 packages/redux-devtools/examples/todomvc/tsconfig.json create mode 100644 packages/redux-devtools/examples/todomvc/tsconfig.webpack.json rename packages/redux-devtools/examples/todomvc/{webpack.config.js => webpack.config.ts} (84%) diff --git a/package.json b/package.json index 4378d3b7..026a3bf7 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@typescript-eslint/eslint-plugin": "^3.9.0", "@typescript-eslint/parser": "^3.9.0", "babel-eslint": "^10.1.0", + "babel-loader": "^8.1.0", "eslint": "^7.6.0", "eslint-config-prettier": "^6.11.0", "eslint-plugin-babel": "^5.3.1", @@ -22,7 +23,9 @@ "jest": "^26.2.2", "lerna": "^3.22.1", "prettier": "^2.0.5", + "raw-loader": "^4.0.1", "rimraf": "^3.0.2", + "style-loader": "^1.2.1", "ts-jest": "^26.2.0", "ts-node": "^9.0.0", "typescript": "^3.9.7", diff --git a/packages/react-json-tree/examples/tsconfig.json b/packages/react-json-tree/examples/tsconfig.json index 47fb1215..5463ab30 100644 --- a/packages/react-json-tree/examples/tsconfig.json +++ b/packages/react-json-tree/examples/tsconfig.json @@ -1,7 +1,4 @@ { "extends": "../../../tsconfig.react.base.json", - "compilerOptions": { - "outDir": "lib" - }, "include": ["src"] } diff --git a/packages/redux-devtools/examples/counter/tsconfig.json b/packages/redux-devtools/examples/counter/tsconfig.json index c640259b..45c3bc56 100644 --- a/packages/redux-devtools/examples/counter/tsconfig.json +++ b/packages/redux-devtools/examples/counter/tsconfig.json @@ -1,7 +1,4 @@ { "extends": "../../../../tsconfig.react.base.json", - "compilerOptions": { - "outDir": "lib" - }, "include": ["src"] } diff --git a/packages/redux-devtools/examples/todomvc/.babelrc b/packages/redux-devtools/examples/todomvc/.babelrc index 3c204230..a01c7adc 100644 --- a/packages/redux-devtools/examples/todomvc/.babelrc +++ b/packages/redux-devtools/examples/todomvc/.babelrc @@ -1,8 +1,11 @@ { - "presets": ["@babel/preset-env", "@babel/preset-react"], + "presets": [ + "@babel/preset-env", + "@babel/preset-react", + "@babel/preset-typescript" + ], "plugins": [ "@babel/plugin-proposal-class-properties", - "@babel/plugin-proposal-function-bind", "react-hot-loader/babel" ] } diff --git a/packages/redux-devtools/examples/todomvc/.eslintrc.js b/packages/redux-devtools/examples/todomvc/.eslintrc.js new file mode 100644 index 00000000..70e1445e --- /dev/null +++ b/packages/redux-devtools/examples/todomvc/.eslintrc.js @@ -0,0 +1,21 @@ +module.exports = { + extends: '../../../../.eslintrc', + overrides: [ + { + files: ['*.ts', '*.tsx'], + extends: '../../../../eslintrc.ts.react.base.json', + parserOptions: { + tsconfigRootDir: __dirname, + project: ['./tsconfig.json'], + }, + }, + { + files: ['webpack.config.ts'], + extends: '../../../../eslintrc.ts.base.json', + parserOptions: { + tsconfigRootDir: __dirname, + project: ['./tsconfig.webpack.json'], + }, + }, + ], +}; diff --git a/packages/redux-devtools/examples/todomvc/actions/TodoActions.js b/packages/redux-devtools/examples/todomvc/actions/TodoActions.js deleted file mode 100644 index f0122846..00000000 --- a/packages/redux-devtools/examples/todomvc/actions/TodoActions.js +++ /dev/null @@ -1,42 +0,0 @@ -import * as types from '../constants/ActionTypes'; - -export function addTodo(text) { - return { - type: types.ADD_TODO, - text, - }; -} - -export function deleteTodo(id) { - return { - type: types.DELETE_TODO, - id, - }; -} - -export function editTodo(id, text) { - return { - type: types.EDIT_TODO, - id, - text, - }; -} - -export function markTodo(id) { - return { - type: types.MARK_TODO, - id, - }; -} - -export function markAll() { - return { - type: types.MARK_ALL, - }; -} - -export function clearMarked() { - return { - type: types.CLEAR_MARKED, - }; -} diff --git a/packages/redux-devtools/examples/todomvc/actions/TodoActions.ts b/packages/redux-devtools/examples/todomvc/actions/TodoActions.ts new file mode 100644 index 00000000..4169e9c1 --- /dev/null +++ b/packages/redux-devtools/examples/todomvc/actions/TodoActions.ts @@ -0,0 +1,82 @@ +import * as types from '../constants/ActionTypes'; + +interface AddTodoAction { + type: typeof types.ADD_TODO; + text: string; +} +export function addTodo(text: string): AddTodoAction { + return { + type: types.ADD_TODO, + text, + }; +} + +interface DeleteTodoAction { + type: typeof types.DELETE_TODO; + id: number; +} +export function deleteTodo(id: number): DeleteTodoAction { + return { + type: types.DELETE_TODO, + id, + }; +} + +interface EditTodoAction { + type: typeof types.EDIT_TODO; + id: number; + text: string; +} +export function editTodo(id: number, text: string): EditTodoAction { + return { + type: types.EDIT_TODO, + id, + text, + }; +} + +interface MarkTodoAction { + type: typeof types.MARK_TODO; + id: number; +} +export function markTodo(id: number): MarkTodoAction { + return { + type: types.MARK_TODO, + id, + }; +} + +interface MarkAllAction { + type: typeof types.MARK_ALL; +} +export function markAll(): MarkAllAction { + return { + type: types.MARK_ALL, + }; +} + +interface ClearMarkedAction { + type: typeof types.CLEAR_MARKED; +} +export function clearMarked(): ClearMarkedAction { + return { + type: types.CLEAR_MARKED, + }; +} + +export type TodoAction = + | AddTodoAction + | DeleteTodoAction + | EditTodoAction + | MarkTodoAction + | MarkAllAction + | ClearMarkedAction; + +export interface TodoActions { + addTodo(text: string): AddTodoAction; + deleteTodo(id: number): DeleteTodoAction; + editTodo(id: number, text: string): EditTodoAction; + markTodo(id: number): MarkTodoAction; + markAll(): MarkAllAction; + clearMarked(): ClearMarkedAction; +} diff --git a/packages/redux-devtools/examples/todomvc/components/Footer.js b/packages/redux-devtools/examples/todomvc/components/Footer.tsx similarity index 74% rename from packages/redux-devtools/examples/todomvc/components/Footer.js rename to packages/redux-devtools/examples/todomvc/components/Footer.tsx index e8914c4d..3c708eeb 100644 --- a/packages/redux-devtools/examples/todomvc/components/Footer.js +++ b/packages/redux-devtools/examples/todomvc/components/Footer.tsx @@ -1,7 +1,12 @@ -import React, { Component } from 'react'; +import React, { Component, MouseEventHandler } from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; -import { SHOW_ALL, SHOW_MARKED, SHOW_UNMARKED } from '../constants/TodoFilters'; +import { + SHOW_ALL, + SHOW_MARKED, + SHOW_UNMARKED, + TodoFilter, +} from '../constants/TodoFilters'; const FILTER_TITLES = { [SHOW_ALL]: 'All', @@ -9,7 +14,15 @@ const FILTER_TITLES = { [SHOW_MARKED]: 'Completed', }; -export default class Footer extends Component { +interface Props { + markedCount: number; + unmarkedCount: number; + filter: TodoFilter; + onClearMarked: MouseEventHandler; + onShow: (filter: TodoFilter) => void; +} + +export default class Footer extends Component { static propTypes = { markedCount: PropTypes.number.isRequired, unmarkedCount: PropTypes.number.isRequired, @@ -23,7 +36,7 @@ export default class Footer extends Component {