-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from delta10/feat/5-additional-questions
Add ability to show additional questions
- Loading branch information
Showing
21 changed files
with
1,684 additions
and
97 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
7 changes: 7 additions & 0 deletions
7
src/app/[locale]/incident/add/components/questions/AssetSelect.tsx
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,7 @@ | ||
import { QuestionField } from '@/types/form' | ||
|
||
interface AssetSelectProps extends Omit<QuestionField, 'register' | 'errors'> {} | ||
|
||
export const AssetSelect = ({ field }: AssetSelectProps) => { | ||
return <p>{field.key}</p> | ||
} |
74 changes: 74 additions & 0 deletions
74
src/app/[locale]/incident/add/components/questions/CheckboxInput.tsx
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,74 @@ | ||
import { QuestionField } from '@/types/form' | ||
import { useTranslations } from 'next-intl' | ||
import { useFormStore } from '@/store/form_store' | ||
import { getValidators } from '@/lib/utils/form-validator' | ||
import React from 'react' | ||
|
||
interface CheckboxInputProps extends QuestionField {} | ||
|
||
export const CheckboxInput = ({ | ||
field, | ||
register, | ||
errors, | ||
}: CheckboxInputProps) => { | ||
const t = useTranslations('general.errors') | ||
const { formState } = useFormStore() | ||
|
||
const errorMessage = errors[field.key]?.message as string | ||
|
||
// Check if the user has already answered a specific question. | ||
// Returns true if an answer exists where the id is same as the value key. | ||
// This is used to determine if the 'defaultChecked' property of a checkbox input should be set. | ||
const getDefaultValueCheckboxInput = (id: string, key: string) => { | ||
const extraProperties = formState.extra_properties.filter( | ||
(question) => question.id === id | ||
) | ||
|
||
if (!extraProperties.length) { | ||
return false | ||
} | ||
|
||
if (Array.isArray(extraProperties[0].answer)) { | ||
return ( | ||
extraProperties[0].answer.filter((answer) => answer?.id === key) | ||
.length > 0 | ||
) | ||
} | ||
|
||
return false | ||
} | ||
|
||
return ( | ||
<fieldset aria-invalid={!!errorMessage}> | ||
<legend> | ||
{field.meta.label}{' '} | ||
<span> {field.required ? '' : `(${t('not_required_short')})`}</span> | ||
{field.meta.subtitle && <span>{field.meta.subtitle}</span>} | ||
</legend> | ||
{errorMessage && ( | ||
<p | ||
id={`${field.key}-error`} | ||
aria-live="assertive" | ||
style={{ color: 'red' }} | ||
> | ||
{errorMessage} | ||
</p> | ||
)} | ||
{Object.keys(field.meta.values).map((key: string) => ( | ||
<div key={key}> | ||
<input | ||
{...register(`${field.key}.${key}`, getValidators(field, t))} | ||
type="checkbox" | ||
id={`${field.key}-${key}`} | ||
value={key} | ||
aria-describedby={errorMessage ? `${field.key}-error` : undefined} | ||
defaultChecked={getDefaultValueCheckboxInput(field.key, key)} | ||
/> | ||
<label htmlFor={`${field.key}-${key}`}> | ||
{field.meta.values[key]} | ||
</label> | ||
</div> | ||
))} | ||
</fieldset> | ||
) | ||
} |
23 changes: 23 additions & 0 deletions
23
src/app/[locale]/incident/add/components/questions/LocationSelect.tsx
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,23 @@ | ||
import { LocationMap } from '@/components/ui/LocationMap' | ||
import { MapDialog } from '@/app/[locale]/incident/add/components/MapDialog' | ||
import { Button } from '@/components/ui/Button' | ||
|
||
export const LocationSelect = (props: any) => { | ||
return ( | ||
<div className="relative"> | ||
<LocationMap /> | ||
{/* TODO: I can not find the reason why not every element inside this dialog is focusable */} | ||
<MapDialog | ||
marker={props.marker} | ||
trigger={ | ||
<Button | ||
className="absolute top-1/2 -translate-y-1/2 left-1/2 -translate-x-1/2 border-none" | ||
type="button" | ||
> | ||
Kies locatie | ||
</Button> | ||
} | ||
/> | ||
</div> | ||
) | ||
} |
15 changes: 15 additions & 0 deletions
15
src/app/[locale]/incident/add/components/questions/PlainText.tsx
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,15 @@ | ||
import { QuestionField } from '@/types/form' | ||
import Markdown from 'react-markdown' | ||
|
||
interface PlainTextProps extends Omit<QuestionField, 'register' | 'errors'> {} | ||
|
||
export const PlainText = ({ field }: PlainTextProps) => { | ||
// TODO: Discuss if alert is the only used PlainText type in Signalen, style Markdown | ||
return field.meta.value ? ( | ||
<div className="bg-red-100 rounded-lg p-4"> | ||
<Markdown>{field.meta.value}</Markdown> | ||
</div> | ||
) : ( | ||
<></> | ||
) | ||
} |
Oops, something went wrong.