Coder Social home page Coder Social logo

elvisgn / entity_state Goto Github PK

View Code? Open in Web Editor NEW

This project forked from devslane/entity_state

1.0 0.0 0.0 96 KB

Flutter Redux Entity State inspired from Angular Entity State https://ngrx.io/guide/entity

License: BSD 2-Clause "Simplified" License

Dart 100.00%

entity_state's Introduction

Entity State

Entity State to manage records in your redux state.

Installation

Add this to your project's pubspec.yaml

dependencies:
  entity_state: ^2.0.1

Usage

In your redux state

abstract class ReminderState
    with EntityState<Reminder, int, ReminderState, ReminderStateBuilder>
    implements Built<ReminderState, ReminderStateBuilder> {
  // This is mandatory to add
  BuiltList<int> get ids;

  // This is mandatory to add
  BuiltMap<int, Reminder> get entities;

  ReminderState._();

  factory ReminderState([updates(ReminderStateBuilder b)]) = _$ReminderState;

  static Serializer<ReminderState> get serializer => _$reminderStateSerializer;

  // This will tell the entity state what is the unique identifier of the model.
  @override
    int getId(Reminder data) {
      return data.id;
    }
}

In your reducer

To Add One

AppNotificationState listNotificationComplete(
    AppNotificationState appNotificationState,
    ListNotificationComplete action) {
  return appNotificationState
        // action.notification is `Notification`
      .addOne(action.notification)
      .rebuild((b) => b..isLoading = false);
}

To Add All (replaces any data that was previously present)

AppNotificationState listNotificationComplete(
    AppNotificationState appNotificationState,
    ListNotificationComplete action) {
  return appNotificationState
        // action.notifications is `List<Notification>`
      .addAll(action.notifications)
      .rebuild((b) => b..isLoading = false);
}

To Add Many (appends to previous data)

AppNotificationState listNotificationComplete(
    AppNotificationState appNotificationState,
    ListNotificationComplete action) {
  return appNotificationState
        // action.notifications is `List<Notification>`
      .addMany(action.notifications)
      .rebuild((b) => b..isLoading = false);
}

To Update One

AppNotificationState listNotificationComplete(
    AppNotificationState appNotificationState,
    ListNotificationComplete action) {
  return appNotificationState
        // action.notification is `Notification`
      .updateOne(action.notification)
      .rebuild((b) => b..isLoading = false);
}

To Update Many

AppNotificationState listNotificationComplete(
    AppNotificationState appNotificationState,
    ListNotificationComplete action) {
  return appNotificationState
        // action.notifications is `List<Notification>`
      .updateMany(action.notifications)
      .rebuild((b) => b..isLoading = false);
}

To Remove One

AppNotificationState listNotificationComplete(
    AppNotificationState appNotificationState,
    ListNotificationComplete action) {
  return appNotificationState
        // action.notification is `int`
      .removeOne(action.notificationId)
      .rebuild((b) => b..isLoading = false);
}

To Remove Many

AppNotificationState listNotificationComplete(
    AppNotificationState appNotificationState,
    ListNotificationComplete action) {
  return appNotificationState
        // action.notifications is `List<Notification>`
      .removeMany(action.notifications)
      .rebuild((b) => b..isLoading = false);
}

To Remove All

AppNotificationState listNotificationComplete(
    AppNotificationState appNotificationState,
    ListNotificationComplete action) {
  return appNotificationState
        // action.notifications is `List<Notification>`
      .removeAll()
      .rebuild((b) => b..isLoading = false);
}

How to get the data

static _ViewModel fromStore(Store<AppState> store) {
       return _ViewModel(
           notifications: store.state.appNotification.getAll(),
           isLoading: store.state.appNotification.isLoading,
           notificationTapped: (AppNotification notification) {
             store.dispatch(UpdateNotification(notification: notification));
           });
     }

How to get the sorted data

static _ViewModel fromStore(Store<AppState> store) {
       return _ViewModel(
           notifications: store.state.appNotification.getAll((notif1,notif2)=>notif1.id.compareTo(notif2.id),
           });
     }

entity_state's People

Contributors

agrawalharsh90 avatar elvisgn avatar kush3107 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.