Skip to content

Commit ea71eb8

Browse files
committed
code refactoring and cleaning
1 parent 2678e29 commit ea71eb8

22 files changed

+67
-357
lines changed

Diff for: README.md

+24-24
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ const incrementCount = () => count++;
102102

103103
export default () => (
104104
<SFCCounter
105-
label='SFCCounter'
105+
label={'SFCCounter'}
106106
count={count}
107107
onIncrement={incrementCount}
108108
/>
@@ -148,7 +148,6 @@ export default () => (
148148
/>
149149
);
150150

151-
152151
```
153152
</p></details>
154153

@@ -270,7 +269,7 @@ export const StatefulCounterWithInitialCount: React.ComponentClass<StatefulCount
270269
</div>
271270
);
272271
}
273-
}
272+
};
274273

275274
```
276275

@@ -305,7 +304,7 @@ import * as React from 'react';
305304
export interface GenericListProps<T> {
306305
items: T[],
307306
itemRenderer: (item: T) => JSX.Element,
308-
};
307+
}
309308

310309
export class GenericList<T> extends React.Component<GenericListProps<T>, {}> {
311310
render() {
@@ -326,7 +325,7 @@ export class GenericList<T> extends React.Component<GenericListProps<T>, {}> {
326325
```tsx
327326
import * as React from 'react';
328327

329-
import { IUser } from '@src/models'
328+
import { IUser } from '@src/models';
330329
import { GenericList } from '@src/components';
331330

332331
export const UserList = class extends GenericList<IUser> { };
@@ -373,7 +372,7 @@ import { SFCCounterConnected } from '@src/connected';
373372

374373
export default () => (
375374
<SFCCounterConnected
376-
label="SFCCounterConnected"
375+
label={'SFCCounterConnected'}
377376
/>
378377
);
379378

@@ -414,7 +413,7 @@ import { SFCCounterConnectedVerbose } from '@src/connected';
414413

415414
export default () => (
416415
<SFCCounterConnectedVerbose
417-
label="SFCCounterConnectedVerbose"
416+
label={'SFCCounterConnectedVerbose'}
418417
/>
419418
);
420419

@@ -455,7 +454,7 @@ import { SFCCounterConnectedExtended } from '@src/connected';
455454

456455
export default () => (
457456
<SFCCounterConnectedExtended
458-
label="SFCCounterConnectedExtended"
457+
label={'SFCCounterConnectedExtended'}
459458
initialCount={10}
460459
/>
461460
);
@@ -495,7 +494,7 @@ export function withState<WrappedComponentProps extends RequiredProps>(
495494

496495
state: State = {
497496
count: 0,
498-
}
497+
};
499498

500499
handleIncrement = () => {
501500
this.setState({ count: this.state.count + 1 });
@@ -650,7 +649,7 @@ export type Actions = {
650649
};
651650

652651
// Action Creators
653-
export const actionCreatorss = {
652+
export const actionCreators = {
654653
incrementSfc: (): Actions[typeof INCREMENT_SFC] => ({
655654
type: INCREMENT_SFC,
656655
}),
@@ -666,7 +665,7 @@ export const actionCreatorss = {
666665
import store from '@src/store';
667666
import { actionCreators } from '@src/redux/counters';
668667

669-
store.dispatch(actionCreators.incrementSfc(1)); // Error: Expected 0 arguments, but got 1.
668+
// store.dispatch(actionCreators.incrementSfc(1)); // Error: Expected 0 arguments, but got 1.
670669
store.dispatch(actionCreators.incrementSfc()); // OK => { type: "INCREMENT_SFC" }
671670

672671
```
@@ -710,7 +709,7 @@ Relevant TypeScript Docs references:
710709
- [Discriminated Union types](https://www.typescriptlang.org/docs/handbook/advanced-types.html)
711710
- [Mapped types](https://www.typescriptlang.org/docs/handbook/advanced-types.html) e.g. `Readonly` & `Partial`
712711

713-
### Typing Reducer State
712+
### Reducer State
714713
Declare reducer `State` type definition with readonly modifier for `type level` immutability
715714
```ts
716715
export type State = {
@@ -796,16 +795,18 @@ export const reducer = combineReducers<State, RootAction>({
796795

797796
### Reducer with static `type` property from helper factory function - `createActionCreator`
798797
```ts
799-
export default function reducer(state = 0, action: RootAction): State {
800-
switch (action.type) {
801-
case actionCreators.increment.type:
802-
return state.counter + 1;
803-
case actionCreators.decrement.type:
804-
return state.counter - 1;
805-
806-
default: return state;
807-
}
808-
}
798+
export const reducer: Reducer<State> =
799+
(state = 0, action: RootAction) => {
800+
switch (action.type) {
801+
case actionCreators.increment.type:
802+
return state + 1;
803+
804+
case actionCreators.decrement.type:
805+
return state - 1;
806+
807+
default: return state;
808+
}
809+
};
809810
```
810811

811812
---
@@ -869,8 +870,7 @@ export const rootReducer = combineReducers<RootState, RootAction>({
869870
```tsx
870871
import { createStore, applyMiddleware, compose } from 'redux';
871872
import { createEpicMiddleware } from 'redux-observable';
872-
import { rootReducer, rootEpic } from '@src/redux';
873-
import { RootState } from '@src/redux';
873+
import { rootReducer, rootEpic, RootState } from '@src/redux';
874874

875875
const composeEnhancers = (
876876
process.env.NODE_ENV === 'development' &&

Diff for: generator/content/2_redux.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Relevant TypeScript Docs references:
5151
- [Discriminated Union types](https://www.typescriptlang.org/docs/handbook/advanced-types.html)
5252
- [Mapped types](https://www.typescriptlang.org/docs/handbook/advanced-types.html) e.g. `Readonly` & `Partial`
5353

54-
### Typing Reducer State
54+
### Reducer State
5555
Declare reducer `State` type definition with readonly modifier for `type level` immutability
5656
```ts
5757
export type State = {
@@ -108,16 +108,18 @@ state.countersCollection[0].readonlyCounter2 = 1; // Error, cannot be mutated
108108

109109
### Reducer with static `type` property from helper factory function - `createActionCreator`
110110
```ts
111-
export default function reducer(state = 0, action: RootAction): State {
112-
switch (action.type) {
113-
case actionCreators.increment.type:
114-
return state.counter + 1;
115-
case actionCreators.decrement.type:
116-
return state.counter - 1;
117-
118-
default: return state;
119-
}
120-
}
111+
export const reducer: Reducer<State> =
112+
(state = 0, action: RootAction) => {
113+
switch (action.type) {
114+
case actionCreators.increment.type:
115+
return state + 1;
116+
117+
case actionCreators.decrement.type:
118+
return state - 1;
119+
120+
default: return state;
121+
}
122+
};
121123
```
122124

123125
---

Diff for: playground/src/components/error-message.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const ErrorMessage: React.SFC<{ onReset: () => any }> = ({ onReset }) =>
44
const handleReset = () => {
55
onReset();
66
// TODO: switch location with router
7-
}
7+
};
88

99
return (
1010
<div>

Diff for: playground/src/components/generic-list.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as React from 'react';
33
export interface GenericListProps<T> {
44
items: T[],
55
itemRenderer: (item: T) => JSX.Element,
6-
};
6+
}
77

88
export class GenericList<T> extends React.Component<GenericListProps<T>, {}> {
99
render() {

Diff for: playground/src/components/generic-list.usage.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22

3-
import { IUser } from '@src/models'
3+
import { IUser } from '@src/models';
44
import { GenericList } from '@src/components';
55

66
export const UserList = class extends GenericList<IUser> { };

Diff for: playground/src/components/sfc-counter.usage.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const incrementCount = () => count++;
77

88
export default () => (
99
<SFCCounter
10-
label='SFCCounter'
10+
label={'SFCCounter'}
1111
count={count}
1212
onIncrement={incrementCount}
1313
/>

Diff for: playground/src/components/sfc-spread-attributes.usage.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ export default () => (
77
style={{ backgroundColor: 'lightcyan' }}
88
/>
99
);
10-

Diff for: playground/src/components/stateful-counter-with-initial-count.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ export const StatefulCounterWithInitialCount: React.ComponentClass<StatefulCount
5151
</div>
5252
);
5353
}
54-
}
54+
};

Diff for: playground/src/connected/sfc-counter-connected-extended.usage.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { SFCCounterConnectedExtended } from '@src/connected';
44

55
export default () => (
66
<SFCCounterConnectedExtended
7-
label="SFCCounterConnectedExtended"
7+
label={'SFCCounterConnectedExtended'}
88
initialCount={10}
99
/>
1010
);

Diff for: playground/src/connected/sfc-counter-connected-verbose.usage.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import { SFCCounterConnectedVerbose } from '@src/connected';
44

55
export default () => (
66
<SFCCounterConnectedVerbose
7-
label="SFCCounterConnectedVerbose"
7+
label={'SFCCounterConnectedVerbose'}
88
/>
99
);

Diff for: playground/src/connected/sfc-counter-connected.usage.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import { SFCCounterConnected } from '@src/connected';
44

55
export default () => (
66
<SFCCounterConnected
7-
label="SFCCounterConnected"
7+
label={'SFCCounterConnected'}
88
/>
99
);

Diff for: playground/src/containers/home.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ export const Home = () => {
1616
<UserListUsage users={[]} />
1717
</section>
1818
);
19-
}
19+
};

Diff for: playground/src/hoc/with-state.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function withState<WrappedComponentProps extends RequiredProps>(
2020

2121
state: State = {
2222
count: 0,
23-
}
23+
};
2424

2525
handleIncrement = () => {
2626
this.setState({ count: this.state.count + 1 });

Diff for: playground/src/models/user.ts

+13-15
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,41 @@ export interface IUserDTO {
88

99
export interface IUser {
1010
constructor: {
11-
fromDTO(user: IUserDTO): IUser;
11+
create(user: IUserDTO): IUser;
1212
}
13-
toDTO(): IUserDTO;
1413

1514
id: string;
1615
firstName: string;
1716
lastName: string;
1817
fullName: string;
18+
19+
serialize(): IUserDTO;
1920
}
2021

2122
export class User implements IUser {
2223
'constructor': typeof User;
2324

24-
constructor(id?: string) {
25-
this.id = id || v4();
25+
id: string = v4();
26+
firstName: string = 'Default';
27+
lastName: string = 'Name';
28+
get fullName(): string {
29+
return `${this.firstName} ${this.lastName}`;
2630
}
2731

28-
static fromDTO(dto: IUserDTO): IUser {
29-
const model = new User(dto.id);
32+
static create(dto: IUserDTO): IUser {
33+
const model = new User();
34+
model.id = dto.id;
3035
model.firstName = dto.first_name;
3136
model.lastName = dto.last_name;
3237

3338
return model;
3439
}
3540

36-
toDTO(): IUserDTO {
41+
serialize(): IUserDTO {
3742
return {
3843
id: this.id,
3944
first_name: this.firstName,
4045
last_name: this.lastName,
41-
}
42-
}
43-
44-
id: string;
45-
firstName: string = 'Default';
46-
lastName: string = 'Name';
47-
get fullName(): string {
48-
return `${this.firstName} ${this.lastName}`;
46+
};
4947
}
5048
}

Diff for: playground/src/redux/counters/actions.usage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import store from '@src/store';
22
import { actionCreators } from '@src/redux/counters';
33

4-
store.dispatch(actionCreators.incrementSfc(1)); // Error: Expected 0 arguments, but got 1.
4+
// store.dispatch(actionCreators.incrementSfc(1)); // Error: Expected 0 arguments, but got 1.
55
store.dispatch(actionCreators.incrementSfc()); // OK => { type: "INCREMENT_SFC" }

Diff for: playground/src/redux/counters/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
export * from './actions';
33
// export * from './epics';
44
export * from './reducer';
5-
export * from './selectors';
5+
import * as CountersSelectors from './selectors';
6+
export { CountersSelectors };
67
// export * from './types';

Diff for: playground/src/redux/types.ts

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export type Reducer<S> = (state: S, action: RootAction) => S;
99
export type Api = {};
1010
// export type ThunkAction<T> = ReduxThunkAction<T, RootState, Api>;
1111

12-
1312
// OLD ACTION MERGING
1413
// import { returntypeof } from 'react-redux-typescript';
1514

Diff for: playground/src/store.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { createStore, applyMiddleware, compose } from 'redux';
22
import { createEpicMiddleware } from 'redux-observable';
3-
import { rootReducer, rootEpic } from '@src/redux';
4-
import { RootState } from '@src/redux';
3+
import { rootReducer, rootEpic, RootState } from '@src/redux';
54

65
const composeEnhancers = (
76
process.env.NODE_ENV === 'development' &&

Diff for: playground/src/types/augmentations.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// }
1010
// }
1111

12-
// import { Reducer } from 'redux';
12+
import { Reducer } from 'redux';
1313

1414
declare module 'redux' {
1515
export type TypedReducer<S, A = any> = (state: S, action: A) => S;

0 commit comments

Comments
 (0)