From 484d21bce7999f3e89ab4d0c0b495088317cf1aa Mon Sep 17 00:00:00 2001 From: GetPsyched Date: Sat, 30 Mar 2024 11:39:35 +0530 Subject: [PATCH] refactor: rename a column and organise imports --- actions/form.actions.tsx | 44 ++++++++++---------- components/forms/FormSubmitForm.tsx | 14 +++---- components/forms/FormSubmitPage.tsx | 6 +-- components/forms/interfaces/FormElements.tsx | 18 ++++---- components/forms/schemas/form.ts | 2 +- schemas/form.ts | 2 +- server/schema/forms.schema.ts | 14 ++++--- 7 files changed, 51 insertions(+), 49 deletions(-) diff --git a/actions/form.actions.tsx b/actions/form.actions.tsx index e00659f8..1ad0dca8 100644 --- a/actions/form.actions.tsx +++ b/actions/form.actions.tsx @@ -2,33 +2,33 @@ import Ajv, { type ValidateFunction } from 'ajv'; import AjvFormats from 'ajv-formats'; +import { and, eq, or } from 'drizzle-orm'; import { revalidatePath } from 'next/cache'; +import FormInvalidResponse, { + type FormInvalidResponseProps, +} from '~/components/forms/FormInvalidResponse'; +import FormSubmitPage, { + type FormSubmitFormProps, +} from '~/components/forms/FormSubmitPage'; import { - type ElementsType, FormElements, + type ElementsType, + type validationProperty, } from '~/components/forms/interfaces/FormElements'; -import { and, eq, or } from 'drizzle-orm'; -import { db } from '~/server/db'; -import { - forms, - formsModifiableByPersons, - formQuestions, - formSubmissions, - formAnswers, -} from '~/server/schema'; import { finalFormSchema, formSchema, type formSchemaType, } from '~/schemas/form'; -import type { validationProperty } from '~/components/forms/interfaces/FormElements'; -import FormSubmitPage, { - type FormSubmitFormProps, -} from '~/components/forms/FormSubmitPage'; -import FormInvalidResponse, { - type FormInvalidResponseProps, -} from '~/components/forms/FormInvalidResponse'; +import { db } from '~/server/db'; +import { + formAnswers, + formQuestions, + formSubmissions, + forms, + formsModifiableByPersons, +} from '~/server/schema'; const ajv = new Ajv({ allErrors: true, @@ -313,7 +313,7 @@ interface formSum { isAnonymous: boolean; isViewAnalyticsAllowed: boolean; description?: string; - expiryDate?: Date; + endedAt?: Date; isQuiz: boolean; } @@ -340,7 +340,7 @@ export async function publishForm( isSingleResponse: form.isSingleResponse, isAnonymous: form.isAnonymous, isViewAnalyticsAllowed: form.isViewAnalyticsAllowed, - expiryDate: form.expiryDate, + endedAt: form.endedAt, isQuiz: form.isQuiz, isPublished: true, questionValidations: properties, @@ -435,7 +435,7 @@ export async function getFormForSubmission(id: number): Promise<{ }, }; - if (form.expiryDate && form.expiryDate < new Date()) { + if (form.endedAt && form.endedAt < new Date()) { await db.update(forms).set({ isActive: false }); return { @@ -555,7 +555,7 @@ export async function submitForm( questionValidations: forms.questionValidations, requiredQuestions: forms.requiredQuestions, isAnonymous: forms.isAnonymous, - expiryDate: forms.expiryDate, + endedAt: forms.endedAt, isSingleResponse: forms.isSingleResponse, isEditingAllowed: forms.isEditingAllowed, onSubmitMessage: forms.onSubmitMessage, @@ -598,7 +598,7 @@ export async function submitForm( if (!form.isActive) return { title: 'Error', description: 'Form is expired' }; - if (form.expiryDate && form.expiryDate < new Date()) { + if (form.endedAt && form.endedAt < new Date()) { await db.update(forms).set({ isActive: true }).where(eq(forms.id, id)); return { title: 'Error', description: 'Form is expired' }; } diff --git a/components/forms/FormSubmitForm.tsx b/components/forms/FormSubmitForm.tsx index 67a0d939..0ba7cd29 100644 --- a/components/forms/FormSubmitForm.tsx +++ b/components/forms/FormSubmitForm.tsx @@ -1,17 +1,14 @@ 'use client'; -import { useState } from 'react'; import { motion } from 'framer-motion'; -import { useForm } from 'react-hook-form'; import { useRouter } from 'next/navigation'; +import { useState } from 'react'; +import { useForm } from 'react-hook-form'; import { submitForm } from '~/actions/form.actions'; import { validateResolver } from '~/lib/validateResolver'; -import { - FormElements, - type validationProperty, -} from './interfaces/FormElements'; +import { Button } from '../ui/button'; import { Form, FormControl, @@ -19,10 +16,13 @@ import { FormItem, FormMessage, } from '../ui/form'; -import { Button } from '../ui/button'; import { Separator } from '../ui/separator'; import { toast } from '../ui/use-toast'; import type { FormSubmitFormProps } from './FormSubmitPage'; +import { + FormElements, + type validationProperty, +} from './interfaces/FormElements'; export default function FormSubmitForm({ locale, diff --git a/components/forms/FormSubmitPage.tsx b/components/forms/FormSubmitPage.tsx index 0cb3aff0..a6698f45 100644 --- a/components/forms/FormSubmitPage.tsx +++ b/components/forms/FormSubmitPage.tsx @@ -1,9 +1,9 @@ import { Separator } from '../ui/separator'; import FormDetails from './FormDetails'; import FormSubmitForm from './FormSubmitForm'; -import type { - ElementsType, - validationProperty, +import { + type ElementsType, + type validationProperty, } from './interfaces/FormElements'; export interface FormSubmitFormProps { diff --git a/components/forms/interfaces/FormElements.tsx b/components/forms/interfaces/FormElements.tsx index 7a28ba92..c0cf7e1b 100644 --- a/components/forms/interfaces/FormElements.tsx +++ b/components/forms/interfaces/FormElements.tsx @@ -4,21 +4,21 @@ import type { RefAttributes, } from 'react'; -import { type InputProps } from '~/components/ui/input'; +import { type MultiListProps } from '~/components/inputs/multiSelectItem'; import { type ListProps } from '~/components/inputs/radioItems'; -import type { MultiListProps } from '~/components/inputs/multiSelectItem'; +import { type InputProps } from '~/components/ui/input'; -import { TextFieldFormElement } from '../fields/TextFieldFormElement'; -import { TimeFieldFormElement } from '../fields/TimeFieldFormElement'; -import { EmailFieldFormElement } from '../fields/EmailFieldFormElement'; import { DateFieldFormElement } from '../fields/DateFieldFormElement'; -import { PhoneFieldFormElement } from '../fields/PhoneFieldFormElement'; -import { SelectDropdownFormElement } from '../fields/SelectDropdownFormElement'; -import { RadioGenericFormElement } from '../fields/RadioGenericFormElement'; import { DateTimeFieldFormElement } from '../fields/DateTimeFieldFormElement'; +import { EmailFieldFormElement } from '../fields/EmailFieldFormElement'; +import { MultiSelectFormElement } from '../fields/MultiSelectFormElement'; import { NumberFieldFormElement } from '../fields/NumberFieldFormElement'; +import { PhoneFieldFormElement } from '../fields/PhoneFieldFormElement'; +import { RadioGenericFormElement } from '../fields/RadioGenericFormElement'; +import { SelectDropdownFormElement } from '../fields/SelectDropdownFormElement'; import { TextAreaFieldFormElement } from '../fields/TextAreaFormElement'; -import { MultiSelectFormElement } from '../fields/MultiSelectFormElement'; +import { TextFieldFormElement } from '../fields/TextFieldFormElement'; +import { TimeFieldFormElement } from '../fields/TimeFieldFormElement'; export type ElementsType = | 'TextField' diff --git a/components/forms/schemas/form.ts b/components/forms/schemas/form.ts index cf49f738..113b8519 100644 --- a/components/forms/schemas/form.ts +++ b/components/forms/schemas/form.ts @@ -18,7 +18,7 @@ export const finalFormSchema = z isViewAnalyticsAllowed: z.boolean().default(false), isShuffled: z.boolean().default(false), isCopySent: z.boolean().default(false), - expiryDate: z + endedAt: z .date() .min(new Date(), { message: "form cannot expire before it's creation." }) .optional(), diff --git a/schemas/form.ts b/schemas/form.ts index cf49f738..113b8519 100644 --- a/schemas/form.ts +++ b/schemas/form.ts @@ -18,7 +18,7 @@ export const finalFormSchema = z isViewAnalyticsAllowed: z.boolean().default(false), isShuffled: z.boolean().default(false), isCopySent: z.boolean().default(false), - expiryDate: z + endedAt: z .date() .min(new Date(), { message: "form cannot expire before it's creation." }) .optional(), diff --git a/server/schema/forms.schema.ts b/server/schema/forms.schema.ts index 72344d8e..6220768b 100644 --- a/server/schema/forms.schema.ts +++ b/server/schema/forms.schema.ts @@ -1,17 +1,19 @@ import { relations, sql } from 'drizzle-orm'; import { boolean, - date, integer, + json, + jsonb, pgTable, real, serial, smallint, + timestamp, varchar, - json, - jsonb, } from 'drizzle-orm/pg-core'; + import { persons } from '.'; + //temp export interface validationProperty { type: string; @@ -36,8 +38,6 @@ export const forms = pgTable('forms', { isShuffled: boolean('is_shuffled').default(false).notNull(), isCopySent: boolean('is_copy_sent').default(false).notNull(), isQuiz: boolean('is_quiz').default(false).notNull(), - expiryDate: date('date', { mode: 'date' }), - isActive: boolean('is_active').default(true).notNull(), persistentUrl: varchar('persistent_url'), oldPersistentUrls: varchar('old_persistent_urls') .array() @@ -51,6 +51,8 @@ export const forms = pgTable('forms', { questionValidations: json('question_validations').$type< Record >(), + isActive: boolean('is_active').default(true).notNull(), + endedAt: timestamp('ended_at'), }); export const formRelations = relations(forms, ({ many }) => ({ questions: many(formQuestions), @@ -163,7 +165,7 @@ export const formAnswers = pgTable('form_answers', { submissionId: integer('submission_id').notNull(), value: jsonb('value').notNull(), }); -export const formAnswersRelations = relations(formAnswers, ({ one, many }) => ({ +export const formAnswersRelations = relations(formAnswers, ({ one }) => ({ questions: one(formQuestions, { fields: [formAnswers.questionId], references: [formQuestions.id],