Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading State from store.get #213

Open
omeiirr opened this issue Jun 2, 2021 · 5 comments
Open

Loading State from store.get #213

omeiirr opened this issue Jun 2, 2021 · 5 comments

Comments

@omeiirr
Copy link

omeiirr commented Jun 2, 2021

Context:
I have a grid of images whose order can be changed by the user. I want to save the modified order and have it accessible so that it persists on a page refresh or when the user revisits the form.
Ideally, the order should be communicated to the server and loaded from the server as well, but currently the 'get' method is not working even with the localStorage, as described in the store documentation.

image

Problem:
I tried the get and set methods from 'store', but only the set seems to work.
Whenever I reorder the images, the localStorage gets updated accordingly, but the images go to their default positions on page refresh.

Key-value pair in localStorage:
example : 60b4f506a8ee53a1d8aad392|60b4f506a8ee53a1d8aad394|60b4f506a8ee53a1d8aad393

I tried to use

    sortable.sort(order)

but I'm unsure where to call this.

How can I ensure that the images are positioned according to the positions last set by the user when the page loads?

Any help in this matter is highly appreciated. Thank you for your time.

      <ReactSortable
        list={samples}
        setList={setSamples}
        className="list"
        onEnd={handleEnd}
        delayOnTouchStart={true}
        // delay={50}
        animation={500}
        ghostClass="ghost"

        group="example"

        store={{
          get: function (sortable) {
            console.log(sortable);        // even this is not being logged
            var order = localStorage.getItem(sortable.options.group.name);
            return order ? order.split("|") : [];
          },

          set: function (sortable) {
            var order = sortable.toArray();
            console.log(order);
            localStorage.setItem(sortable.options.group.name, order.join("|"));
          },

        }}
      >
@2brownc
Copy link

2brownc commented May 28, 2023

Did you figure it out?

@omeiirr
Copy link
Author

omeiirr commented May 28, 2023

I don't exactly remember since it's been so long, but I think the solution involved using useEffect to sort the images on page load.

Let me know if it works.

@2brownc
Copy link

2brownc commented May 29, 2023

Thanks for the reply.

The state variable contains the current order of the list and that is what I ended up using.

  const [state, setState] = useState<ItemType[]>([
    { id: 1, name: "shrek" },
    { id: 2, name: "fiona" },
  ]);

@omeiirr
Copy link
Author

omeiirr commented May 29, 2023

So it's working as expected now?

@2brownc
Copy link

2brownc commented May 29, 2023

I don't think it's the intended way. But I found a way I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants