-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create end-to-end form filling with SurveyJS
- Loading branch information
Showing
24 changed files
with
498 additions
and
260 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
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 was deleted.
Oops, something went wrong.
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,42 @@ | ||
import { v } from "convex/values"; | ||
import { userMutation, userQuery } from "./helpers"; | ||
|
||
export const get = userQuery({ | ||
args: {}, | ||
handler: async (ctx, _args) => { | ||
const userData = await ctx.db | ||
.query("userFormData") | ||
.withIndex("userId", (q) => q.eq("userId", ctx.userId)) | ||
.first(); | ||
|
||
return userData; | ||
}, | ||
}); | ||
|
||
export const set = userMutation({ | ||
args: { | ||
field: v.string(), | ||
value: v.any(), | ||
}, | ||
handler: async (ctx, args) => { | ||
// If data already exists, update it | ||
const existingData = await ctx.db | ||
.query("userFormData") | ||
.withIndex("userIdAndField", (q) => | ||
q.eq("userId", ctx.userId).eq("field", args.field), | ||
) | ||
.first(); | ||
|
||
if (existingData) { | ||
await ctx.db.patch(existingData._id, { value: args.value }); | ||
return; | ||
} | ||
|
||
// Otherwise, insert new data | ||
await ctx.db.insert("userFormData", { | ||
userId: ctx.userId, | ||
field: args.field, | ||
value: args.value, | ||
}); | ||
}, | ||
}); |
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,61 @@ | ||
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"; | ||
|
||
export type QuestFormProps = { | ||
quest: Doc<"quests">; | ||
}; | ||
|
||
export const QuestForm = ({ quest }: QuestFormProps) => { | ||
const { resolvedTheme } = useTheme(); | ||
const [isSurveyOpen, setIsSurveyOpen] = useState(false); | ||
const saveData = useMutation(api.userFormData.set); | ||
|
||
if (!quest.formSchema) return null; | ||
|
||
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, | ||
}); | ||
} | ||
} | ||
setIsSurveyOpen(false); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}; | ||
|
||
survey.onComplete.add(handleSubmit); | ||
|
||
return ( | ||
<> | ||
<Button onPress={() => setIsSurveyOpen(true)}>Get Started</Button> | ||
<Modal isOpen={isSurveyOpen}> | ||
<Survey model={survey} /> | ||
</Modal> | ||
</> | ||
); | ||
}; |
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 @@ | ||
export * from "./QuestForm"; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.