Skip to content

Commit

Permalink
refactor: rename a column and organise imports
Browse files Browse the repository at this point in the history
  • Loading branch information
GetPsyched committed Mar 30, 2024
1 parent 10a27c1 commit 484d21b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 49 deletions.
44 changes: 22 additions & 22 deletions actions/form.actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -313,7 +313,7 @@ interface formSum {
isAnonymous: boolean;
isViewAnalyticsAllowed: boolean;
description?: string;
expiryDate?: Date;
endedAt?: Date;
isQuiz: boolean;
}

Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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' };
}
Expand Down
14 changes: 7 additions & 7 deletions components/forms/FormSubmitForm.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
'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,
FormField,
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,
Expand Down
6 changes: 3 additions & 3 deletions components/forms/FormSubmitPage.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions components/forms/interfaces/FormElements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion components/forms/schemas/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion schemas/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
14 changes: 8 additions & 6 deletions server/schema/forms.schema.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()
Expand All @@ -51,6 +51,8 @@ export const forms = pgTable('forms', {
questionValidations: json('question_validations').$type<
Record<string, validationProperty>
>(),
isActive: boolean('is_active').default(true).notNull(),
endedAt: timestamp('ended_at'),
});
export const formRelations = relations(forms, ({ many }) => ({
questions: many(formQuestions),
Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit 484d21b

Please sign in to comment.