From 8c63bad9c4885fe719585c62e6474e644101f159 Mon Sep 17 00:00:00 2001 From: Li Yi Yu Date: Wed, 11 Oct 2023 19:52:26 -0400 Subject: [PATCH] fix logic --- frontend/src/scenes/scenes.ts | 2 +- .../src/scenes/surveys/SurveyTemplates.tsx | 8 ++++--- frontend/src/scenes/surveys/surveyLogic.tsx | 23 +++++++++++++++++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/frontend/src/scenes/scenes.ts b/frontend/src/scenes/scenes.ts index 9a0d5c54f3268..42fea53780d15 100644 --- a/frontend/src/scenes/scenes.ts +++ b/frontend/src/scenes/scenes.ts @@ -164,7 +164,7 @@ export const sceneConfigurations: Partial> = { }, [Scene.SurveyTemplates]: { projectBased: true, - name: 'Survey Templates', + name: 'New survey', }, [Scene.DataWarehouse]: { projectBased: true, diff --git a/frontend/src/scenes/surveys/SurveyTemplates.tsx b/frontend/src/scenes/surveys/SurveyTemplates.tsx index e339494e5b0ac..fc1a63d867a66 100644 --- a/frontend/src/scenes/surveys/SurveyTemplates.tsx +++ b/frontend/src/scenes/surveys/SurveyTemplates.tsx @@ -4,17 +4,17 @@ import { defaultSurveyTemplates } from './constants' import { SurveyQuestion } from '~/types' import './SurveyTemplates.scss' import { useActions } from 'kea' -import { surveyTemplatesLogic } from './surveyTemplatesLogic' import { PageHeader } from 'lib/components/PageHeader' import { LemonButton } from '@posthog/lemon-ui' import { urls } from 'scenes/urls' +import { surveyLogic } from './surveyLogic' export const scene: SceneExport = { component: SurveyTemplates, } export function SurveyTemplates(): JSX.Element { - const { openSurveyTemplate } = useActions(surveyTemplatesLogic) + const { setSurveyTemplateValues } = useActions(surveyLogic({ id: 'new' })) return ( <> @@ -32,7 +32,9 @@ export function SurveyTemplates(): JSX.Element {
openSurveyTemplate(template)} + onClick={() => + setSurveyTemplateValues({ name: template.type, questions: template.questions }) + } > {template.type} diff --git a/frontend/src/scenes/surveys/surveyLogic.tsx b/frontend/src/scenes/surveys/surveyLogic.tsx index fc03107471aa6..c5047f86f7ec8 100644 --- a/frontend/src/scenes/surveys/surveyLogic.tsx +++ b/frontend/src/scenes/surveys/surveyLogic.tsx @@ -2,7 +2,7 @@ import { lemonToast } from '@posthog/lemon-ui' import { kea, path, props, key, listeners, afterMount, reducers, actions, selectors, connect } from 'kea' import { forms } from 'kea-forms' import { loaders } from 'kea-loaders' -import { router, urlToAction } from 'kea-router' +import { actionToUrl, router, urlToAction } from 'kea-router' import api from 'lib/api' import { urls } from 'scenes/urls' import { @@ -96,6 +96,7 @@ export const surveyLogic = kea([ archiveSurvey: true, setCurrentQuestionIndexAndType: (idx: number, type: SurveyQuestionType) => ({ idx, type }), setWritingHTMLDescription: (writingHTML: boolean) => ({ writingHTML }), + setSurveyTemplateValues: (template: any) => ({ template }), }), loaders(({ props, actions, values }) => ({ survey: { @@ -110,7 +111,11 @@ export const surveyLogic = kea([ throw error } } - return { ...NEW_SURVEY } + if (props.id === 'new' && router.values.hashParams.fromTemplate) { + return values.survey + } else { + return { ...NEW_SURVEY } + } }, createSurvey: async (surveyPayload: Partial) => { return await api.surveys.create(sanitizeQuestions(surveyPayload)) @@ -293,6 +298,12 @@ export const surveyLogic = kea([ }, } }, + setSurveyTemplateValues: (state, { template }) => { + return { + ...state, + ...template, + } + }, }, ], currentQuestionIndexAndType: [ @@ -541,6 +552,14 @@ export const surveyLogic = kea([ } }, })), + actionToUrl(({ values }) => ({ + setSurveyTemplateValues: () => { + const hashParams = router.values.hashParams + hashParams['fromTemplate'] = true + + return [urls.survey(values.survey.id), router.values.searchParams, hashParams] + }, + })), afterMount(async ({ props, actions }) => { if (props.id !== 'new') { await actions.loadSurvey()