Giter Club home page Giter Club logo

autoaction's People

Contributors

allysmith424 avatar tonyhb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

autoaction's Issues

scope for autoaction

What should AutoAction really do?

It should:

  • Automatically call actions if all arguments can be derived from state
  • Automatically call actions if all arguments can be derived from props
  • Be nested: one component might call the same action as an inner compnent and the action should fire once.

It should not:

  • Worry about the status of loading data. This is dependent on how you fetch (falcor, promises etc.)

Autoactions with args as a function of props & state fail to work when autoaction is defined as an object

When defining a decorator in the form:

const actionCreators = {
     load: loadSpy
};

const autoActions = {
            load: {
                args: (state, props) => "xyz",
                key: (state, props) => "xyz"
            }
 };
const decorator = autoaction(autoActions, actionCreators);
    It fails to work(throwwing an exception) properly because of the fact the check for args being a function is incorrectly made within the autoaction lib. A folllow up PR fixing this will follow shortly.

Batching requests from props

PTAL at how to do this.

If any two components in a tree call an action using props as an argument, and those props were passed down from an initial action call, we should batch and dedupe these actions.

Provide a runnable example (and is this project still alive?)

autoaction sounds very useful, however I think I'm misunderstanding how to use it.

I tried to create an example where I have a Dashboard which has many Cards, some of which might be duplicated, and I only want to fetch each Card once.

It works, but the card requests aren't deduplicated. The logs for I get for dashboard 100 which has cards 101, 102, 102, 103 are:

GET_DASHBOARD 100
GET_CARD 101
GET_CARD 102
GET_CARD 102 <-- duplicate
GET_CARD 103

Also, I (perhaps naively) assumed if a prop changes that results in an action argument changing then it will fire the action again with the new arguments, but that doesn't appear to be the case.

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import { Provider, connect } from 'react-redux'
import { createStore } from 'redux'
import autoaction from 'autoaction';
import { createStructuredSelector } from 'reselect';

const actions = {
    getDashboardById(id) {
        console.log("GET_DASHBOARD", id)
        return {
            type: "GET_DASHBOARD",
            payload: {
                id: id,
                name: "dashboard"+id,
                cards: [id + 1, id + 2, id + 2, id + 3]
            }
        };
    },
    getCardById(id) {
        console.log("GET_CARD", id)
        return {
            type: "GET_CARD",
            payload: {
                id: id,
                name: "card"+id
            }
        };
    }
}


function reducer(state = { cards: {}, dashboards: {} }, { type, payload }) {
    switch (type) {
        case "GET_DASHBOARD":
            return { ...state, dashboards: { ...state.dashboards, [payload.id]: payload }};
        case "GET_CARD":
            return { ...state, cards: { ...state.cards, [payload.id]: payload }};
        default:
            return state;
    }
}

@connect(createStructuredSelector({
    dashboard: (state, props) => state.dashboards[props.dashboardId]
}))
@autoaction({
    getDashboardById: (props, state) => props.dashboardId
}, actions)
class Dashboard extends Component {
    render() {
        const { dashboard } = this.props;
        return (
            <div>
                <h1>dashboard={dashboard && dashboard.name}</h1>
                <ul>
                    {dashboard && dashboard.cards.map(id =>
                        <Card cardId={id} />
                    )}
                </ul>
            </div>
        );
    }
}

@connect(createStructuredSelector({
    card: (state, props) => state.cards[props.cardId]
}))
@autoaction({
    getCardById: (props, state) => props.cardId
}, actions)
class Card extends Component {
    render() {
        const { card } = this.props;
        return (
            <li>card={card && card.name}</li>
        );
    }
}

class App extends Component {
    constructor(props, context) {
        super(props, context);
        this.state = {
            dashboardId: "100"
        }
    }
    render() {
        const { dashboardId } = this.state;
        return (
            <div className="App">
                <input value={dashboardId} onChange={(e) => this.setState({ dashboardId: e.target.value })} />
                <Dashboard dashboardId={parseInt(dashboardId)} />
            </div>
        );
    }
}

ReactDOM.render(
    <Provider store={createStore(reducer)}>
        <App />
    </Provider>,
    document.getElementById('root')
);

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.