lundi 19 août 2019

How to send a refresh signal to a ServerSide grid

I have a wrapper component for an AgGrid which is using the Serverside model, and i have a parent component which will use this component.
The current design is in such a way that the user has access to a Add new record button which shows a modal and the user can fill the necessary fields, after that he click on submit, the submit send a post to the server and the server updates its table, now at that moment i want to force a refresh inside the grid component so the grid shows the latest changes, i have a function for this inside the grid component but i don't know how to send the signal to activate it, currently i keep an ForceRefresh in the parent state and i turn it true the moment the Submit is successful, but that only works once, because after it turns true, it will remain true, of course i can try to set it to false again maybe by using a call back from the grid that the refresh has occurred ?, but that would seem too messy and too long of a solution, surely there must be a better way

Here are some code of what i just explained:

class grid extends Component {

    state = {
        forceRefresh: false,
    }

    componentDidUpdate (prevProps) {
        if (this.props.forceRefresh !== prevProps.forceRefresh)            
            this.setState({forceRefresh: this.props.forceRefresh})                      
    }

    refreshGrid = () => {
        this.gridApi.purgeServerSideCache([]);
    }

    render () {
        //some other sutff...
        if (this.state.forceRefresh) {
            this.purgeCache();
            this.setState({ forceRefresh: false })
        }

        return (
            //some other sutff...
        )
    }
}


class parentComponent extends Component {
    state = {
        forceRefresh: false
    }

    onSubmitFinished = () => {
        //some other sutff...
        this.setState({forceRefresh: true})
    }

    render () {
        //some other sutff...

        return (
            <Grid forceRefresh={this.state.forceRefresh} />
        )
    }
}

The current approch is obviosuly wrong and doesn't work (only works one time), how can i make this so that the parent can call force refresh multiple times and each time the refreshFunction inside the grid is called,

By the way i said i'm using AgGrid, because i thought maybe there is an even better way, maybe a method inside AgGrid to call refresh automatically when the server table is updated ?.

In any case i really appreciate any help or tip.

Aucun commentaire:

Enregistrer un commentaire