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

Filtros nao persistem ao retornar de um abrigo especifico #291 #297

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

Tiago-Silva
Copy link
Contributor

Para que os filtros fossem persistido eu utilizei o localStorage. Veja:

useEffect(() => {
    const storedSearchParams = localStorage.getItem('searchParams');
    if (storedSearchParams) {
      setSearchParams(new URLSearchParams(storedSearchParams));
    }
  }, []);

Nesse trecho toda vez que a página é carregada eu verifico se já existe algo salvo no localStorage com determinada chave, se houver então eu seto o setSearchParams com o valor guardado.

const onSubmitFilterForm = useCallback(
    (values: IFilterFormProps) => {
      setOpenModal(false);
      setFilterData(values);
      const searchQuery = qs.stringify(values, {
        skipNulls: true,
      });
      setSearchParams(searchQuery);
      localStorage.setItem('searchParams', JSON.stringify(searchQuery));
      refresh({
        params: {
          search: searchQuery,
        },
      });
    },
    [refresh, setSearchParams]
  );

Quando o filtro é aplicado essa função é chamada, nela eu guardo os parametros no localStorage dessa maneira:

localStorage.setItem('searchParams', JSON.stringify(searchQuery));

E por último quando o botão 'Limpar filtros' é executado eu limpo o valor salvo no localStorage:

const clearSearch = useCallback(() => {
    setSearch('');
    setFilterData(initialFilterData);
    setSearchParams('');
    refresh();
    localStorage.removeItem('searchParams'); //aqui eu limpo, esse comentário não está no commit
  }, [refresh, setSearch, setSearchParams]);

  const hasMore = useMemo(
    () => shelters.page * shelters.perPage < shelters.count,
    [shelters.page, shelters.perPage, shelters.count]
  );

@Tiago-Silva Tiago-Silva changed the title Filtros nao persistem ao retornar de um abrigo especifico 291 Filtros nao persistem ao retornar de um abrigo especifico #291 May 23, 2024
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

Successfully merging this pull request may close these issues.

1 participant