More redux and api work
This commit is contained in:
parent
866e776389
commit
2c67b68512
17 changed files with 256 additions and 11 deletions
2
lib/redux/actions.dart
Normal file
2
lib/redux/actions.dart
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export 'appActions.dart';
|
||||
export 'yourLocationActions.dart';
|
||||
29
lib/redux/appActions.dart
Normal file
29
lib/redux/appActions.dart
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import '../models/yourLocation.dart';
|
||||
|
||||
abstract class AppActions {}
|
||||
|
||||
class FetchYourLocationsAction extends AppActions {}
|
||||
|
||||
class FetchYourLocationsSucceededAction extends AppActions {
|
||||
final List<YourLocation> fetchedYourLocations;
|
||||
|
||||
FetchYourLocationsSucceededAction(this.fetchedYourLocations);
|
||||
}
|
||||
|
||||
class FetchYourLocationsFailedAction extends AppActions {
|
||||
final Exception error;
|
||||
|
||||
FetchYourLocationsFailedAction(this.error);
|
||||
}
|
||||
|
||||
class OnUserTokenAction extends AppActions {
|
||||
final String token;
|
||||
|
||||
OnUserTokenAction(this.token);
|
||||
}
|
||||
|
||||
class OnUserCreatedAction extends AppActions {
|
||||
final String userId;
|
||||
|
||||
OnUserCreatedAction(this.userId);
|
||||
}
|
||||
3
lib/redux/errorReducer.dart
Normal file
3
lib/redux/errorReducer.dart
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
String errorReducer(String error, action) {
|
||||
return error;
|
||||
}
|
||||
36
lib/redux/fetchDataMiddleware.dart
Normal file
36
lib/redux/fetchDataMiddleware.dart
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import 'package:redux/redux.dart';
|
||||
import 'actions.dart';
|
||||
import '../models/yourLocation.dart';
|
||||
import '../models/appState.dart';
|
||||
import '../globals.dart' as globals;
|
||||
import '../models/firesApi.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
|
||||
// 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) {
|
||||
|
||||
final api = globals.getIt.get<FiresApi>();
|
||||
|
||||
// Use the api to fetch the YourLocations
|
||||
api.fetchYourLocations().then((List<YourLocation> YourLocations) {
|
||||
// If it succeeds, dispatch a success action with the YourLocations.
|
||||
// Our reducer will then update the State using these YourLocations.
|
||||
store.dispatch(new FetchYourLocationsSucceededAction(YourLocations));
|
||||
}).catchError((Exception error) {
|
||||
// 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);
|
||||
}
|
||||
5
lib/redux/fireMapReducer.dart
Normal file
5
lib/redux/fireMapReducer.dart
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import '../models/fireMapState.dart';
|
||||
|
||||
FireMapState fireMapReducer(FireMapState state, action) {
|
||||
return state;
|
||||
}
|
||||
3
lib/redux/loadedReducer.dart
Normal file
3
lib/redux/loadedReducer.dart
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
bool loadedReducer(isLoaded, action) {
|
||||
return isLoaded;
|
||||
}
|
||||
3
lib/redux/loadingReducer.dart
Normal file
3
lib/redux/loadingReducer.dart
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
bool loadingReducer(isLoading, action) {
|
||||
return isLoading;
|
||||
}
|
||||
22
lib/redux/reducers.dart
Normal file
22
lib/redux/reducers.dart
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import '../models/appState.dart';
|
||||
import 'yourLocationsReducer.dart';
|
||||
import 'loadedReducer.dart';
|
||||
import 'loadingReducer.dart';
|
||||
import 'errorReducer.dart';
|
||||
import 'userIdReducer.dart';
|
||||
import 'userTokenReducer.dart';
|
||||
import 'fireMapReducer.dart';
|
||||
|
||||
// We create the State reducer by combining many smaller reducers into one!
|
||||
AppState appStateReducer(AppState state, action) {
|
||||
|
||||
return AppState(
|
||||
yourLocations: yourLocationsReducer(state.yourLocations, action),
|
||||
userId: userReducer(state.userId, action),
|
||||
token: userTokenReducer(state.token, action),
|
||||
isLoading: loadingReducer(state.isLoading, action),
|
||||
isLoaded: loadedReducer(state.isLoaded, action),
|
||||
error: errorReducer(state.error, action),
|
||||
fireMapState: fireMapReducer(state.fireMapState, action)
|
||||
);
|
||||
}
|
||||
3
lib/redux/userIdReducer.dart
Normal file
3
lib/redux/userIdReducer.dart
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
String userReducer(String userId, action) {
|
||||
return userId;
|
||||
}
|
||||
3
lib/redux/userTokenReducer.dart
Normal file
3
lib/redux/userTokenReducer.dart
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
String userTokenReducer(String userToken, action) {
|
||||
return userToken;
|
||||
}
|
||||
41
lib/redux/yourLocationActions.dart
Normal file
41
lib/redux/yourLocationActions.dart
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import 'package:bson_objectid/bson_objectid.dart';
|
||||
|
||||
import '../models/yourLocation.dart';
|
||||
|
||||
abstract class YourLocationActions {}
|
||||
|
||||
class AddYourLocationAction extends YourLocationActions {
|
||||
YourLocation loc;
|
||||
|
||||
AddYourLocationAction(this.loc);
|
||||
}
|
||||
|
||||
class DeleteYourLocationAction extends YourLocationActions {
|
||||
ObjectId id;
|
||||
DeleteYourLocationAction(this.id);
|
||||
}
|
||||
|
||||
class UpdateYourLocationAction extends YourLocationActions {
|
||||
ObjectId id;
|
||||
YourLocation loc;
|
||||
|
||||
UpdateYourLocationAction(this.id, this.loc);
|
||||
}
|
||||
|
||||
class ToggleSubscriptionAction extends YourLocationActions {
|
||||
ObjectId id;
|
||||
|
||||
ToggleSubscriptionAction(this.id);
|
||||
}
|
||||
|
||||
class SubscribeAction extends YourLocationActions {
|
||||
ObjectId id;
|
||||
|
||||
SubscribeAction(this.id);
|
||||
}
|
||||
|
||||
class UnSubscribeAction extends YourLocationActions {
|
||||
ObjectId id;
|
||||
|
||||
UnSubscribeAction(this.id);
|
||||
}
|
||||
51
lib/redux/yourLocationsReducer.dart
Normal file
51
lib/redux/yourLocationsReducer.dart
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import 'package:redux/redux.dart';
|
||||
|
||||
import '../models/yourLocation.dart';
|
||||
import 'actions.dart';
|
||||
|
||||
final yourLocationsReducer = combineReducers<List<YourLocation>>([
|
||||
new TypedReducer<List<YourLocation>, AddYourLocationAction>(_addYourLocation),
|
||||
new TypedReducer<List<YourLocation>, DeleteYourLocationAction>(
|
||||
_deleteYourLocation),
|
||||
new TypedReducer<List<YourLocation>, UpdateYourLocationAction>(
|
||||
_updateYourLocation),
|
||||
|
||||
/* new TypedReducer<List<YourLocation>, ToggleSubscriptionAction>(_toggleSubscriptionAction),
|
||||
new TypedReducer<List<YourLocation>, SubscribeAction>(_setLoadedYourLocations),
|
||||
new TypedReducer<List<YourLocation>, UnSubscribeAction>(_setNoYourLocations), */
|
||||
]);
|
||||
|
||||
List<YourLocation> _addYourLocation(
|
||||
List<YourLocation> yourLocations, AddYourLocationAction action) {
|
||||
return new List.from(yourLocations)..add(action.loc);
|
||||
}
|
||||
|
||||
List<YourLocation> _deleteYourLocation(
|
||||
List<YourLocation> yourLocations, DeleteYourLocationAction action) {
|
||||
return yourLocations
|
||||
.where((yourLocation) => yourLocation.id != action.id)
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<YourLocation> _updateYourLocation(
|
||||
List<YourLocation> yourLocations, UpdateYourLocationAction action) {
|
||||
return yourLocations
|
||||
.map((yourLocation) =>
|
||||
yourLocation.id == action.id ? action.loc : yourLocation)
|
||||
.toList();
|
||||
}
|
||||
/*
|
||||
List<YourLocation> _toggleSubscriptionAction(List<YourLocation> yourLocations, UpdateYourLocationAction action) {
|
||||
return yourLocations
|
||||
.map((yourLocation) => yourLocation.id == action.id ? action.loc : yourLocation)
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<YourLocation> _setLoadedYourLocations(List<YourLocation> yourLocations, YourLocationsLoadedAction action) {
|
||||
return action.yourLocations;
|
||||
}
|
||||
|
||||
List<YourLocation> _setNoYourLocations(List<YourLocation> yourLocations, YourLocationsNotLoadedAction action) {
|
||||
return [];
|
||||
}
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue