Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Configurable question tags #4694

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { TextFieldOptions } from "./TextFieldOptions";
import { CharacterLimitOptions } from "./CharacterLimitOptions";
import { useRefsContext } from "@formBuilder/[id]/edit/components/RefsContext";
import { FormElement } from "@lib/types";
import { QuestionTagOptions } from "./QuestionTagOptions";

export const MoreDialog = () => {
const { elements, updateField, setChangeKey, getFormElementById } = useTemplateStore((s) => ({
Expand Down Expand Up @@ -118,6 +119,8 @@ export const MoreDialog = () => {
<TextFieldOptions item={item} setItem={setItem} />

<CharacterLimitOptions item={item} setItem={setItem} />

<QuestionTagOptions item={item} setItem={setItem} />
</form>
</div>
</Dialog>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { useTranslation } from "@i18n/client";
import { InfoDetails } from "@formBuilder/components/shared/InfoDetails";
import { FormElement, FormElementTypes } from "@lib/types";
import { Label } from "./Label";
import { Hint } from "./Hint";
import { Input } from "@formBuilder/components/shared/Input";
import { v4 as uuid } from "uuid";

export const QuestionTagOptions = ({
item,
setItem,
}: {
item: FormElement;
setItem: (item: FormElement) => void;
}) => {
const { t } = useTranslation("form-builder");

if (item.type === FormElementTypes.richText) {
return null;
}

return (
<section className="mb-4 mt-8">
<Label htmlFor="">{t("Question tag")}</Label>
<Hint>
{t("Adding a question tag allows the question to contain a permanent identifier.")}
</Hint>
<Input
id={`title--modal--${item.id}`}
name={`item${item.id}`}
value={item.properties.tag || uuid()}
className="w-11/12"
onChange={(e) => {
setItem({
...item,
properties: {
...item.properties,
tag: e.target.value,
},
});
}}
/>
<div>
<InfoDetails summary={t("How tags can help")}>
<div className="mb-8 mt-4 border-l-3 border-gray-500 pl-8">
<p className="mb-4 text-sm">{t("text description")}</p>
</div>
</InfoDetails>
</div>
</section>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,24 @@ const sortByGroups = ({ form, elements }: { form: FormProperties; elements: Answ
return sortByLayout({ layout, elements });
};

// @TODO: moved to lib/responses
export type TaggedResponse = {
tag: string;
answer: string;
};

const extractAnswerFromTaggedResponse = (answer: unknown): string => {
try {
const answerObject = JSON.parse(answer as string) as TaggedResponse;
return answerObject.answer;
} catch (e) {
return answer as string;
}
};

const getAnswerAsString = (question: FormElement | undefined, answer: unknown): string => {
answer = extractAnswerFromTaggedResponse(answer);

if (question && question.type === "checkbox") {
return Array(answer).join(", ");
}
Expand All @@ -125,6 +142,7 @@ const getAnswerAsString = (question: FormElement | undefined, answer: unknown):
if (!answer) {
return "";
}

const dateFormat = (question.properties.dateFormat || "YYYY-MM-DD") as DateFormat;
const dateObject = JSON.parse(answer as string) as DateObject;

Expand Down Expand Up @@ -229,12 +247,12 @@ export const getSubmissionsByFormat = async ({
const allowGroupsFlag = allowGrouping();
// Get responses into a ResponseSubmission array containing questions and answers that can be easily transformed
const responses = queryResult.map((item) => {
// console.log({ item });
const submission = Object.entries(JSON.parse(String(item.formSubmission))).map(
([questionId, answer]) => {
const question = fullFormTemplate.form.elements.find(
(element) => element.id === Number(questionId)
);

// Handle Dynamic Rows
if (question?.type === FormElementTypes.dynamicRow && answer instanceof Array) {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export function buildFormDataObject(formRecord: PublicFormRecord, values: Respon
const result = _handleDynamicRowTypeIfNeeded(element, values[element.id]);
for (const tuple of result) {
formData[tuple[0]] = tuple[1];

// If there is a question tag, store it with the response
// if (element.properties.tag) {
// formData[tuple[0]] = JSON.stringify({
// answer: tuple[1],
// tag: element.properties.tag,
// });
// }
}
}

Expand Down
7 changes: 7 additions & 0 deletions lib/middleware/schemas/templates.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
"id": 1,
"type": "richText",
"properties": {
"tag": "91eac504-d781-4c64-85f2-3233895f907d1",
"validation": {
"required": false
},
Expand All @@ -139,6 +140,7 @@
"id": 2,
"type": "radio",
"properties": {
"tag": "f8c8245a-3fc0-42a4-8686-e036403734e4",
"choices": [
{
"en": "Team nomination",
Expand Down Expand Up @@ -229,6 +231,11 @@
"properties": {
"type": "object",
"properties": {
"tag": {
"description": "A configurable tag that can be used to identify the element",
"type": "string",
"minimum": 0
},
"titleEN": {
"description": "Input label in English",
"type": "string"
Expand Down
1 change: 1 addition & 0 deletions lib/types/form-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type dynamicRowType = {

// used to define attributes for the properties of an element in the form
export interface ElementProperties {
tag?: string;
titleEn: string;
titleFr: string;
placeholderEn?: string;
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/form-builder/itemHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FormElement, FormElementTypes, HTMLTextInputTypeAttribute } from "@lib/types";
import { Language, LocalizedElementProperties } from "../../types/form-builder-types";
import { isValidatedTextType, isAutoCompleteField } from "@lib/utils/form-builder";
import { v4 as uuid } from "uuid";

type ElementType =
| keyof typeof FormElementTypes
Expand Down Expand Up @@ -28,6 +29,7 @@ export const defaultField: FormElement = {
id: 0,
type: FormElementTypes.textField,
properties: {
tag: uuid(),
subElements: [],
choices: [{ en: "", fr: "" }],
titleEn: "",
Expand Down
Loading