From d27d909bccceece25e66491cc717320c7a1ecbc2 Mon Sep 17 00:00:00 2001 From: Li Yi Yu Date: Wed, 18 Oct 2023 22:06:17 -0400 Subject: [PATCH] add auto opt ins for surveys list launch and stop surveys --- frontend/src/scenes/surveys/Surveys.tsx | 24 +++++++++++++++++---- frontend/src/scenes/surveys/surveyLogic.tsx | 12 +++++------ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/frontend/src/scenes/surveys/Surveys.tsx b/frontend/src/scenes/surveys/Surveys.tsx index 1252eef3172b8..08479be6eac44 100644 --- a/frontend/src/scenes/surveys/Surveys.tsx +++ b/frontend/src/scenes/surveys/Surveys.tsx @@ -60,6 +60,7 @@ export function Surveys(): JSX.Element { const { user } = useValues(userLogic) const { currentTeam } = useValues(teamLogic) + const { updateCurrentTeam } = useActions(teamLogic) const surveysPopupDisabled = currentTeam && !currentTeam?.surveys_opt_in && surveys.some((s) => s.start_date && !s.end_date) @@ -301,14 +302,26 @@ export function Surveys(): JSX.Element { + onClick={() => { updateSurvey({ id: survey.id, updatePayload: { end_date: dayjs().toISOString(), }, }) - } + if (currentTeam && currentTeam.surveys_opt_in) { + const activeSurveys = surveys.filter( + (s) => s.start_date && !s.end_date + ) + if ( + activeSurveys.filter( + (s) => s.id !== survey.id + ).length === 0 + ) { + updateCurrentTeam({ surveys_opt_in: false }) + } + } + }} > Stop survey @@ -317,12 +330,15 @@ export function Surveys(): JSX.Element { + onClick={() => { + if (currentTeam && !currentTeam.surveys_opt_in) { + updateCurrentTeam({ surveys_opt_in: true }) + } updateSurvey({ id: survey.id, updatePayload: { end_date: null }, }) - } + }} > Resume survey diff --git a/frontend/src/scenes/surveys/surveyLogic.tsx b/frontend/src/scenes/surveys/surveyLogic.tsx index a9476839eb672..6ce751931f2cd 100644 --- a/frontend/src/scenes/surveys/surveyLogic.tsx +++ b/frontend/src/scenes/surveys/surveyLogic.tsx @@ -405,9 +405,6 @@ export const surveyLogic = kea([ lemonToast.success(<>Survey {survey.name} created) actions.loadSurveys() router.actions.replace(urls.survey(survey.id)) - if (values.currentTeam?.surveys_opt_in === false) { - actions.setSurveysOptIn(true) - } actions.reportSurveyCreated(survey) }, updateSurveySuccess: ({ survey }) => { @@ -419,13 +416,16 @@ export const surveyLogic = kea([ launchSurveySuccess: ({ survey }) => { lemonToast.success(<>Survey {survey.name} launched) actions.loadSurveys() + if (!values.currentTeam?.surveys_opt_in) { + actions.setSurveysOptIn(true) + } actions.reportSurveyLaunched(survey) }, stopSurveySuccess: ({ survey }) => { actions.loadSurveys() - if (values.currentTeam?.surveys_opt_in === true) { - const hasActiveSurveys = values.surveys.some((s) => s.start_date && !s.end_date) - if (!hasActiveSurveys) { + if (values.currentTeam?.surveys_opt_in) { + const activeSurveys = values.surveys.filter((s) => s.start_date && !s.end_date) + if (activeSurveys.filter((s) => s.id !== survey.id).length === 0) { actions.setSurveysOptIn(false) } }