Spaces:
Sleeping
Sleeping
import streamlit as st | |
from db.crud import read, reset_feedback_in_db | |
from views.questions_screen import questions_screen | |
def continue_survey_screen(data): | |
"""Screen for existing users to continue or restart their survey.""" | |
st.title("Resume Your Survey") | |
st.markdown( | |
f""" | |
<div style="padding:1rem;"> | |
<h3 style="text-align:left;"> | |
Welcome back | |
{st.session_state.username.capitalize()}! | |
</h3> | |
<p style="text-align:left;"> | |
Would you like to continue from where you left off | |
or restart the survey? | |
<br><b>Note that restarting will delete your previous progress.</b> | |
</p> | |
</div> | |
""", | |
unsafe_allow_html=True, | |
) | |
# Fetch user progress from Firebase | |
saved_state = read(st.session_state.username) | |
col1, col2 = st.columns(2) | |
with col1: | |
if st.button("Continue"): | |
if saved_state: | |
# Set session state values based on saved progress | |
st.session_state.current_index = saved_state.get("current_index", 0) | |
st.session_state.responses = saved_state.get("responses", []) | |
# Find the last answered config_id in responses | |
last_config_id = None | |
if st.session_state.responses: | |
last_config_id = st.session_state.responses[-1].get("config_id") | |
# Set the index to the matching config_id | |
if last_config_id is not None: | |
matching_index = data[data["config_id"] == last_config_id].index | |
if not matching_index.empty: | |
st.session_state.current_index = matching_index[0] | |
st.success("Resuming your survey!") | |
# Set survey_continued flag to True only when there's saved progress | |
st.session_state.survey_continued = True | |
st.rerun() | |
else: | |
st.warning("No previous progress found. Starting a new survey.") | |
st.session_state.current_index = 0 | |
st.session_state.responses = [] | |
with col2: | |
if st.button("Restart"): | |
st.session_state.current_index = 0 | |
st.session_state.responses = [] | |
st.success("Survey restarted!") | |
# Reset Firebase data for the user | |
reset_feedback_in_db(st.session_state.username) | |
st.rerun() | |