More redux work. User creation/retrieve
This commit is contained in:
parent
2c67b68512
commit
2dfa745c6a
16 changed files with 212 additions and 110 deletions
|
|
@ -26,4 +26,10 @@ class OnUserCreatedAction extends AppActions {
|
|||
final String userId;
|
||||
|
||||
OnUserCreatedAction(this.userId);
|
||||
}
|
||||
|
||||
class OnUserLangAction extends AppActions {
|
||||
final String lang;
|
||||
|
||||
OnUserLangAction(this.lang);
|
||||
}
|
||||
|
|
@ -1,22 +1,39 @@
|
|||
import 'package:redux/redux.dart';
|
||||
import 'actions.dart';
|
||||
import '../models/yourLocation.dart';
|
||||
import '../models/appState.dart';
|
||||
|
||||
import '../globals.dart' as globals;
|
||||
import '../models/appState.dart';
|
||||
import '../models/firesApi.dart';
|
||||
import '../models/yourLocation.dart';
|
||||
import 'actions.dart';
|
||||
|
||||
// A middleware takes in 3 parameters: your Store, which you can use to
|
||||
// read state or dispatch new actions, the action that was dispatched,
|
||||
// and a `next` function. The first two you know about, and the `next`
|
||||
// function is responsible for sending the action to your Reducer, or
|
||||
// read state or dispatch new actions, the action that was dispatched,
|
||||
// and a `next` function. The first two you know about, and the `next`
|
||||
// function is responsible for sending the action to your Reducer, or
|
||||
// the next Middleware if you provide more than one.
|
||||
//
|
||||
// Middleware do not return any values themselves. They simply forward
|
||||
// actions on to the Reducer or swallow actions in some special cases.
|
||||
void fetchYourLocationsMiddleware(Store<AppState> store, action, NextDispatcher next) {
|
||||
// If our Middleware encounters a `FetchYourLocationAction`
|
||||
if (action is FetchYourLocationsAction) {
|
||||
|
||||
FiresApi api = globals.getIt.get<FiresApi>();
|
||||
|
||||
void fetchYourLocationsMiddleware(
|
||||
Store<AppState> store, action, NextDispatcher next) {
|
||||
// If our Middleware encounters a `FetchYourLocationAction`
|
||||
|
||||
if (action is OnUserLangAction) {
|
||||
// I can create the user with the lang and the token
|
||||
if (store.state.user.token != null)
|
||||
createUser(store, action.lang, store.state.user.token);
|
||||
}
|
||||
|
||||
if (action is OnUserTokenAction) {
|
||||
// I can create the user with the lang and the token
|
||||
if (store.state.user.lang != null)
|
||||
createUser(store, store.state.user.lang, action.token);
|
||||
}
|
||||
|
||||
if (action is FetchYourLocationsAction) {
|
||||
final api = globals.getIt.get<FiresApi>();
|
||||
|
||||
// Use the api to fetch the YourLocations
|
||||
|
|
@ -28,9 +45,17 @@ void fetchYourLocationsMiddleware(Store<AppState> store, action, NextDispatcher
|
|||
// If it fails, dispatch a failure action. The reducer will
|
||||
// update the state with the error.
|
||||
store.dispatch(new FetchYourLocationsFailedAction(error));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Make sure our actions continue on to the reducer.
|
||||
next(action);
|
||||
}
|
||||
}
|
||||
|
||||
void createUser(store, lang, token) {
|
||||
assert(token != null, "User lang is null");
|
||||
assert(token != null, "User mobile token is null");
|
||||
api
|
||||
.createUser(store.state, token, lang)
|
||||
.then((userId) => store.dispatch(new OnUserCreatedAction(userId)));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,22 +1,22 @@
|
|||
import '../models/appState.dart';
|
||||
import 'yourLocationsReducer.dart';
|
||||
import 'errorReducer.dart';
|
||||
import 'fireMapReducer.dart';
|
||||
import 'loadedReducer.dart';
|
||||
import 'loadingReducer.dart';
|
||||
import 'errorReducer.dart';
|
||||
import 'userIdReducer.dart';
|
||||
import 'userTokenReducer.dart';
|
||||
import 'fireMapReducer.dart';
|
||||
import 'userReducer.dart';
|
||||
import 'yourLocationsReducer.dart';
|
||||
|
||||
// We create the State reducer by combining many smaller reducers into one!
|
||||
AppState appStateReducer(AppState state, action) {
|
||||
|
||||
return AppState(
|
||||
return AppState(
|
||||
yourLocations: yourLocationsReducer(state.yourLocations, action),
|
||||
userId: userReducer(state.userId, action),
|
||||
token: userTokenReducer(state.token, action),
|
||||
user: userReducer(state.user, action),
|
||||
isLoading: loadingReducer(state.isLoading, action),
|
||||
isLoaded: loadedReducer(state.isLoaded, action),
|
||||
error: errorReducer(state.error, action),
|
||||
fireMapState: fireMapReducer(state.fireMapState, action)
|
||||
);
|
||||
fireMapState: fireMapReducer(state.fireMapState, action),
|
||||
firesApiKey: state.firesApiKey,
|
||||
firesApiUrl: state.firesApiUrl,
|
||||
gmapKey: state.gmapKey
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
String userReducer(String userId, action) {
|
||||
return userId;
|
||||
}
|
||||
9
lib/redux/userReducer.dart
Normal file
9
lib/redux/userReducer.dart
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import 'actions.dart';
|
||||
import '../models/user.dart';
|
||||
|
||||
User userReducer(User user, action) {
|
||||
if (action is OnUserCreatedAction) return user.copyWith(userId: action.userId);
|
||||
if (action is OnUserTokenAction) return user.copyWith(token: action.token);
|
||||
if (action is OnUserLangAction) return user.copyWith(lang: action.lang);
|
||||
return user;
|
||||
}
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
String userTokenReducer(String userToken, action) {
|
||||
return userToken;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue