-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor ui improvements saving input word to the db
- Loading branch information
1 parent
338e6d6
commit f6c6f23
Showing
29 changed files
with
303 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
app/(app)/(add-word)/components/thematic-category-select.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
import { titleCase } from 'title-case' | ||
import { categoriesAtom } from '@/app/(app)/(add-word)/state' | ||
import type { AddWordFormValues, Category } from '@/app/(app)/(add-word)/types' | ||
import { Button } from '@/components/ui/button' | ||
import { | ||
Command, | ||
CommandEmpty, | ||
CommandGroup, | ||
CommandInput, | ||
CommandItem, | ||
CommandList, | ||
} from '@/components/ui/command' | ||
import { FormField, FormItem, FormLabel } from '@/components/ui/form' | ||
import { | ||
Popover, | ||
PopoverContent, | ||
PopoverTrigger, | ||
} from '@/components/ui/popover' | ||
import { cn } from '@/lib/utils' | ||
import { useAtom } from 'jotai' | ||
import { CheckIcon, CirclePlusIcon, PlusIcon } from 'lucide-react' | ||
import { useState } from 'react' | ||
import type { UseFormReturn } from 'react-hook-form' | ||
import { Badge } from '@/components/ui/badge' | ||
import { useCommandState } from 'cmdk' | ||
|
||
type ThematicCategorySelectProps = { | ||
form: UseFormReturn<AddWordFormValues> | ||
} | ||
|
||
const EmptyState = ({ | ||
onAdd, | ||
}: { | ||
onAdd: (name: string) => void | ||
}) => { | ||
const search = useCommandState(state => state.search) | ||
|
||
return ( | ||
<div | ||
onClick={() => onAdd(search)} | ||
onKeyDown={e => { | ||
if (e.key === 'Enter' || e.key === ' ') { | ||
onAdd(search) | ||
} | ||
}} | ||
className="relative flex gap-2 cursor-pointer select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none hover:bg-accent" | ||
> | ||
<PlusIcon className="size-3" /> Add new category | ||
</div> | ||
) | ||
} | ||
|
||
export const ThematicCategorySelect = ({ | ||
form, | ||
}: ThematicCategorySelectProps) => { | ||
const [categories, setCategories] = useAtom(categoriesAtom) | ||
const [open, setOpen] = useState(false) | ||
|
||
const removeTCategory = ( | ||
event: | ||
| React.MouseEvent<HTMLDivElement> | ||
| React.KeyboardEvent<HTMLDivElement>, | ||
category: Category, | ||
) => { | ||
event.stopPropagation() | ||
|
||
const newCategories = form | ||
?.getValues('category') | ||
?.filter(c => c.id !== category.id) | ||
|
||
form.setValue('category', newCategories) | ||
} | ||
|
||
const addNewCategory = (name: string) => { | ||
setCategories([ | ||
...categories, | ||
{ | ||
id: performance.now(), | ||
name, | ||
create: true, | ||
}, | ||
]) | ||
} | ||
|
||
// TODO: combobox on desktop, drawer on mobile | ||
return ( | ||
<FormField | ||
control={form.control} | ||
name="category" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel> | ||
Thematic category{' '} | ||
<span className="text-xs text-muted-foreground">(max 2)</span> | ||
</FormLabel> | ||
<Popover open={open} onOpenChange={setOpen}> | ||
<div className="flex items-center justify-between gap-2 p-1 pl-3 border rounded-md"> | ||
{field.value?.map(category => ( | ||
<Badge | ||
className="cursor-pointer" | ||
onClick={e => removeTCategory(e, category)} | ||
key={category.id} | ||
tabIndex={0} | ||
onKeyDown={e => { | ||
if (e.key === 'Enter' || e.key === ' ') { | ||
removeTCategory(e, category) | ||
} | ||
}} | ||
> | ||
{titleCase(category.name)} | ||
</Badge> | ||
))} | ||
<PopoverTrigger asChild> | ||
<Button | ||
type="button" | ||
variant="ghost" | ||
role="combobox" | ||
size="sm" | ||
className="justify-between ml-auto" | ||
> | ||
<CirclePlusIcon className="size-4" /> | ||
</Button> | ||
</PopoverTrigger> | ||
</div> | ||
<PopoverContent className="w-[200px] p-0" align="end"> | ||
<Command> | ||
<CommandInput | ||
placeholder="Search category..." | ||
className="h-9" | ||
/> | ||
<CommandList> | ||
<CommandEmpty className="p-1"> | ||
<EmptyState onAdd={addNewCategory} /> | ||
</CommandEmpty> | ||
<CommandGroup> | ||
{categories.map(category => ( | ||
<CommandItem | ||
className="flex items-center gap-2" | ||
value={category.name} | ||
key={category.id} | ||
disabled={ | ||
field.value?.length === 2 && | ||
!field.value.some(c => c.id === category.id) | ||
} | ||
onSelect={() => { | ||
let newCategories = [...(field.value || [])] | ||
|
||
if (newCategories.some(c => c.id === category.id)) { | ||
newCategories = newCategories.filter( | ||
c => c.id !== category.id, | ||
) | ||
} else { | ||
newCategories = [...newCategories, category] | ||
} | ||
|
||
form.setValue('category', newCategories) | ||
}} | ||
> | ||
<CheckIcon | ||
className={cn( | ||
'size-3', | ||
field.value?.some(c => c.id === category.id) | ||
? 'opacity-100' | ||
: 'opacity-0', | ||
)} | ||
/> | ||
{titleCase(category.name)} | ||
</CommandItem> | ||
))} | ||
</CommandGroup> | ||
</CommandList> | ||
</Command> | ||
</PopoverContent> | ||
</Popover> | ||
</FormItem> | ||
)} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { Prisma } from '@prisma/client' | ||
|
||
export const CategoriesArgs = { | ||
select: { | ||
id: true, | ||
name: true, | ||
}, | ||
distinct: 'name', | ||
orderBy: { | ||
name: 'asc', | ||
}, | ||
} satisfies Prisma.CategoryFindManyArgs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,27 @@ | ||
import { z } from 'zod' | ||
import { WordSchema } from '@/prisma/zod/word' | ||
import { z, type ZodType } from 'zod' | ||
import { | ||
DifficultyCategory, | ||
FrequencyCategory, | ||
RegisterCategory, | ||
} from '@prisma/client' | ||
import type { Category } from '@/app/(app)/(add-word)/types' | ||
|
||
export const AddWordFormSchema = WordSchema.omit({ | ||
id: true, | ||
createdAt: true, | ||
updatedAt: true, | ||
userId: true, | ||
export const AddWordFormSchema = z.object({ | ||
word: z.string().min(1, 'Word must be at least 1 character long'), | ||
translation: z | ||
.string() | ||
.min(1, 'Translation must be at least 1 character long'), | ||
description: z.string().optional(), | ||
difficultyCategory: z.nativeEnum(DifficultyCategory).optional(), | ||
frequencyCategory: z.nativeEnum(FrequencyCategory).optional(), | ||
registerCategory: z.nativeEnum(RegisterCategory).optional(), | ||
category: z | ||
.array( | ||
z.object({ | ||
id: z.number(), | ||
name: z.string(), | ||
create: z.boolean().optional(), | ||
}) satisfies ZodType<Category>, | ||
) | ||
.optional(), | ||
}) | ||
.extend({ | ||
description: z.string().optional(), | ||
difficultyCategory: z.nativeEnum(DifficultyCategory).optional(), | ||
frequencyCategory: z.nativeEnum(FrequencyCategory).optional(), | ||
registerCategory: z.nativeEnum(RegisterCategory).optional(), | ||
}) | ||
.superRefine((data, ctx) => { | ||
if (data.word.length < 1) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: 'Word must be at least 1 character long', | ||
path: ['word'], | ||
}) | ||
} | ||
|
||
if (data.translation.length < 1) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: 'Translation must be at least 1 character long', | ||
path: ['translation'], | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import type { CategoryWithCreate } from '@/app/(app)/(add-word)/types' | ||
import { atom } from 'jotai' | ||
|
||
export const categoriesAtom = atom<CategoryWithCreate[]>([]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
import type { z } from 'zod' | ||
import type { AddWordFormSchema } from './schemas' | ||
import type { CategoriesArgs } from '@/app/(app)/(add-word)/query-args' | ||
import type { Prisma } from '@prisma/client' | ||
|
||
export type AddWordFormValues = z.infer<typeof AddWordFormSchema> | ||
export type Category = Prisma.CategoryGetPayload<typeof CategoriesArgs> | ||
export type CategoryWithCreate = Category & { | ||
create?: true | ||
} |
Oops, something went wrong.