Skip to content

Commit

Permalink
fix logic
Browse files Browse the repository at this point in the history
  • Loading branch information
liyiy committed Oct 11, 2023
1 parent c729606 commit 8c63bad
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion frontend/src/scenes/scenes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const sceneConfigurations: Partial<Record<Scene, SceneConfig>> = {
},
[Scene.SurveyTemplates]: {
projectBased: true,
name: 'Survey Templates',
name: 'New survey',
},
[Scene.DataWarehouse]: {
projectBased: true,
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/scenes/surveys/SurveyTemplates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
Expand All @@ -32,7 +32,9 @@ export function SurveyTemplates(): JSX.Element {
<div
className="flex flex-col items-center"
key={idx}
onClick={(template) => openSurveyTemplate(template)}
onClick={() =>
setSurveyTemplateValues({ name: template.type, questions: template.questions })
}
>
<span className="mb-2 text-md">
<b>{template.type}</b>
Expand Down
23 changes: 21 additions & 2 deletions frontend/src/scenes/surveys/surveyLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -96,6 +96,7 @@ export const surveyLogic = kea<surveyLogicType>([
archiveSurvey: true,
setCurrentQuestionIndexAndType: (idx: number, type: SurveyQuestionType) => ({ idx, type }),
setWritingHTMLDescription: (writingHTML: boolean) => ({ writingHTML }),
setSurveyTemplateValues: (template: any) => ({ template }),
}),
loaders(({ props, actions, values }) => ({
survey: {
Expand All @@ -110,7 +111,11 @@ export const surveyLogic = kea<surveyLogicType>([
throw error
}
}
return { ...NEW_SURVEY }
if (props.id === 'new' && router.values.hashParams.fromTemplate) {
return values.survey
} else {
return { ...NEW_SURVEY }
}
},
createSurvey: async (surveyPayload: Partial<Survey>) => {
return await api.surveys.create(sanitizeQuestions(surveyPayload))
Expand Down Expand Up @@ -293,6 +298,12 @@ export const surveyLogic = kea<surveyLogicType>([
},
}
},
setSurveyTemplateValues: (state, { template }) => {
return {
...state,
...template,
}
},
},
],
currentQuestionIndexAndType: [
Expand Down Expand Up @@ -541,6 +552,14 @@ export const surveyLogic = kea<surveyLogicType>([
}
},
})),
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()
Expand Down

0 comments on commit 8c63bad

Please sign in to comment.