# SurveyConfigForm

Drawer form for creating and editing survey-type step actions (type `FORM`) within an Employee Lifecycle process step. Integrates with the Survey Builder (SurveySetupScreen) to configure sections and questions.

## File structure

```
SurveyConfigForm/
├── index.tsx          # Re-exports: { SurveyConfigForm } from './form', default from './provider'
├── provider.tsx       # Data loading: useGetFormDefaultValues + useFormSummary → renders SurveyConfigForm
├── form.tsx           # Form shell: useForm, Drawer chrome (Header/Body/Actions), submit/cancel logic
├── content.tsx        # Pure field sections (no Drawer.* components); consumes form context
├── types.ts           # TypeScript types for form values, props, hooks (JSDoc documented)
├── schemas.ts         # Zod schemas: base (configure path) + full (save path)
├── fields.ts          # Field configuration generator (getSurveyConfigFormFields)
├── constants.ts       # Field name map (surveyConfigFormFields) + SURVEY_BLOCK_KEYS i18n constants
├── utils.ts           # getDefaultValues, getInitialDynamicFormData, validateSurveyContent
├── hooks/
│   ├── useDeleteSurveyAction.ts  # Mutation: delete action on cancel (cleanup workaround)
│   ├── useFormQuery.ts           # Query: fetch dynamic form by formTag
│   ├── useFormSummary.ts         # Derives sectionsCount / questionsCount from form query
│   └── useGetFormTag.ts          # Logic to obtain formId/formTag before opening builder
└── components/
    └── SurveyBlock/
        └── index.tsx  # UI block with "Configure / Edit survey" button; opens SurveySetupScreen
```

## Usage

Default export is the Provider (handles data loading). Use it when opening from a drawer layer.

```tsx
import SurveyConfigFormProvider from './SurveyConfigForm';

// Create
<SurveyConfigFormProvider processId={processId} stepId={stepId} />

// Edit
<SurveyConfigFormProvider processId={processId} stepId={stepId} actionId={actionId} />
```

Named export `SurveyConfigForm` (from `form.tsx`) can be used when `defaultValues` are already available (e.g. in tests or a custom provider).

## Form sections (content.tsx)

Sections are composed from shared components plus one survey-specific block:

| Section | Shared component | Key fields |
|---|---|---|
| Cover image | `CoverImageSection` | `hasCoverImage`, `coverImage` |
| Icon + name | `IconNameSection` | `icon`, `name` |
| Assignee + description | `AssigneeDescriptionSection` | `assignee`, `description` |
| **Survey** | `SurveyBlock` (local) | `dynamicFormData` |
| Attachments | `AttachmentsSection` | `hasAttachments`, `attachments` |
| Due date | `DueDateSection` | `hasDueDate`, `expiresAfter` |
| Reminders | `ReminderSection` | `hasReminder`, `remindOnDueDay`, `repeatReminder`, `reminderFrequency`, `reminderFrequencyType`, `reminderChannels` |
| Dependency | `DependencySection` | `hasDependency`, `dependsOn` |

## Key concepts

### Two-schema validation workaround (schemas.ts, form.tsx)

The API requires a FORM action to exist before a `formId`/`formTag` can be obtained, so the form has two submit modes:

- **`configure`** — triggered by the "Configure survey" button in `SurveyBlock`. Uses `createSurveyConfigFormBaseSchema` (skips `dynamicFormData` validation) to create the action and get `formId`/`formTag`.
- **`save`** — triggered by the drawer Save button. Uses `createSurveyConfigFormSchema` (requires `dynamicFormData` with at least 1 section and 1 question).

`SurveySubmitModeContext` (exported from `form.tsx`) lets `SurveyBlock` set the active mode before submission.

Remove the base schema and the `configure` mode when the API supports creating surveys independently of actions.

### Survey Builder flow (SurveyBlock/index.tsx)

1. User clicks "Configure survey": `SurveyBlock` sets mode to `'configure'` and submits the base fields.
2. On success, `actionId`, `formId`, and `formTag` are stored in form state.
3. `SurveySetupScreenProvider` opens in a new drawer layer (`useTaskFocus`).
4. On builder close, `sectionsCount` and `questionsCount` are computed via `parseFormSectionsToSummary` and written back to `dynamicFormData`.

### Cancel cleanup (useDeleteSurveyAction.ts)

If the user created a new action only to obtain a `formId`/`formTag` (workaround above) and then cancels without saving, `useDeleteSurveyAction` deletes that action to avoid orphaned records.

### Provider data flow

```
SurveyConfigFormProvider (provider.tsx)
  ├── useGetFormDefaultValues({ processId, stepId, actionId, getDefaultValuesFn })
  └── useFormSummary(formTag)  ← populates sectionsCount / questionsCount
        │
        ▼
  SurveyConfigForm (form.tsx)
  ├── useForm({ defaultValues, resolver: dynamic Zod resolver })
  ├── SurveyConfigFormContent (content.tsx)  ← field sections
  └── onSubmit → convertFormValuesToStepAction → updateOrCreateCustomTaskAction
```

## Related

- `../shared/` — shared field sections, hooks (`useTaskFormSubmit`, `useConfirmChangesModal`), transforms
- `../CustomTaskForm/` — same structure without Survey Builder integration
- `../../../../SurveySetupScreen/` — Survey Builder screen opened from `SurveyBlock`
- `../../../../../../types.ts` — `ActionType`, `UpdateOrCreateCustomTaskActionResponseType`

---

> Update this file when the module structure, form sections, or the survey-builder workaround changes.
