Skip to content

Commit

Permalink
code cleanu
Browse files Browse the repository at this point in the history
  • Loading branch information
ddimitrov1108 committed Jan 30, 2024
1 parent 055148a commit 35a0c00
Show file tree
Hide file tree
Showing 39 changed files with 241 additions and 256 deletions.
2 changes: 1 addition & 1 deletion app/actions/label/createLabel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use server";

import { getUserFromServerSession } from "@/lib/auth";
import { LabelFormValues } from "@/lib/form-schemas";
import { LabelFormValues } from "@/lib/interfaces/form-values";
import { revalidatePath } from "next/cache";

export default async function createLabel(values: LabelFormValues) {
Expand Down
2 changes: 1 addition & 1 deletion app/actions/label/updateLabel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use server";

import { getUserFromServerSession } from "@/lib/auth";
import { LabelFormValues } from "@/lib/form-schemas";
import { LabelFormValues } from "@/lib/interfaces/form-values";
import { isUUID } from "@/lib/utils";
import { revalidatePath } from "next/cache";

Expand Down
2 changes: 1 addition & 1 deletion app/actions/project/createProject.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use server";

import { getUserFromServerSession } from "@/lib/auth";
import { ProjectFormValues } from "@/lib/form-schemas";
import { ProjectFormValues } from "@/lib/interfaces/form-values";
import { revalidatePath } from "next/cache";

export default async function createProject(values: ProjectFormValues) {
Expand Down
2 changes: 1 addition & 1 deletion app/actions/project/updateProject.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use server";

import { getUserFromServerSession } from "@/lib/auth";
import { ProjectFormValues } from "@/lib/form-schemas";
import { ProjectFormValues } from "@/lib/interfaces/form-values";
import { isUUID } from "@/lib/utils";
import { revalidatePath } from "next/cache";

Expand Down
2 changes: 1 addition & 1 deletion app/actions/task/createTask.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use server";

import { getUserFromServerSession } from "@/lib/auth";
import { TaskFormValues } from "@/lib/form-schemas";
import { TaskFormValues } from "@/lib/interfaces/form-values";
import { isUUID } from "@/lib/utils";
import { revalidatePath } from "next/cache";

Expand Down
2 changes: 1 addition & 1 deletion app/actions/task/updateTask.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use server";

import { getUserFromServerSession } from "@/lib/auth";
import { TaskFormValues } from "@/lib/form-schemas";
import { TaskFormValues } from "@/lib/interfaces/form-values";
import { isUUID } from "@/lib/utils";
import { revalidatePath } from "next/cache";

Expand Down
2 changes: 1 addition & 1 deletion app/actions/user/changePasswordByToken.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use server";

import userController from "@/db/UserController";
import { ChangePasswordFormValues } from "@/lib/form-schemas";
import { ChangePasswordFormValues } from "@/lib/interfaces/form-values";

export const changePasswordByToken = async (
resetPasswordToken: string,
Expand Down
2 changes: 1 addition & 1 deletion app/actions/user/resetPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import userController from "@/db/UserController";
import { ResetPasswordEmailTemplate } from "@/components/email-temlates/ResetPasswordEmailTemplate";
import { ResetPasswordFormValues } from "@/lib/form-schemas";
import { ResetPasswordFormValues } from "@/lib/interfaces/form-values";

export const resetPassword = async (values: ResetPasswordFormValues) => {
if (!userController.validatePasswordReset(values))
Expand Down
4 changes: 2 additions & 2 deletions components/forms/ChangePasswordWithTokenForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import dynamic from "next/dynamic";
import useForm from "../hooks/useForm";
import { ChangePasswordFormValues, FormErrors } from "@/lib/form-schemas";
import { ChangePasswordFormValues, FormErrors } from "@/lib/interfaces/form-values";
import PasswordField from "./formik/PasswordField";
import Button from "../ui/Button";
import { useRouter } from "next/navigation";
Expand All @@ -21,7 +21,7 @@ const ChangePasswordWithTokenForm = ({ resetPasswordToken}: Props) => {

const onValidateHandler = async (values: ChangePasswordFormValues) => {
try {
(await import("@/lib/form-schemas")).changePasswordSchema.parse(values);
(await import("@/lib/interfaces/form-schemas")).changePasswordSchema.parse(values);

if (values.password !== values.confirmPassword)
throw new Error("Passwords do not match.");
Expand Down
6 changes: 3 additions & 3 deletions components/forms/LabelForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import { useParams } from "next/navigation";
import useForm from "../hooks/useForm";
import { Field, Form, Formik } from "formik";
import { FormErrors, LabelFormValues } from "@/lib/form-schemas";
import { FormErrors, LabelFormValues } from "@/lib/interfaces/form-values";
import { toast } from "sonner";
import { IForm } from "@/lib/interfaces";
import { IForm } from "@/lib/interfaces/form";
import Button from "../ui/Button";
import TextField from "./formik/TextField";
import dynamic from "next/dynamic";
Expand All @@ -22,7 +22,7 @@ const LabelForm = ({

const onValidateHandler = async (values: LabelFormValues) => {
try {
(await import("@/lib/form-schemas")).labelFormSchema.parse(values);
(await import("@/lib/interfaces/form-schemas")).labelFormSchema.parse(values);
} catch (error) {
if (error instanceof FormErrors) return error.formErrors.fieldErrors;
}
Expand Down
6 changes: 3 additions & 3 deletions components/forms/ProjectForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import { useParams, useRouter } from "next/navigation";
import useForm from "../hooks/useForm";
import { Field, Form, Formik } from "formik";
import { FormErrors, ProjectFormValues } from "@/lib/form-schemas";
import { FormErrors, ProjectFormValues } from "@/lib/interfaces/form-values";
import { toast } from "sonner";
import { IForm } from "@/lib/interfaces";
import { IForm } from "@/lib/interfaces/form";
import Button from "../ui/Button";
import TextField from "./formik/TextField";
import ColorPickerField from "./formik/ColorPickerField";
Expand All @@ -24,7 +24,7 @@ const ProjectForm = ({

const onValidateHandler = async (values: ProjectFormValues) => {
try {
(await import("@/lib/form-schemas")).projectFormSchema.parse(values);
(await import("@/lib/interfaces/form-schemas")).projectFormSchema.parse(values);
} catch (error) {
if (error instanceof FormErrors) return error.formErrors.fieldErrors;
}
Expand Down
4 changes: 2 additions & 2 deletions components/forms/ResetPasswordForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import dynamic from "next/dynamic";
import useForm from "../hooks/useForm";
import { ResetPasswordFormValues, FormErrors } from "@/lib/form-schemas";
import { ResetPasswordFormValues, FormErrors } from "@/lib/interfaces/form-values";
import Button from "../ui/Button";
import { useRouter } from "next/navigation";
import { Field, Form, Formik } from "formik";
Expand All @@ -17,7 +17,7 @@ const ResetPasswordForm = () => {

const onValidateHandler = async (values: ResetPasswordFormValues) => {
try {
(await import("@/lib/form-schemas")).resetPasswordSchema.parse(values);
(await import("@/lib/interfaces/form-schemas")).resetPasswordSchema.parse(values);
} catch (error) {
if (error instanceof FormErrors) return error.formErrors.fieldErrors;
}
Expand Down
4 changes: 2 additions & 2 deletions components/forms/SignInForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useRouter } from "next/navigation";
import useForm from "../hooks/useForm";
import { Field, Form, Formik } from "formik";
import { FormErrors, SignInFormValues } from "@/lib/form-schemas";
import { FormErrors, SignInFormValues } from "@/lib/interfaces/form-values";
import Link from "next/link";
import TextField from "./formik/TextField";
import PasswordField from "./formik/PasswordField";
Expand All @@ -18,7 +18,7 @@ const SignInForm = () => {

const onValidateHandler = async (values: SignInFormValues) => {
try {
(await import("@/lib/form-schemas")).signInFormSchema.parse(values);
(await import("@/lib/interfaces/form-schemas")).signInFormSchema.parse(values);
} catch (error) {
if (error instanceof FormErrors) return error.formErrors.fieldErrors;
}
Expand Down
4 changes: 2 additions & 2 deletions components/forms/SignUpForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useRouter } from "next/navigation";
import useForm from "../hooks/useForm";
import { Field, Form, Formik } from "formik";
import { FormErrors, SignUpFormValues } from "@/lib/form-schemas";
import { FormErrors, SignUpFormValues } from "@/lib/interfaces/form-values";
import Link from "next/link";
import TextField from "./formik/TextField";
import PasswordField from "./formik/PasswordField";
Expand All @@ -18,7 +18,7 @@ const SignUpForm = () => {

const onValidateHandler = async (values: SignUpFormValues) => {
try {
(await import("@/lib/form-schemas")).signUpFormSchema.parse(values);
(await import("@/lib/interfaces/form-schemas")).signUpFormSchema.parse(values);

if (values.password !== values.confirmPassword)
throw new Error("Passwords do not match.");
Expand Down
14 changes: 8 additions & 6 deletions components/forms/TaskForm.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use client";

import { Field, Form, Formik } from "formik";
import { FormErrors, TaskFormValues, taskFormSchema } from "@/lib/form-schemas";
import { FormErrors, TaskFormValues } from "@/lib/interfaces/form-values";
import Button from "../ui/Button";
import TextField from "./formik/TextField";
import DatePickerField from "./formik/DatePickerField";
import TextareaField from "./formik/TextareaField";
import CheckboxField from "./formik/CheckboxField";
import SwitchField from "./formik/SwitchField";
import dynamic from "next/dynamic";
import useForm from "../hooks/useForm";
import { useParams, usePathname } from "next/navigation";
Expand All @@ -32,7 +32,9 @@ const TaskForm = ({ initialState, editMode = false, afterSubmit }: Props) => {

const onValidateHandler = async (values: TaskFormValues) => {
try {
taskFormSchema.parse(values);
(await import("@/lib/interfaces/form-schemas")).taskFormSchema.parse(
values
);
} catch (error) {
if (error instanceof FormErrors) return error.formErrors.fieldErrors;
}
Expand Down Expand Up @@ -118,7 +120,7 @@ const TaskForm = ({ initialState, editMode = false, afterSubmit }: Props) => {
component={DatePickerField}
fullWidth
/>

<Field
id="labels"
name="labels"
Expand Down Expand Up @@ -146,7 +148,7 @@ const TaskForm = ({ initialState, editMode = false, afterSubmit }: Props) => {
type="checkbox"
disabled={form.loading}
maxLength={4}
component={CheckboxField}
component={SwitchField}
fullWidth
/>

Expand All @@ -157,7 +159,7 @@ const TaskForm = ({ initialState, editMode = false, afterSubmit }: Props) => {
type="checkbox"
disabled={form.loading}
maxLength={4}
component={CheckboxField}
component={SwitchField}
fullWidth
/>

Expand Down
6 changes: 3 additions & 3 deletions components/forms/formik/ColorPickerField.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IFormInput } from "@/lib/interfaces";
import { IFormField } from "@/lib/interfaces/form";
import cn from "@/lib/cn";
import Label from "./Label";
import dynamic from "next/dynamic";
Expand Down Expand Up @@ -35,15 +35,15 @@ const ColorPickerField = ({
form: { setFieldValue, touched, errors },
fullWidth,
disabled,
}: IFormInput<string>) => {
}: IFormField<string>) => {
const onClickHandler = (e: React.MouseEvent<HTMLButtonElement>) =>
setFieldValue(field.name, e.currentTarget.value);

return (
<div
className={cn("mb-4", fullWidth ? "w-full" : "w-fit", containerClassName)}
>
<Label className="pb-2" htmlFor={field.name} label={label} />
<Label className="pb-2" htmlFor={field.name} text={label} />

<div className="w-full flex flex-wrap gap-1">
{colorPickerColors.map((color) => (
Expand Down
8 changes: 3 additions & 5 deletions components/forms/formik/DatePickerField.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { IFormInput } from "@/lib/interfaces";
import { IFormField } from "@/lib/interfaces/form";
import cn from "@/lib/cn";
import { format } from "date-fns";
import Label from "./Label";
import dynamic from "next/dynamic";

const FormErrorMessage = dynamic(() => import("./FormErrorMessage"));

type Props = IFormInput<string> & React.ComponentProps<"input">;

const DatePickerField = ({
label = "",
type = "date",
Expand All @@ -17,7 +15,7 @@ const DatePickerField = ({
form: { setFieldValue, touched, errors },
fullWidth,
...restProps
}: Props) => {
}: IFormField<string> & React.ComponentProps<"input">) => {
const onChangeHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
setFieldValue(field.name, format(new Date(e.target.value), "yyyy-MM-dd"));
};
Expand All @@ -26,7 +24,7 @@ const DatePickerField = ({
<div
className={cn("mb-4", fullWidth ? "w-full" : "w-fit", containerClassName)}
>
<Label className="pb-2" htmlFor={field.name} label={label} />
<Label className="pb-2" htmlFor={field.name} text={label} />

<input
type="date"
Expand Down
7 changes: 3 additions & 4 deletions components/forms/formik/FormErrorMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import cn from "@/lib/cn";
import { FormikErrors } from "formik";

type Props = {
interface Props extends React.ComponentProps<"div"> {
message?:
| string
| FormikErrors<any>
| string[]
| FormikErrors<any>[]
| undefined;
className?: string;
};
}

const FormErrorMessage = ({ message, className }: Props) => {
return (
Expand All @@ -19,7 +18,7 @@ const FormErrorMessage = ({ message, className }: Props) => {
className
)}
>
<>{Array.isArray(message) ? message[0] : message}</>
<>{message as string}</>
</div>
);
};
Expand Down
14 changes: 6 additions & 8 deletions components/forms/formik/Label.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import cn from "@/lib/cn";

type Props = {
label: string;
htmlFor?: string;
className?: string;
};
interface Props extends React.ComponentProps<"label"> {
text: string;
}

const Label = ({ label, htmlFor, className }: Props) => {
const Label = ({ text, className, ...restProps }: Props) => {
return (
<div className={cn("font-medium text-sm text-light min-w-fit", className)}>
<label htmlFor={htmlFor}>{label}</label>
<div className={cn("font-medium text-sm text-light w-fit", className)}>
<label {...restProps}>{text}</label>
</div>
);
};
Expand Down
Loading

0 comments on commit 35a0c00

Please sign in to comment.