Skip to content

Commit

Permalink
refactor: reorganize validation schemas and types
Browse files Browse the repository at this point in the history
- Move Zod schemas to dedicated schema files
- Update type definitions to use schema inference
- Fix import paths to use absolute imports
- Remove duplicate schema definitions from components
- Centralize types in dedicated type files
  • Loading branch information
athens-server committed Jan 28, 2025
1 parent 415377a commit f85a00a
Show file tree
Hide file tree
Showing 21 changed files with 60 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,9 @@ import {
Spacer,
Text
} from '@/components'
import { newUserSchema } from '@/views/user-management/components/dialogs/create-user/schemas'
import { ICreateUserDialogProps, NewUserFields } from '@/views/user-management/components/dialogs/create-user/types'
import { zodResolver } from '@hookform/resolvers/zod'
import { z } from 'zod'

import { ICreateUserDialogProps } from './types'

const newUserSchema = z.object({
uid: z.string().min(1, { message: 'Please provide a user ID' }),
email: z.string().email({ message: 'Please enter a valid email address' }),
display_name: z.string().min(1, { message: 'Please provide a display name' })
})

export type NewUserFields = z.infer<typeof newUserSchema>

export function CreateUserDialog({ handleCreateUser, isLoading, apiError, open, onClose }: ICreateUserDialogProps) {
const {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { z } from 'zod'

export const newUserSchema = z.object({
uid: z.string().min(1, { message: 'Please provide a user ID' }),
email: z.string().email({ message: 'Please enter a valid email address' }),
display_name: z.string().min(1, { message: 'Please provide a display name' })
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { NewUserFields } from '../../create-user-dialog'
import { newUserSchema } from '@/views/user-management/components/dialogs/create-user/schemas'
import { z } from 'zod'

export type NewUserFields = z.infer<typeof newUserSchema>

export interface ICreateUserDialogProps {
handleCreateUser: (data: NewUserFields) => void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AlertDialog, Button, Spacer } from '@/components'

import { IDeleteDialogProps } from './types'
import { IDeleteDialogProps } from '@/views/user-management/components/dialogs/delete-user/types'

export const DeleteUserDialog: React.FC<IDeleteDialogProps> = ({
useAdminListUsersStore,
Expand All @@ -10,6 +9,7 @@ export const DeleteUserDialog: React.FC<IDeleteDialogProps> = ({
open
}) => {
const { user } = useAdminListUsersStore()

return (
<AlertDialog.Root open={open} onOpenChange={onClose}>
<AlertDialog.Trigger asChild></AlertDialog.Trigger>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { useEffect } from 'react'
import { SubmitHandler, useForm } from 'react-hook-form'

import { AlertDialog, Button, ButtonGroup, ControlGroup, Fieldset, FormWrapper, Input } from '@/components'
import { newUserSchema } from '@/views/user-management/components/dialogs/edit-user/schemas'
import { IEditUserDialogProps, MemberFields } from '@/views/user-management/components/dialogs/edit-user/types'
import { zodResolver } from '@hookform/resolvers/zod'
import { z } from 'zod'

import { IEditUserDialogProps } from './types'

export const EditUserDialog: React.FC<IEditUserDialogProps> = ({
useAdminListUsersStore,
Expand All @@ -15,13 +14,6 @@ export const EditUserDialog: React.FC<IEditUserDialogProps> = ({
open
}) => {
const { user } = useAdminListUsersStore()
const newUserSchema = z.object({
userID: z.string(),
email: z.string().email({ message: 'Please provide a valid email, ex: [email protected]' }),
displayName: z.string().min(1, { message: 'Please provide a display name' })
})

type MemberFields = z.infer<typeof newUserSchema>

const {
handleSubmit,
Expand All @@ -33,7 +25,6 @@ export const EditUserDialog: React.FC<IEditUserDialogProps> = ({
mode: 'onChange'
})

// Form edit submit handler
const onSubmit: SubmitHandler<MemberFields> = data => {
handleUpdateUser(data)
resetNewMemberForm(data)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { z } from 'zod'

export const newUserSchema = z.object({
userID: z.string(),
email: z.string().email({ message: 'Please provide a valid email, ex: [email protected]' }),
displayName: z.string().min(1, { message: 'Please provide a display name' })
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { IAdminListUsersStore } from 'dist/views'
import { IAdminListUsersStore } from '@/views'
import { newUserSchema } from '@/views/user-management/components/dialogs/edit-user/schemas'
import { z } from 'zod'

export type MemberFields = z.infer<typeof newUserSchema>

export interface IEditUserDialogProps {
isSubmitting: boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { AlertDialog, Button } from '@/components'
import { IRemoveAdminDialogProps } from '@/views/user-management/components/dialogs/remove-admin/types'

import { IRemoveAdminDialogProps } from './types'

// Form Remove/Add Admin Dialog
export const AdminDialog: React.FC<IRemoveAdminDialogProps> = ({
useAdminListUsersStore,
open,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { FC } from 'react'

import { AlertDialog, Button, CopyButton, Input, Text } from '@/components'

import { IResetPasswordDialogProps } from './types'
import { IResetPasswordDialogProps } from '@/views/user-management/components/dialogs/reset-password/types'

export const ResetPasswordDialog: FC<IResetPasswordDialogProps> = ({
open,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IAdminListUsersStore } from 'dist/views'
import { IAdminListUsersStore } from '@/views'

export interface IResetPasswordDialogProps {
onClose: () => void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NoData } from '@/components'

import { IEmptyStateProps } from './types'
import { IEmptyStateProps } from '@/views/user-management/components/empty-state/types'

export const EmptyState = ({ t, onButtonClick }: IEmptyStateProps) => {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Button, Filters, ListActions, SearchBox } from '@/components'

import { DialogLabels } from '../../../types'
import { ActionsProps } from './types'
import { ActionsProps } from '@/views/user-management/components/page-components/actions/types'
import { DialogLabels } from '@/views/user-management/types'

export const Actions = ({
searchQuery,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { UsersList } from './users-list'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { UsersProps } from '@views/user-management/types'

export interface PageProps {
users: UsersProps[]
handleDialogOpen: (user: UsersProps | null, dialogLabel: string) => void
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ import {
Text
} from '@/components'
import { getInitials } from '@/utils/utils'

import { DialogLabels, UsersProps } from '../types'

interface PageProps {
users: UsersProps[]
handleDialogOpen: (user: UsersProps | null, dialogLabel: string) => void
}
import { PageProps } from '@/views/user-management/components/page-components/content/components/users-list/types'
import { DialogLabels } from '@/views/user-management/types'

export const UsersList = ({ users, handleDialogOpen }: PageProps) => {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { FiltersBar, PaginationComponent, Spacer } from '@/components'
import { SandboxLayout } from '@/views'

import { DialogLabels } from '../../../types'
import { EmptyState } from '../../empty-state'
import { UsersList } from '../../users-list'
import { Actions } from '../actions'
import { Header } from '../header'
import { ContentProps } from './types'
import { EmptyState } from '@/views/user-management/components/empty-state'
import { Actions } from '@/views/user-management/components/page-components/actions'
import { UsersList } from '@/views/user-management/components/page-components/content/components/users-list'
import { ContentProps } from '@/views/user-management/components/page-components/content/types'
import { Header } from '@/views/user-management/components/page-components/header'
import { DialogLabels } from '@/views/user-management/types'

export const Content = ({
userData,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { TranslationStore } from '@/views'

import { UsersProps } from '../../../types'
import { UsersProps } from '@/views/user-management/types'

export interface ContentProps {
userData: UsersProps[] | null
Expand Down
7 changes: 2 additions & 5 deletions packages/ui/src/views/user-management/components/tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Tabs, TabsList, TabsTrigger } from '@/components'
import { SandboxLayout, TranslationStore } from '@/views'
import { EActiveTab } from '@/views/user-management/types'

import { EActiveTab } from '../types'

const UserManagementTabs = ({
export const UserManagementTabs = ({
activeTab,
setActiveTab,
useTranslationStore
Expand Down Expand Up @@ -32,5 +31,3 @@ const UserManagementTabs = ({
</SandboxLayout.SubHeader>
)
}

export { UserManagementTabs }
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMemo } from 'react'

import { filterItems } from '../utils'
import { filterItems } from '@/views/user-management/utils'

export const useUserManagement = (userData: any[], searchQuery: string) => {
const filteredUsers = useMemo(() => {
Expand Down
11 changes: 5 additions & 6 deletions packages/ui/src/views/user-management/user-management-page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { SandboxLayout } from '@/views'
import { Content } from '@/views/user-management/components/page-components/content'
import { UserManagementTabs } from '@/views/user-management/components/tabs'
import { useUserManagement } from '@/views/user-management/hooks/use-user-management'
import { IUserManagementPageProps } from '@/views/user-management/types'
import { getFilterOptions, getSortDirections, getSortOptions } from '@views/repo/constants/filter-options'
import { useFilters } from '@views/repo/hooks'

import { SandboxLayout } from '..'
import { Content } from './components/page-components/content'
import { UserManagementTabs } from './components/tabs'
import { useUserManagement } from './hooks/use-user-management'
import { IUserManagementPageProps } from './types'

export const UserManagementPage: React.FC<IUserManagementPageProps> = ({
useAdminListUsersStore,
useTranslationStore,
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/views/user-management/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UsersProps } from './types'
import { UsersProps } from '@/views/user-management/types'

export const filterItems = (items: UsersProps[], query: string) => {
if (!query.trim()) return items
Expand Down

0 comments on commit f85a00a

Please sign in to comment.