Fetch fire data on new locations

This commit is contained in:
Vicente J. Ruiz Jurado 2018-08-17 06:49:44 +02:00
parent d40859c5d8
commit 46026917c7

View file

@ -58,6 +58,7 @@ void fetchDataMiddleware(Store<AppState> store, action, NextDispatcher next) {
store.dispatch(new AddedYourLocationAction(action.loc)); store.dispatch(new AddedYourLocationAction(action.loc));
persistYourLocations(store.state.yourLocations); persistYourLocations(store.state.yourLocations);
} }
getFiresStatsInLocation(store, action.loc);
} }
if (action is DeleteYourLocationAction) { if (action is DeleteYourLocationAction) {
@ -87,13 +88,7 @@ void fetchDataMiddleware(Store<AppState> store, action, NextDispatcher next) {
} }
if (action is ShowYourLocationMapAction) { if (action is ShowYourLocationMapAction) {
api getFiresStatsInLocation(store, action.loc);
.getFiresInLocation(
state: store.state,
lat: action.loc.lat,
lon: action.loc.lon,
distance: action.loc.distance)
.then((result) => store.dispatch(result));
} }
if (action is ShowFireNotificationMapAction) { if (action is ShowFireNotificationMapAction) {
@ -111,9 +106,6 @@ void fetchDataMiddleware(Store<AppState> store, action, NextDispatcher next) {
lon: action.loc.lon, lon: action.loc.lon,
distance: action.loc.distance) distance: action.loc.distance)
.then((result) => store.dispatch(result))); .then((result) => store.dispatch(result)));
else {
// FIXME do something?
}
store.dispatch(new UpdatedYourLocationAction(action.loc)); store.dispatch(new UpdatedYourLocationAction(action.loc));
persistYourLocations(store.state.yourLocations); persistYourLocations(store.state.yourLocations);
} }
@ -150,12 +142,12 @@ void fetchDataMiddleware(Store<AppState> store, action, NextDispatcher next) {
if (action is FetchYourLocationsAction) { if (action is FetchYourLocationsAction) {
// Use the api to fetch the YourLocations // Use the api to fetch the YourLocations
loadYourLocations().then((localLocations) { loadYourLocations().then((localLocations) {
api api
.fetchYourLocations(store.state) .fetchYourLocations(store.state)
.then((List<YourLocation> subscribedLocations) { .then((List<YourLocation> subscribedLocations) {
// If it succeeds, dispatch a success action with the YourLocations. // If it succeeds, dispatch a success action with the YourLocations.
// Our reducer will then update the State using these YourLocations. // Our reducer will then update the State using these YourLocations.
// print('Subscribed to: ${subscribedLocations.length}'); // print('Subscribed to: ${subscribedLocations.length}');
if (subscribedLocations is List) { if (subscribedLocations is List) {
// unsubscribe all locally to sync the subs state // unsubscribe all locally to sync the subs state
localLocations.forEach((location) => location.subscribed = false); localLocations.forEach((location) => location.subscribed = false);
@ -173,12 +165,12 @@ void fetchDataMiddleware(Store<AppState> store, action, NextDispatcher next) {
localLocations.forEach((yl) { localLocations.forEach((yl) {
api api
.getFiresInLocation( .getFiresInLocation(
state: store.state, state: store.state,
lat: yl.lat, lat: yl.lat,
lon: yl.lon, lon: yl.lon,
distance: yl.distance) distance: yl.distance)
.then((value) { .then((value) {
yl.currentNumFires = value.numFires; yl.currentNumFires = value.numFires;
store.dispatch(new UpdateYourLocationAction(yl)); store.dispatch(new UpdateYourLocationAction(yl));
}); });
@ -211,18 +203,21 @@ void fetchDataMiddleware(Store<AppState> store, action, NextDispatcher next) {
} }
if (action is FetchMonitoredAreasAction) { if (action is FetchMonitoredAreasAction) {
api.getMonitoredAreas(state: store.state).then((result) { // store.dispatch() api.getMonitoredAreas(state: store.state).then((result) {
store.dispatch( // store.dispatch()
new FetchMonitoredAreasSucceededAction(result)); store.dispatch(new FetchMonitoredAreasSucceededAction(result));
}); });
} }
if (action is MarkFireAsFalsePositiveAction) { if (action is MarkFireAsFalsePositiveAction) {
api.markFalsePositive(store.state, store.state.user.token, action.notif.sealed, action.type).then((result) { api
.markFalsePositive(store.state, store.state.user.token,
action.notif.sealed, action.type)
.then((result) {
if (result) { if (result) {
// Not necessary // Not necessary
// store.dispatch(new UpdatedFireNotificationAction(action.notif));
getFiresStatsInFire(store, action.notif); getFiresStatsInFire(store, action.notif);
store.dispatch(new UpdatedFireNotificationAction(action.notif));
} }
}); });
} }
@ -231,6 +226,20 @@ void fetchDataMiddleware(Store<AppState> store, action, NextDispatcher next) {
next(action); next(action);
} }
void getFiresStatsInLocation(Store<AppState> store, YourLocation loc) {
api
.getFiresInLocation(
state: store.state,
lat: loc.lat,
lon: loc.lon,
distance: loc.distance)
.then((result) {
store.dispatch(result);
loc.currentNumFires = result.numFires;
store.dispatch(new UpdateYourLocationAction(loc));
});
}
void getFiresStatsInFire(Store<AppState> store, FireNotification notif) { void getFiresStatsInFire(Store<AppState> store, FireNotification notif) {
api api
.getFiresInLocation( .getFiresInLocation(