Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4624af3

Browse files
author
okan.cetin
committed
#000 okan move async callback into actions and call from reducers
1 parentcba2fc1 commit4624af3

File tree

5 files changed

+48
-49
lines changed

5 files changed

+48
-49
lines changed

‎src/app/actions/authActions.ts‎

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,23 @@ import { Action, SyncAction, AsyncAction } from '../utils/actionHelpers'
22
import{push}from'react-router-redux'
33

44

5-
consttokenKey="auth";
6-
7-
85
@Action
9-
exportclassLoginRequestextendsAsyncAction{
6+
exportclassLoginextendsAsyncAction{
107
constructor(publicusername:string,publicpassword:string){
118
super();
12-
13-
this.then(dispatch=>{
14-
fetch(`https://httpbin.org/get?username=${username}&password=${password}`)
15-
.then(x=>x.json())
16-
.then(data=>{
17-
dispatch(newLogin(data.args.username+"|"+data.args.password));
18-
dispatch(push("dashboard"));
19-
});
20-
returnthis;
21-
});
229
}
2310
}
2411

2512
@Action
26-
exportclassLoginextendsSyncAction{
27-
constructor(publictoken:string){
28-
super();
29-
}
13+
exportclassLogoutextendsAsyncAction{}
3014

31-
getTokenKey(){
32-
returntokenKey;
33-
}
34-
}
3515

3616
@Action
37-
exportclassLogoutextendsAsyncAction{
38-
constructor(){
17+
exportclassSetTokenextendsSyncAction{
18+
constructor(publictoken:string){
3919
super();
40-
41-
this.then(dispatch=>{
42-
dispatch(push("dashboard"));
43-
returnthis;
44-
});
4520
}
46-
4721
getTokenKey(){
48-
returntokenKey;
22+
return"auth";
4923
}
5024
}

‎src/app/containers/Login.tsx‎

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

33
import{connect}from'react-redux'
44
import{Dispatch}from"redux";
5-
import{LoginRequest}from'../actions'
5+
import{Login}from'../actions'
66

77
importPaperfrom'material-ui/Paper';
88
importTextFieldfrom'material-ui/TextField';
@@ -31,7 +31,7 @@ class LoginContainer extends React.Component<LoginProps, any>{
3131
varusername=this.refs.username.getValue();
3232
varpassword=this.refs.password.getValue();
3333

34-
this.props.dispatch(newLoginRequest(username,password));
34+
this.props.dispatch(newLogin(username,password));
3535
}
3636

3737
render(){

‎src/app/reducers/authReducer.ts‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import{ReducerBuilder}from'../utils/reducerBuilder'
2-
import{Login,Logout}from'../actions'
2+
import{Login,Logout,SetToken}from'../actions'
33
import{push}from'react-router-redux'
44

55

@@ -11,21 +11,44 @@ export const authReducer = new ReducerBuilder<AuthState>()
1111
.init({})
1212

1313
.handle(Login,(state,action)=>{
14-
consttoken=action.token;
1514

16-
localStorage.setItem(action.getTokenKey(),token);
15+
action.then(dispatch=>{
16+
fetch(`https://httpbin.org/get?username=${action.username}&password=${action.password}`)
17+
.then(x=>x.json())
18+
.then(data=>{
19+
dispatch(newSetToken(data.args.username+"|"+data.args.password));
20+
dispatch(push("dashboard"))
21+
});
22+
});
1723

18-
return{
19-
token:token
20-
};
24+
returnnull;
2125
})
2226

27+
2328
.handle(Logout,(state,action)=>{
24-
localStorage.removeItem(action.getTokenKey());
29+
30+
action.then(dispatch=>{
31+
dispatch(newSetToken(null));
32+
dispatch(push("dashboard"));
33+
});
34+
35+
returnnull;
36+
})
37+
38+
39+
.handle(SetToken,(state,action)=>{
40+
41+
if(action.token!=null){
42+
localStorage.setItem(action.getTokenKey(),action.token);
43+
}
44+
else{
45+
localStorage.removeItem(action.getTokenKey());
46+
}
2547

2648
return{
27-
token:null
49+
token:action.token
2850
};
2951
})
3052

53+
3154
.build();

‎src/app/utils/actionHelpers.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export abstract class SyncAction implements Redux.Action {
1414
}
1515
}
1616

17-
exportabstractclassAsyncActionextendsSyncActionimplementsPromise<Dispatch<any>>{
17+
typeNullableDispatch=Dispatch<any>|void;
18+
19+
exportabstractclassAsyncActionextendsSyncActionimplementsPromise<NullableDispatch>{
1820
async:boolean=true;
1921
resolve:(value?:Dispatch<any>)=>void;
2022
reject:(reason?:any)=>void;
@@ -23,13 +25,12 @@ export abstract class AsyncAction extends SyncAction implements Promise<Dispatch
2325
this.reject=reject;
2426
});
2527

26-
then(onfulfilled?:(value:Dispatch<any>)=>Dispatch<any>|PromiseLike<Dispatch<any>>,
27-
onrejected?:(reason:any)=>void):Promise<Dispatch<any>>{
28+
then(onfulfilled?:(value:Dispatch<any>)=>NullableDispatch|PromiseLike<NullableDispatch>,onrejected?:(reason:any)=>void):Promise<NullableDispatch>{
2829
returnthis.promise.then(onfulfilled,onrejected);
2930
}
3031

31-
catch(onrejected?:(reason:any)=>Dispatch<any>|PromiseLike<Dispatch<any>>):Promise<Dispatch<any>>{
32-
returnthis.promise.then(onrejected);
32+
catch(onrejected?:(reason:any)=>NullableDispatch|PromiseLike<NullableDispatch>):Promise<NullableDispatch>{
33+
returnthis.promise.catch(onrejected);
3334
}
3435
}
3536

‎src/app/utils/reducerBuilder.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import*as_from'lodash'
22

3-
import{Reducer}from'redux'
43
import{SyncAction,IAction}from'../utils/actionHelpers'
54

65

6+
typeReducer<State,ActionTypeextendsSyncAction>=(state:State,action?:ActionType)=>State;
7+
78
exportclassReducerBuilder<State>{
89

9-
actions:{[type:string]:Reducer<State>}={};
10+
actions:{[type:string]:Reducer<State,SyncAction>}={};
1011
initState:State;
1112

1213

@@ -15,7 +16,7 @@ export class ReducerBuilder<State> {
1516
returnthis;
1617
}
1718

18-
publichandle<TextendsSyncAction>(actionType:IAction<T>,actionBody:(state:State,action?:T)=>State){
19+
publichandle<TextendsSyncAction>(actionType:IAction<T>,actionBody:Reducer<State,T>){
1920
this.actions[actionType.prototype.type]=actionBody;
2021
returnthis;
2122
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp