Redirect State update on background task
I am using breadcrumbs to navigate between my pages.
I would like to clear/reset the state when i redirect back to my home(projects page) or just leave the current page.
Currently i have the reset state implemented on my on_load function of the project page but it takes too long to fetch and clear all prev states:
@rx.background
async def fetch_data(self):
async with self:
self.eda_wf_status = ""
self.project_name = ""
self.fetching_list = True
if self.workspace_id == "":
return rx.redirect("/workspaces")
projects = await self.fetch_files()
datasources = await self.fetch_datasources()
for projects in projects:
self._projects.append(projects)
for ds in datasources:
if ds.type != "Geo Set":
self._datasources.append(ds)
self.fetching_list = False
self.fetching = False
# Here i am reseting all the prev states
print(" reset state")
del_eda = (await self.get_state(VariableState)).reset()
del_ms = (await self.get_state(SelectionState)).reset()
del_eval = (await self.get_state(EvalState)).reset()
def on_load(self):
return ProjectState.fetch_data
I would like to reset the specific previous page when i navigate with the breadcrumbs. So I implemented the code below:
@rx.background
async def reset_state(self, url):
# async with self:
# print("1",self.current_url)
# yield rx.redirect(url)
# print("2",self.current_url)
async with self:
print("3",self.current_url)
match url:
case "/projects/":
print(" in here")
(await self.get_state(VariableState)).reset()
print("done")
def back_to_page(self,url):
self.fetching = True
self.project_progress = 0
if url == "/projects/" or url == "/workspaces/":
self.project_name = ""
self.eda_tab_value= "tab1"
self.eval_selected_tag_id = ""
self._variables= []
yield rx.redirect(url)
return NavBarState.reset_state(url)
@rx.memo
def navbar():
"""The navbar."""
return rx.box(
rx.flex(
rx.grid(
rx.flex(
rx.match(
NavBarState.current_url,
(
"/projects/",
rc.breadcrumb(
rc.breadcrumb_item(
rc.breadcrumb_link(
"Workspaces",
href=""
),
on_click=NavBarState.back_to_page("/workspaces/"),
),
rc.breadcrumb_item(
rc.breadcrumb_link(
"Projects", href=""
),
),
),
),
(
"/eda/",
rc.breadcrumb(
rc.breadcrumb_item(
rc.breadcrumb_link(
"Workspaces",href=""
),
on_click=NavBarState.back_to_page("/workspaces/"),
),
rc.breadcrumb_item(
rc.breadcrumb_link( "Projects", href=""),
on_click=NavBarState.back_to_page("/projects/"),
),
rc.breadcrumb_item(
rc.breadcrumb_link(
"EDA",href="", is_current_page=True
)
),
),
),)
.....
What happens is the state resets which is what i want, but because the redirect has not yet updated the state (the current url is still the previous one) the previous page loads again (filling the state again) and then it redirects back to project page with the full state.
As you can see from the commented out code i tried a few different ways to update the state to the redirected url before reseting the state.
Any help would be appreciated