Skip to content

Commit 1b6c008

Browse files
committed
refactoring to adapt for new patterns available by typesafe-actionsresolved piotrwitek#77
1 parent d89d1f1 commit 1b6c008

25 files changed

+126
-125
lines changed

README.md

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ You should check Playground Project located in the `/playground` folder. It is a
6060
- [Vendor Types Augmentation](#vendor-types-augmentation)
6161
- [Default and Named Module Exports](#default-and-named-module-exports)
6262
- [FAQ](#faq)
63-
- [Contribution Guide](#contribution-guide)
6463
- [Tutorials](#tutorials)
6564

6665
---
@@ -505,8 +504,8 @@ export const withState = <WrappedProps extends InjectedProps>(
505504
```tsx
506505
import * as React from 'react';
507506

508-
import { withState } from '@src/hoc';
509-
import { SFCCounter } from '@src/components';
507+
import { withState } from '../hoc';
508+
import { SFCCounter } from '../components';
510509

511510
const SFCCounterWithState =
512511
withState(SFCCounter);
@@ -589,8 +588,8 @@ export const withErrorBoundary = <WrappedProps extends InjectedProps>(
589588
```tsx
590589
import * as React from 'react';
591590

592-
import { withErrorBoundary } from '@src/hoc';
593-
import { ErrorMessage } from '@src/components';
591+
import { withErrorBoundary } from '../hoc';
592+
import { ErrorMessage } from '../components';
594593

595594
const ErrorMessageWithErrorBoundary =
596595
withErrorBoundary(ErrorMessage);
@@ -633,14 +632,14 @@ const mapDispatchToProps = (dispatch: Dispatch) => ({
633632
#### - redux connected counter
634633
635634
```tsx
635+
import Types from 'Types';
636636
import { connect } from 'react-redux';
637637

638-
import { RootState } from '@src/redux';
639-
import { countersActions, countersSelectors } from '@src/redux/counters';
640-
import { SFCCounter } from '@src/components';
638+
import { countersActions, countersSelectors } from '../features/counters';
639+
import { SFCCounter } from '../components';
641640

642-
const mapStateToProps = (state: RootState) => ({
643-
count: countersSelectors.getReduxCounter(state),
641+
const mapStateToProps = (state: Types.RootState) => ({
642+
count: countersSelectors.getReduxCounter(state.counters),
644643
});
645644

646645
export const SFCCounterConnected = connect(mapStateToProps, {
@@ -653,7 +652,7 @@ export const SFCCounterConnected = connect(mapStateToProps, {
653652
```tsx
654653
import * as React from 'react';
655654

656-
import { SFCCounterConnected } from '@src/connected';
655+
import { SFCCounterConnected } from '../connected';
657656

658657
export default () => (
659658
<SFCCounterConnected
@@ -669,14 +668,14 @@ export default () => (
669668
#### - redux connected counter (verbose)
670669
671670
```tsx
672-
import { bindActionCreators } from 'redux';
671+
import Types from 'Types';
672+
import { bindActionCreators, Dispatch } from 'redux';
673673
import { connect } from 'react-redux';
674674

675-
import { RootState, Dispatch } from '@src/redux';
676-
import { countersActions } from '@src/redux/counters';
677-
import { SFCCounter } from '@src/components';
675+
import { countersActions } from '../features/counters';
676+
import { SFCCounter } from '../components';
678677

679-
const mapStateToProps = (state: RootState) => ({
678+
const mapStateToProps = (state: Types.RootState) => ({
680679
count: state.counters.reduxCounter,
681680
});
682681

@@ -693,7 +692,7 @@ export const SFCCounterConnectedVerbose =
693692
```tsx
694693
import * as React from 'react';
695694

696-
import { SFCCounterConnectedVerbose } from '@src/connected';
695+
import { SFCCounterConnectedVerbose } from '../connected';
697696

698697
export default () => (
699698
<SFCCounterConnectedVerbose
@@ -709,18 +708,18 @@ export default () => (
709708
#### - with own props
710709
711710
```tsx
711+
import Types from 'Types';
712712
import { connect } from 'react-redux';
713713

714-
import { RootState } from '@src/redux';
715-
import { countersActions, countersSelectors } from '@src/redux/counters';
716-
import { SFCCounter } from '@src/components';
714+
import { countersActions, countersSelectors } from '../features/counters';
715+
import { SFCCounter } from '../components';
717716

718717
export interface SFCCounterConnectedExtendedProps {
719718
initialCount: number;
720719
}
721720

722-
const mapStateToProps = (state: RootState, ownProps: SFCCounterConnectedExtendedProps) => ({
723-
count: countersSelectors.getReduxCounter(state) + ownProps.initialCount,
721+
const mapStateToProps = (state: Types.RootState, ownProps: SFCCounterConnectedExtendedProps) => ({
722+
count: countersSelectors.getReduxCounter(state.counters) + ownProps.initialCount,
724723
});
725724

726725
export const SFCCounterConnectedExtended = connect(mapStateToProps, {
@@ -733,7 +732,7 @@ export const SFCCounterConnectedExtended = connect(mapStateToProps, {
733732
```tsx
734733
import * as React from 'react';
735734

736-
import { SFCCounterConnectedExtended } from '@src/connected';
735+
import { SFCCounterConnectedExtended } from '../connected';
737736

738737
export default () => <SFCCounterConnectedExtended label={'SFCCounterConnectedExtended'} initialCount={10} />;
739738

@@ -756,7 +755,7 @@ export default () => <SFCCounterConnectedExtended label={'SFCCounterConnectedExt
756755
A solution below is using simple factory function to automate the creation of type-safe action creators. The goal is to reduce the maintainability and code repetition of type annotations for actions and creators and the result is completely typesafe action-creators and their actions.
757756
758757
```tsx
759-
import { action, createAction, createStandardAction } from 'typesafe-actions';
758+
import { action } from 'typesafe-actions';
760759

761760
import { ADD, INCREMENT } from './constants';
762761

@@ -770,10 +769,12 @@ export const add = (amount: number) => action(ADD, amount);
770769
// https://github.com/piotrwitek/typesafe-actions#behold-the-mighty-tutorial
771770

772771
// OPTION 1 (with generics):
772+
// import { createStandardAction } from 'typesafe-actions';
773773
// export const increment = createStandardAction(INCREMENT)<void>();
774774
// export const add = createStandardAction(ADD)<number>();
775775

776776
// OPTION 2 (with resolve callback):
777+
// import { createAction } from 'typesafe-actions';
777778
// export const increment = createAction(INCREMENT);
778779
// export const add = createAction(ADD, resolve => {
779780
// return (amount: number) => resolve(amount);
@@ -869,7 +870,7 @@ state.counterPairs[0].immutableCounter2 = 1; // TS Error: cannot be mutated
869870
870871
```tsx
871872
import { combineReducers } from 'redux';
872-
import { ActionsUnion } from 'typesafe-actions';
873+
import { ActionType } from 'typesafe-actions';
873874

874875
import { Todo, TodosFilter } from './models';
875876
import * as actions from './actions';
@@ -882,7 +883,7 @@ export type TodosState = {
882883
readonly todosFilter: TodosFilter;
883884
};
884885

885-
export type TodosAction = ActionsUnion<typeof actions>;
886+
export type TodosAction = ActionType<typeof actions>;
886887

887888
export default combineReducers<TodosState, TodosAction>({
888889
isFetching: (state = false, action) => {
@@ -1010,21 +1011,21 @@ When creating a store instance we don't need to provide any additional types. It
10101011
> The resulting store instance methods like `getState` or `dispatch` will be type checked and will expose all type errors
10111012
10121013
```tsx
1013-
import { createStore, applyMiddleware, compose } from 'redux';
1014+
import { createStore, applyMiddleware } from 'redux';
10141015
import { createEpicMiddleware } from 'redux-observable';
10151016

1017+
import { composeEnhancers } from './utils';
10161018
import rootReducer from './root-reducer';
10171019
import rootEpic from './root-epic';
1020+
import services from '../services';
10181021

1019-
const composeEnhancers =
1020-
(process.env.NODE_ENV === 'development' &&
1021-
window &&
1022-
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||
1023-
compose;
1022+
export const epicMiddleware = createEpicMiddleware(rootEpic, {
1023+
dependencies: services,
1024+
});
10241025

10251026
function configureStore(initialState?: object) {
10261027
// configure middlewares
1027-
const middlewares = [createEpicMiddleware(rootEpic)];
1028+
const middlewares = [epicMiddleware];
10281029
// compose enhancers
10291030
const enhancer = composeEnhancers(applyMiddleware(...middlewares));
10301031
// create store
@@ -1048,7 +1049,6 @@ export default store;
10481049
### For more examples and in-depth explanation you should read [The Mighty Tutorial](https://github.com/piotrwitek/typesafe-actions#behold-the-mighty-tutorial) to learn it all the easy way!
10491050
10501051
```tsx
1051-
// tslint:disable:no-console
10521052
import Types from 'Types';
10531053
import { combineEpics, Epic } from 'redux-observable';
10541054
import { tap, ignoreElements, filter } from 'rxjs/operators';
@@ -1059,12 +1059,13 @@ import { todosConstants, TodosAction } from '../todos';
10591059
// contrived example!!!
10601060
const logAddAction: Epic<TodosAction, Types.RootState, Types.Services> = (
10611061
action$,
1062-
store
1062+
store,
1063+
{ logger }
10631064
) =>
10641065
action$.pipe(
10651066
filter(isOfType(todosConstants.ADD)), // action is narrowed to: { type: "ADD_TODO"; payload: string; }
10661067
tap(action => {
1067-
console.log(
1068+
logger.log(
10681069
`action type must be equal: ${todosConstants.ADD} === ${action.type}`
10691070
);
10701071
}),
@@ -1447,33 +1448,16 @@ class StatefulCounter extends React.Component<Props, State> {
14471448
14481449
---
14491450
1450-
# Contribution Guide
1451-
- Don't edit `README.md` - it is built with `generator` script from separate `.md` files located in the `/docs/markdown` folder, edit them instead
1452-
- For code snippets, they are also injected by `generator` script from the source files located in the playground folder (this step make sure all examples are type-checked and linted), edit them instead
1453-
> look for include directives in `.md` files that look like this: `::[example|usage]='../../playground/src/components/sfc-counter.tsx'::`
1454-
1455-
Before opening PR please make sure to check:
1456-
```bash
1457-
# run linter in playground
1458-
yarn run lint
1459-
1460-
# run type-checking in playground
1461-
yarn run tsc
1462-
1463-
# re-generate `README.md` from repo root
1464-
sh ./generate.sh
1465-
# or
1466-
node ./generator/bin/generate-readme.js
1467-
```
1468-
1469-
[⇧ back to top](#table-of-contents)
1470-
1471-
---
1472-
14731451
# Tutorials
14741452
> Curated list of relevant in-depth tutorials
14751453
14761454
Higher-Order Components:
14771455
- https://medium.com/@jrwebdev/react-higher-order-component-patterns-in-typescript-42278f7590fb
14781456
14791457
[⇧ back to top](#table-of-contents)
1458+
1459+
---
1460+
1461+
MIT License
1462+
1463+
Copyright (c) 2017 Piotr Witek <[email protected]> (http://piotrwitek.github.io)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import * as React from 'react';
22

3-
import { IUser, User } from '@src/models';
4-
import { GenericList } from '@src/components';
3+
import { IUser, User } from '../models';
4+
import { GenericList } from '../components';
55

66
const users = [
77
new User('Rosamonte', 'Especial'),
88
new User('Aguantadora', 'Despalada'),
99
new User('Taragui', 'Vitality'),
1010
];
1111

12-
export class UserList extends GenericList<IUser> { }
12+
export class UserList extends GenericList<IUser> {}
1313

1414
export default () => (
1515
<UserList
1616
items={users}
17-
itemRenderer={(item) => <div key={item.id}>{item.fullName}</div>}
17+
itemRenderer={item => <div key={item.id}>{item.fullName}</div>}
1818
/>
1919
);

playground/src/components/sfc-counter.usage.tsx

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

3-
import { SFCCounter } from '@src/components';
3+
import { SFCCounter } from '../components';
44

55
export default class extends React.Component<{}, { count: number }> {
66
state = { count: 0 };
@@ -10,7 +10,9 @@ export default class extends React.Component<{}, { count: number }> {
1010
<SFCCounter
1111
label={'SFCCounter'}
1212
count={this.state.count}
13-
onIncrement={() => { this.setState({ count: this.state.count + 1 }); }}
13+
onIncrement={() => {
14+
this.setState({ count: this.state.count + 1 });
15+
}}
1416
/>
1517
);
1618
}

playground/src/components/sfc-spread-attributes.usage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22

3-
import { SFCSpreadAttributes } from '@src/components';
3+
import { SFCSpreadAttributes } from '../components';
44

55
export default () => (
66
<SFCSpreadAttributes

playground/src/components/stateful-counter-with-default.usage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22

3-
import { StatefulCounterWithDefault } from '@src/components';
3+
import { StatefulCounterWithDefault } from '../components';
44

55
export default () => (
66
<StatefulCounterWithDefault

playground/src/components/stateful-counter.usage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22

3-
import { StatefulCounter } from '@src/components';
3+
import { StatefulCounter } from '../components';
44

55
export default () => (
66
<StatefulCounter

playground/src/connected/sfc-counter-connected-extended.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
import Types from 'Types';
12
import { connect } from 'react-redux';
23

3-
import { RootState } from '@src/redux';
4-
import { countersActions, countersSelectors } from '@src/redux/counters';
5-
import { SFCCounter } from '@src/components';
4+
import { countersActions, countersSelectors } from '../features/counters';
5+
import { SFCCounter } from '../components';
66

77
export interface SFCCounterConnectedExtendedProps {
88
initialCount: number;
99
}
1010

11-
const mapStateToProps = (state: RootState, ownProps: SFCCounterConnectedExtendedProps) => ({
12-
count: countersSelectors.getReduxCounter(state) + ownProps.initialCount,
11+
const mapStateToProps = (state: Types.RootState, ownProps: SFCCounterConnectedExtendedProps) => ({
12+
count: countersSelectors.getReduxCounter(state.counters) + ownProps.initialCount,
1313
});
1414

1515
export const SFCCounterConnectedExtended = connect(mapStateToProps, {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
22

3-
import { SFCCounterConnectedExtended } from '@src/connected';
3+
import { SFCCounterConnectedExtended } from '../connected';
44

55
export default () => <SFCCounterConnectedExtended label={'SFCCounterConnectedExtended'} initialCount={10} />;

playground/src/connected/sfc-counter-connected-verbose.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { bindActionCreators } from 'redux';
1+
import Types from 'Types';
2+
import { bindActionCreators, Dispatch } from 'redux';
23
import { connect } from 'react-redux';
34

4-
import { RootState, Dispatch } from '@src/redux';
5-
import { countersActions } from '@src/redux/counters';
6-
import { SFCCounter } from '@src/components';
5+
import { countersActions } from '../features/counters';
6+
import { SFCCounter } from '../components';
77

8-
const mapStateToProps = (state: RootState) => ({
8+
const mapStateToProps = (state: Types.RootState) => ({
99
count: state.counters.reduxCounter,
1010
});
1111

playground/src/connected/sfc-counter-connected-verbose.usage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22

3-
import { SFCCounterConnectedVerbose } from '@src/connected';
3+
import { SFCCounterConnectedVerbose } from '../connected';
44

55
export default () => (
66
<SFCCounterConnectedVerbose

playground/src/connected/sfc-counter-connected.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import Types from 'Types';
12
import { connect } from 'react-redux';
23

3-
import { RootState } from '@src/redux';
4-
import { countersActions, countersSelectors } from '@src/redux/counters';
5-
import { SFCCounter } from '@src/components';
4+
import { countersActions, countersSelectors } from '../features/counters';
5+
import { SFCCounter } from '../components';
66

7-
const mapStateToProps = (state: RootState) => ({
8-
count: countersSelectors.getReduxCounter(state),
7+
const mapStateToProps = (state: Types.RootState) => ({
8+
count: countersSelectors.getReduxCounter(state.counters),
99
});
1010

1111
export const SFCCounterConnected = connect(mapStateToProps, {

playground/src/connected/sfc-counter-connected.usage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22

3-
import { SFCCounterConnected } from '@src/connected';
3+
import { SFCCounterConnected } from '../connected';
44

55
export default () => (
66
<SFCCounterConnected

0 commit comments

Comments
 (0)