Full Page Reload Using callbackAfterUpdate (v.3.0.7)

Hi @johnny ,

I was following the documentation:
[Full Page Redirection | Grocery CRUD](https://Full Page Reload)

I was trying to use callbackAfterUpdate and in my code was:

            $crud->callbackAfterUpdate(function ($stateParameters) {
                $redirectResponse = new \GroceryCrud\Core\Redirect\RedirectResponse();
                return $redirectResponse->setUrl(base_url().'/profile#step-2/');
            });

However, instead of reloading the page, an error occurs:

Is there something wrong with my code? Can you guide me on this one?
Thank you in advance :blush:

Hello @larasmith ,

This seems missed at the version 3. I will try to include it at the next release and let you know :slight_smile:.

Regards
Johnny

1 Like

Hi @johnny

Thanks! I am looking forward to this… :blush: :blush: :blush:
I’ll go program other parts of the project while I wait for this.

Hello @larasmith ,

I am glad to announce you that I’ve just released Grocery CRUD Enterprise version 3.0.8 :slight_smile:
which is fixing the redirection issue.

Please let me know if that worked for you.

Regards
Johnny

1 Like

Hi @johnny

I can’t wait to try this out on Monday.
I will definitely provide feedback about it :blush:

Thank you for the overwhelming support. :blush::blush::blush:

Hi @johnny

I was able to upgrade to 3.0.8.
The code itself can reload the page but cannot do it if I have other code that needs the stateparameters returned… Here’s the code that I am currently working on:

            $crud->callbackAfterUpdate(function ($stateParameters){

                $submitStatus = $this->_getDataSubmitStatus();
                $this->_step3($submitStatus);
                $this->_updateRelatedTable();

                $redirectResponse = new \GroceryCrud\Core\Redirect\RedirectResponse();

                return [$stateParameters, $redirectResponse->setUrl(base_url().'/profile#step-2')];              

            });

The code above was able to do the first 3 lines but fails to reload the page after… Kindly guide me.
Thank you very much. :blush:

Hello @larasmith ,

Since we have a full page redirection we can only return the redirect as a return parameter. So please check your code if it will work if you change it to:

       $crud->callbackAfterUpdate(function ($stateParameters){

                $submitStatus = $this->_getDataSubmitStatus();
                $this->_step3($submitStatus);
                $this->_updateRelatedTable();

                $redirectResponse = new \GroceryCrud\Core\Redirect\RedirectResponse();

                return $redirectResponse->setUrl(base_url().'/profile#step-2');              

            });

instead

Hi @johnny,

Thank you! I highly appreciate the recommendation. :blush: :blush: :blush:

The odd thing about using the code above is sometimes it works sometimes it just hangs, kindly refer to the image below:


No error when checking the inspect element though…

What seems to be the problem?

Hello @larasmith ,

Can you please give more information? It should be something with the response I guess. Can you press right click “Inspect” and then go to “Console” tab to see if you have any JavaScript errors? If not then check the response of your request. Press the “Network” tab and check the last request. My blind guess is that those 3 lines:

                $submitStatus = $this->_getDataSubmitStatus();
                $this->_step3($submitStatus);
                $this->_updateRelatedTable();

sometimes are throwing an error. A more robust way to have the code is this one:

$crud->callbackAfterUpdate(function ($stateParameters){
    try {
        $submitStatus = $this->_getDataSubmitStatus();
        $this->_step3($submitStatus);
        $this->_updateRelatedTable();
    } catch (Exception $e) {
        $errorMessage = new \GroceryCrud\Core\Error\ErrorMessage();
        return $errorMessage->setMessage($e->getMessage());
    }


    $redirectResponse = new \GroceryCrud\Core\Redirect\RedirectResponse();

    return $redirectResponse->setUrl(base_url().'/profile#step-2');              

});

Hi @johnny,

Thank you for the response and suggestion :blush:
I highly appreciate it :blush::blush::blush:

Here’ the screenshot of the Console:

And here’s the screenshot of the Network:

By the way, the 3 lines actually are working fine when tested on their own.

Here’s the observation that I gathered:

The reload works on the first attempt but will fail if I edit the record again and save. Through observation, I found out that once the link being passed our reload to is the same link in the address bar it would trigger just the loading but will not proceed. Refer to the image:

My current workaround is to randomly pick a link the reroutes to the same page:

            $crud->callbackAfterUpdate(function ($stateParameters){

                $submitStatus = $this->_getDataSubmitStatus();
                $this->_step3($submitStatus);
                $this->_updateRelatedTable();

                /* LINK RANDOMIZER BEGIN: LARA'S WORK-AROUND IS TO RANDOMLY PICK THE URL TO LOAD FOR STEP 2  */
                    $links = [
                        base_url().'profile#step2',
                        PATM_BASEURL.'profile#step-2',
                        base_url().'../profile#step2',
                    ];


                    // Retrieve previously selected links from session or initialize an empty array
                    $selectedLinks = $_SESSION['selected_links'] ?? [];

                    // If all links have been selected, reset the selection
                    if (count($selectedLinks) === count($links)) {
                        $selectedLinks = [];
                    }

                    // Filter out previously selected links from the available options
                    $availableLinks = array_diff($links, $selectedLinks);

                    // Randomly select a link from the available options
                    $randomLink = $availableLinks[array_rand($availableLinks)];

                    // Store the selected link in the session
                    $selectedLinks[] = $randomLink;
                    $_SESSION['selected_links'] = $selectedLinks;

                /* LINK RANDOMIZER END */

                $redirectResponse = new \GroceryCrud\Core\Redirect\RedirectResponse();
                return $redirectResponse->setUrl($randomLink);

            });