feat: add projects page
This commit is contained in:
parent
e8bc35d443
commit
6ce98e6dae
75
app/routes/admin/projects/index.tsx
Normal file
75
app/routes/admin/projects/index.tsx
Normal file
@ -0,0 +1,75 @@
|
||||
import { useSearchParams } from 'react-router';
|
||||
import type { Route } from './+types/index';
|
||||
import { AddButton } from '~/components/ui/add-button';
|
||||
import { EmptyState } from '~/components/ui/empty-state';
|
||||
import { ProjectCard } from '~/components/projects/list/project-card';
|
||||
import { ProjectsHeader } from '~/components/projects/list/projects-header';
|
||||
import { UnassignedBanner } from '~/components/projects/list/unassigned-banner';
|
||||
import { PROJECT_INTENT, type ProjectIntent } from '~/data/projects/constants';
|
||||
import { useAdminData } from '~/hooks/admin/use-admin-data';
|
||||
import { useAdminDialogs } from '~/hooks/admin/use-admin-dialogs';
|
||||
import { getErrorMessage } from '~/lib/errors';
|
||||
import { fromProjectFormData } from '~/lib/projects/form-data';
|
||||
import { withLogoRule } from '~/schemas/projects/project-form';
|
||||
import { createProject, updateProject } from '~/services/projects';
|
||||
import { getSettings } from '~/services/settings';
|
||||
import type { ActionResult } from '~/types/action';
|
||||
import type { ProjectStatus } from '~/types/project';
|
||||
|
||||
export async function clientAction({ request }: Route.ClientActionArgs): Promise<ActionResult<ProjectIntent>> {
|
||||
const formData = await request.formData();
|
||||
const intent = formData.get('intent');
|
||||
const id = String(formData.get('id') ?? '');
|
||||
try {
|
||||
const settings = await getSettings();
|
||||
const parsed = withLogoRule(settings.logo_required).safeParse(fromProjectFormData(formData));
|
||||
if (!parsed.success) return { ok: false, error: parsed.error.issues[0]?.message ?? 'Check the form and try again' };
|
||||
if (intent === PROJECT_INTENT.create) {
|
||||
const project = await createProject(parsed.data);
|
||||
return { ok: true, intent, message: 'Project created', id: project.id };
|
||||
}
|
||||
if (intent === PROJECT_INTENT.update && id) {
|
||||
await updateProject(id, parsed.data);
|
||||
return { ok: true, intent, message: 'Project saved' };
|
||||
}
|
||||
return { ok: false, error: 'Unknown action' };
|
||||
} catch (error) {
|
||||
return { ok: false, error: getErrorMessage(error) };
|
||||
}
|
||||
}
|
||||
|
||||
export default function ProjectsPage() {
|
||||
const { projects, counts } = useAdminData();
|
||||
const { openProject } = useAdminDialogs();
|
||||
const [params, setParams] = useSearchParams();
|
||||
const status: ProjectStatus = params.get('status') === 'archived' ? 'archived' : 'active';
|
||||
const activeCount = projects.filter((project) => project.status === 'active').length;
|
||||
const shown = projects.filter((project) => project.status === status);
|
||||
|
||||
return (
|
||||
<div className='flex flex-col gap-6'>
|
||||
<ProjectsHeader
|
||||
description={`${activeCount} active ${activeCount === 1 ? 'client' : 'clients'} · ${counts.total} assets`}
|
||||
status={status}
|
||||
onStatusChange={(next) => setParams(next === 'archived' ? { status: next } : {})}
|
||||
onCreate={() => openProject()}
|
||||
/>
|
||||
{status === 'active' && counts.unassigned > 0 && <UnassignedBanner count={counts.unassigned} />}
|
||||
{shown.length > 0 ? (
|
||||
<div className='grid grid-cols-[repeat(auto-fill,minmax(260px,1fr))] gap-4'>
|
||||
{shown.map((project) => (
|
||||
<ProjectCard key={project.id} project={project} onEdit={openProject} />
|
||||
))}
|
||||
</div>
|
||||
) : status === 'active' ? (
|
||||
<EmptyState
|
||||
title='No projects yet'
|
||||
message='Create a project for each client company, then add its AR assets.'
|
||||
action={<AddButton label='New project' onClick={() => openProject()} />}
|
||||
/>
|
||||
) : (
|
||||
<EmptyState title='No archived projects' />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user