-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Locate form on its own route, improve edit experience
- Loading branch information
Showing
6 changed files
with
152 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,60 @@ | ||
import "survey-core/defaultV2.min.css"; | ||
|
||
import { Button, Modal } from "@/components/common"; | ||
import { api } from "@convex/_generated/api"; | ||
import type { Doc } from "@convex/_generated/dataModel"; | ||
import { useMutation } from "convex/react"; | ||
import { useTheme } from "next-themes"; | ||
import { useState } from "react"; | ||
import type { CompleteEvent, SurveyModel } from "survey-core"; | ||
import { | ||
LayeredDarkPanelless, | ||
LayeredLightPanelless, | ||
} from "survey-core/themes"; | ||
import { Model, Survey } from "survey-react-ui"; | ||
import { Link } from "@/components/common"; | ||
import type { Doc, Id } from "@convex/_generated/dataModel"; | ||
import { Pencil, Plus } from "lucide-react"; | ||
import { Heading } from "react-aria-components"; | ||
|
||
export type QuestFormProps = { | ||
quest: Doc<"quests">; | ||
editable?: boolean; | ||
}; | ||
|
||
export const QuestForm = ({ quest }: QuestFormProps) => { | ||
const { resolvedTheme } = useTheme(); | ||
const [isSurveyOpen, setIsSurveyOpen] = useState(false); | ||
const saveData = useMutation(api.userFormData.set); | ||
export const EditButton = ({ | ||
isNew, | ||
questId, | ||
}: { isNew: boolean; questId: Id<"quests"> }) => { | ||
const { buttonText, Icon } = isNew | ||
? { buttonText: "Create form", Icon: Plus } | ||
: { buttonText: "Edit form", Icon: Pencil }; | ||
|
||
if (!quest.formSchema) return null; | ||
|
||
const survey = new Model(quest.formSchema); | ||
survey.applyTheme( | ||
resolvedTheme === "dark" ? LayeredDarkPanelless : LayeredLightPanelless, | ||
return ( | ||
<Link | ||
href={{ | ||
to: "/admin/quests/$questId/form", | ||
params: { questId }, | ||
}} | ||
button={{ variant: "secondary" }} | ||
> | ||
<Icon size={16} /> {buttonText} | ||
</Link> | ||
); | ||
}; | ||
|
||
const handleSubmit = async (sender: SurveyModel, _options: CompleteEvent) => { | ||
try { | ||
// Validate JSON against schema and remove invalid values | ||
sender.clearIncorrectValues(true); | ||
for (const key in sender.data) { | ||
const question = sender.getQuestionByName(key); | ||
if (question) { | ||
saveData({ | ||
field: key, | ||
value: question.value, | ||
}); | ||
} | ||
} | ||
setIsSurveyOpen(false); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}; | ||
|
||
survey.onComplete.add(handleSubmit); | ||
export const QuestForm = ({ quest, editable }: QuestFormProps) => { | ||
if (!quest.formSchema && !editable) return null; | ||
|
||
return ( | ||
<> | ||
<Button onPress={() => setIsSurveyOpen(true)}>Get Started</Button> | ||
<Modal isOpen={isSurveyOpen}> | ||
<Survey model={survey} /> | ||
</Modal> | ||
</> | ||
<div className="rounded-lg border border-gray-dim flex items-center justify-between gap-4 p-4 mb-4"> | ||
<div className="flex flex-col"> | ||
<Heading className="text-lg">Answer questions</Heading> | ||
<p className="text-gray-dim"> | ||
We'll walk you through the steps to fill out your forms. | ||
</p> | ||
</div> | ||
{editable ? ( | ||
<EditButton isNew={!quest.formSchema} questId={quest._id} /> | ||
) : ( | ||
<Link | ||
href={{ | ||
to: "/quests/$questId/form", | ||
params: { questId: quest._id }, | ||
}} | ||
button={{ variant: "secondary" }} | ||
> | ||
Get Started | ||
</Link> | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { api } from "@convex/_generated/api"; | ||
import type { Id } from "@convex/_generated/dataModel"; | ||
import { createFileRoute, useNavigate } from "@tanstack/react-router"; | ||
import { useMutation, useQuery } from "convex/react"; | ||
import { useTheme } from "next-themes"; | ||
import type { CompleteEvent, SurveyModel } from "survey-core"; | ||
import { | ||
LayeredDarkPanelless, | ||
LayeredLightPanelless, | ||
} from "survey-core/themes"; | ||
import { Model, Survey } from "survey-react-ui"; | ||
|
||
export const Route = createFileRoute( | ||
"/_authenticated/_home_/quests/$questId/form", | ||
)({ | ||
component: QuestFormRoute, | ||
}); | ||
|
||
function QuestFormRoute() { | ||
const navigate = useNavigate(); | ||
const { questId } = Route.useParams(); | ||
const { resolvedTheme } = useTheme(); | ||
const quest = useQuery(api.quests.getById, { | ||
questId: questId as Id<"quests">, | ||
}); | ||
|
||
const saveData = useMutation(api.userFormData.set); | ||
|
||
const survey = new Model(quest?.formSchema); | ||
survey.applyTheme( | ||
resolvedTheme === "dark" ? LayeredDarkPanelless : LayeredLightPanelless, | ||
); | ||
|
||
const handleSubmit = async (sender: SurveyModel, _options: CompleteEvent) => { | ||
try { | ||
// Validate JSON against schema and remove invalid values | ||
sender.clearIncorrectValues(true); | ||
for (const key in sender.data) { | ||
const question = sender.getQuestionByName(key); | ||
if (question) { | ||
saveData({ | ||
field: key, | ||
value: question.value, | ||
}); | ||
} | ||
} | ||
|
||
navigate({ to: "/quests/$questId", params: { questId } }); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}; | ||
|
||
survey.onComplete.add(handleSubmit); | ||
|
||
return <Survey model={survey} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters