sdk-react icon indicating copy to clipboard operation
sdk-react copied to clipboard

Pagination without safari component

Open AstaDK opened this issue 3 years ago • 25 comments

I using sajari for my demo app.

  1. I have 1 list and I want to show more items, I don't want to use the sajari component but I can't query after I change variables page.

  2. I using useSearchContext to get page and results, I cover my component with component search provider, code example bellow: <SearchProvider key="{index}" searchOnLoad={false} initialResponse="" search={{ pipeline: new Pipeline( { ...getConfigPipeline("") }, "query" ), variables: new Variables({ q: "", filter: "", resultsPerPage: 6, }), }} > <MyComponent/> </SearchProvider>

  3. Render result const { setQuery } = useQuery(); const { variables } = useVariables(); const { results, searching, totalResults, page, pageCount, } = useSearchContext();

  4. And then I use variables hook(useVariables) to set request for other page but It not request, code example bellow: const OtherComponent = ({ setQuery, textSearch, variables }) => { const handleShowMore = () => { variables.set({ page: page + 1, }); };

So how can set pagination without component your support?

  • [ ] I only try for used useSearch but not good for my case.

AstaDK avatar Mar 08 '21 13:03 AstaDK

Hi @AstaDK, have you tried the usePagination hook? - https://react.docs.sajari.com/hooks/usepagination

zlatanpham avatar Mar 08 '21 13:03 zlatanpham

Hi @AstaDK, have you tried the usePagination hook? - https://react.docs.sajari.com/hooks/usepagination

Tks for reply, but this example using component pagination, in my case I don't want to using it.

AstaDK avatar Mar 08 '21 14:03 AstaDK

FIY my version using "@sajari/react-components": "1.4.8", "@sajari/react-hooks": "1.4.1", "@sajari/react-search-ui": "1.7.4", "@sajari/server": "1.2.1",

AstaDK avatar Mar 08 '21 14:03 AstaDK

You don't have to use the Pagination component, the variables and methods from usePagination can be consumed by any piece of UI. For example:

const { page, setPage, resultsPerPage, pageCount, totalResults } = usePagination();

return (
   <div>
       <div>Current page: {page}</div>
       <div>
          <button onClick={() => {setPage(1)}}>Go to page 1</button>
          <button onClick={() => {setPage(2)}}>Go to page 2</button>          
          ....
       </div>
   </div>
)

Or maybe I misunderstood your question. Any further elaboration is very much appreciated.

zlatanpham avatar Mar 08 '21 14:03 zlatanpham

Cool, That all I need. Tks you countryman.

AstaDK avatar Mar 08 '21 14:03 AstaDK

Sr, I just find one point and want to ask you. After I setPage the request server called. But variable searching from useSearchContext not change, It mean the result do not update. The code: const handleShowMore = () => { setPage(page + 1) };

const { setPage } = usePagination(); const { results, searching, page, pageCount, } = [useSearchContext(); console.log("searching", searching);

The log not appear. The request server called but results not update @zlatanpham

AstaDK avatar Mar 08 '21 14:03 AstaDK

@AstaDK, I see your point. The searching actually doesn't change if you set a new page number. @tuanddd can you confirm if it could be a bug?

zlatanpham avatar Mar 08 '21 14:03 zlatanpham

@AstaDK, I see your point. The searching actually doesn't change if you set a new page number. @tuanddd can you confirm if it could be a bug?

Tks, I hope you can check this issue. Im using the Sajari and somethings have some bugs but if update new version the code maybe do not work with my desire.

AstaDK avatar Mar 08 '21 15:03 AstaDK

The log not appear. The request server called but results not update

@AstaDK although the searching doesn't change, the results should still be updated as I can check it works on my end.

Im using the Sajari and somethings have some bugs but if update new version the code maybe do not work with my desire.

May I ask why the new versions do not work for you?

zlatanpham avatar Mar 08 '21 15:03 zlatanpham

It is indeed not updating the searching variable, I'll create a PR for this.

tuanddd avatar Mar 08 '21 15:03 tuanddd

May I ask why the new versions do not work for you?

Im not sure but I was updated latest versions for all Sajari, but the filter option do not work fine, It have some issue and after I downgrade with the version present.

AstaDK avatar Mar 08 '21 15:03 AstaDK

Im not sure but I was updated latest versions for all Sajari, but the filter option do not work fine, It have some issue and after I downgrade with the version present.

Sorry, I'm not quite clear what you mean 😅

zlatanpham avatar Mar 08 '21 15:03 zlatanpham

No problem, Can you update for me the latest stable versions?

AstaDK avatar Mar 08 '21 15:03 AstaDK

You can find the latest version of the packages in README.md.

zlatanpham avatar Mar 08 '21 15:03 zlatanpham

You can find the latest version of the packages in README.md.

Tks, I will check with the latest version. If another issue, I will create a topic on this.

AstaDK avatar Mar 08 '21 15:03 AstaDK

https://github.com/sajari/sdk-react/pull/433/commits/edfd0194ed999d4775a1ac51f8e3d539a040f46b I check this commit, after I setPage the searching is true FOREVER? The log show true and not another change Screen Shot 2021-03-10 at 17 07 39

I think it change from true to false and response the results right? @tuanddd @zlatanpham

AstaDK avatar Mar 10 '21 10:03 AstaDK

@AstaDK thanks for reporting. We'll check and fix it if it's an issue.

zlatanpham avatar Mar 10 '21 15:03 zlatanpham

@AstaDK Can you give me a codesandbox of the mentioned issue? I can't seem to be able to reproduce it locally.

tuanddd avatar Mar 11 '21 03:03 tuanddd

The codesandbox do not fully shown my case. But I don't use set Paging and used setQuery, it is okay. I don't know what a reason. The code exp:

const { setQuery } = useQuery();
const { variables } = useVariables()

const handleShowMore = () => {
    // setPage(page + 1);
    variables.set({
      page: page + 1,
    });
    setQuery(textSearch);
  };

But How I can set page inside variables and then I use setQuery to query with the new config variables I set @tuanddd

AstaDK avatar Mar 12 '21 08:03 AstaDK

https://github.com/AstaDK/Next-Sajari/tree/pagination This is repo code I reproduced my case. I think reason loading only "true" due to the SearchProvider rebuild. But how can I update code better. @tuanddd @zlatanpham

AstaDK avatar Mar 13 '21 09:03 AstaDK

Hi @AstaDK, I'll checkout your code whenever I got the time, thank you.

tuanddd avatar Mar 15 '21 15:03 tuanddd

FYI: I used component pagination still the same issue. Screen Shot 2021-03-17 at 21 47 03

AstaDK avatar Mar 17 '21 14:03 AstaDK

Hi @AstaDK, your code seems to render multiple SearchProvider in your app, this is not recommended as noted in our documentation. Rendering only one SearchProvider now works. Feel free to ask any questions. Cheers.

import { FilterBuilder, SearchProvider, Variables } from "@sajari/react-hooks";
import { useState } from "react";
import { pipelineConfig } from "../../config";
import ItemComponent from "../items";

const variables = new Variables({
  resultsPerPage: 6,
  q: ``,
});

const pipeline = pipelineConfig();

const HomeComponent = () => {
  const [listViews, setListViews] = useState([
    {
      name: "Asta",
      isRender: true,
      text: "SanDisk",
    },
    {
      name: "Nick",
      isRender: true,
      text: "HP",
    },
    {
      name: "Deku",
      isRender: true,
      text: "Apple",
    },
  ]);

  const brandFilter = new FilterBuilder({
    name: "brand",
    options: listViews.reduce((acc, v) => {
      acc[v.text] = `brand = '${v.text}'`;
      return acc;
    }, {}),
  });

  return (
    <div className="space-y-6">
      <SearchProvider
        search={{
          variables,
          pipeline,
          filters: [brandFilter],
        }}
        searchOnLoad={true}
      >
        {listViews &&
          listViews
            .filter((i) => i.isRender)
            .map((item, index) => {
              return (
                <ItemComponent
                  listViews={listViews}
                  setListViews={setListViews}
                  item={item}
                />
              );
            })}
      </SearchProvider>
    </div>
  );
};
export default HomeComponent;

tuanddd avatar Mar 17 '21 16:03 tuanddd

I think it does work incorrectly, Image below shows that when I load more one part (load more data of Asta), other data will load more too. Screen Shot 2021-03-18 at 00 02 26

AstaDK avatar Mar 17 '21 17:03 AstaDK

I try to move the provider to the top level but I still have the same issue above.

AstaDK avatar Mar 17 '21 17:03 AstaDK