Exemple #1
0
        public static object fetchNotifications(int pageNumber)
        {
            return(new ThunkAction <AppState>((dispatcher, getState) => {
                return NotificationApi.FetchNotifications(pageNumber)
                .Then(notificationResponse => {
                    var oldResults = notificationResponse.results;
                    if (oldResults != null && oldResults.Count > 0)
                    {
                        oldResults.ForEach(item => {
                            var data = item.data;
                            var user = new User {
                                id = data.userId,
                                fullName = data.fullname,
                                avatar = data.avatarWithCDN
                            };
                            dispatcher.dispatch(new UserMapAction {
                                userMap = new Dictionary <string, User> {
                                    { data.userId, user }
                                }
                            });
                        });
                    }

                    var notifications = getState().notificationState.notifications;
                    if (pageNumber == 1)
                    {
                        notifications = notificationResponse.results;
                    }
                    else
                    {
                        if (pageNumber <= notificationResponse.pageTotal)
                        {
                            notifications.AddRange(notificationResponse.results);
                        }
                    }

                    dispatcher.dispatch(new FetchNotificationsSuccessAction
                    {
                        pageTotal = notificationResponse.pageTotal, notifications = notifications
                    });
                })
                .Catch(err => { dispatcher.dispatch(new FetchNotificationsFailureAction()); });
            }));
        }
        public static object fetchNotifications(int pageNumber)
        {
            return(new ThunkAction <AppState>((dispatcher, getState) => {
                return NotificationApi.FetchNotifications(pageNumber)
                .Then(notificationResponse => {
                    var results = notificationResponse.results;
                    if (results != null && results.Count > 0)
                    {
                        var userMap = notificationResponse.userMap;
                        results.ForEach(item => {
                            var data = item.data;
                            var user = new User {
                                id = data.userId,
                                fullName = data.fullname,
                                avatar = data.avatarWithCDN
                            };
                            if (userMap.ContainsKey(key: data.userId))
                            {
                                userMap[key: data.userId] = user;
                            }
                            else
                            {
                                userMap.Add(key: data.userId, value: user);
                            }
                        });
                        dispatcher.dispatch(new UserMapAction {
                            userMap = userMap
                        });
                    }

                    dispatcher.dispatch(new FetchNotificationsSuccessAction {
                        page = notificationResponse.page,
                        pageNumber = pageNumber,
                        pageTotal = notificationResponse.pageTotal,
                        notifications = results,
                        mentions = notificationResponse.userMap.Values.ToList()
                    });
                })
                .Catch(err => { dispatcher.dispatch(new FetchNotificationsFailureAction()); });
            }));
        }
        public static object fetchNotifications(int pageNumber)
        {
            return(new ThunkAction <AppState>((dispatcher, getState) => {
                return NotificationApi.FetchNotifications(pageNumber)
                .Then(notificationResponse => {
                    var oldResults = notificationResponse.results;
                    if (oldResults != null && oldResults.Count > 0)
                    {
                        var userMap = notificationResponse.userMap;
                        oldResults.ForEach(item => {
                            var data = item.data;
                            var user = new User {
                                id = data.userId,
                                fullName = data.fullname,
                                avatar = data.avatarWithCDN
                            };
                            if (userMap.ContainsKey(data.userId))
                            {
                                userMap[data.userId] = user;
                            }
                            else
                            {
                                userMap.Add(data.userId, user);
                            }
                        });
                        dispatcher.dispatch(new UserMapAction {
                            userMap = userMap
                        });
                    }

                    var mentions = getState().notificationState.mentions;
                    var notifications = getState().notificationState.notifications;
                    if (pageNumber == 1)
                    {
                        notifications = notificationResponse.results;
                        mentions = notificationResponse.userMap.Values.ToList();
                    }
                    else
                    {
                        if (pageNumber <= notificationResponse.pageTotal)
                        {
                            notifications.AddRange(notificationResponse.results);
                        }

                        foreach (var user in notificationResponse.userMap.Values)
                        {
                            if (!mentions.Contains(user))
                            {
                                mentions.Add(user);
                            }
                        }
                    }

                    dispatcher.dispatch(new FetchNotificationsSuccessAction {
                        pageTotal = notificationResponse.pageTotal, notifications = notifications,
                        mentions = mentions
                    });
                })
                .Catch(err => { dispatcher.dispatch(new FetchNotificationsFailureAction()); });
            }));
        }