feat: add project select to the asset form
This commit is contained in:
parent
fc875ec6ca
commit
39896998bc
35
app/components/assets/form/project-select-field.tsx
Normal file
35
app/components/assets/form/project-select-field.tsx
Normal file
@ -0,0 +1,35 @@
|
||||
import { useFormContext, useWatch } from 'react-hook-form';
|
||||
import { ClientLogo } from '~/components/ui/client-logo';
|
||||
import { Field } from '~/components/ui/field';
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '~/components/ui/select';
|
||||
import { NO_PROJECT } from '~/data/assets/constants';
|
||||
import { useAdminData } from '~/hooks/admin/use-admin-data';
|
||||
import type { AssetFormValues } from '~/schemas/assets/asset-form';
|
||||
import { toBrand } from '~/services/asset-media';
|
||||
|
||||
export function ProjectSelectField() {
|
||||
const { projects } = useAdminData();
|
||||
const { control, setValue } = useFormContext<AssetFormValues>();
|
||||
const value = useWatch({ control, name: 'project' });
|
||||
// Archived projects are only listed when the asset already belongs to one
|
||||
const options = projects.filter((project) => project.status === 'active' || project.id === value);
|
||||
|
||||
return (
|
||||
<Field label='Project' htmlFor='asset-project' hint={value ? undefined : 'Unassigned assets show only the thob logo on their AR page.'}>
|
||||
<Select value={value || NO_PROJECT} onValueChange={(next) => setValue('project', next === NO_PROJECT ? '' : next, { shouldDirty: true })}>
|
||||
<SelectTrigger id='asset-project' className='w-full'>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent position='popper'>
|
||||
<SelectItem value={NO_PROJECT}>Unassigned</SelectItem>
|
||||
{options.map((project) => (
|
||||
<SelectItem key={project.id} value={project.id}>
|
||||
<ClientLogo brand={toBrand(project)} variant='mark' size='sm' />
|
||||
{project.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</Field>
|
||||
);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user