From c3d294fadce5bd824b3eb8b5a211f25199d1d771 Mon Sep 17 00:00:00 2001 From: psiddharthdesign <107192927+psiddharthdesign@users.noreply.github.com> Date: Fri, 19 Jul 2024 16:25:32 +0530 Subject: [PATCH] feat/minor fix to create project form --- .../projects/create/page.tsx | 5 +- src/components/CreateProjectForm.tsx | 84 +++++++++++-------- src/data/user/repos.ts | 14 ++++ 3 files changed, 65 insertions(+), 38 deletions(-) diff --git a/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/projects/create/page.tsx b/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/projects/create/page.tsx index 4c2f241b..337634d6 100644 --- a/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/projects/create/page.tsx +++ b/src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/org/[organizationId]/(specific-organization-pages)/projects/create/page.tsx @@ -1,5 +1,5 @@ - import CreateProjectForm from "@/components/CreateProjectForm"; +import { getOrganizationRepos } from "@/data/user/repos"; import { organizationParamSchema } from "@/utils/zod-schemas/params"; import type { Metadata } from "next"; @@ -14,10 +14,11 @@ export default async function CreateProjectPage({ params: unknown; }) { const { organizationId } = organizationParamSchema.parse(params); + const repositories = await getOrganizationRepos(organizationId); return (
- +
); } \ No newline at end of file diff --git a/src/components/CreateProjectForm.tsx b/src/components/CreateProjectForm.tsx index 02ad532a..2974fa08 100644 --- a/src/components/CreateProjectForm.tsx +++ b/src/components/CreateProjectForm.tsx @@ -32,23 +32,25 @@ const createProjectFormSchema = z.object({ type CreateProjectFormData = z.infer; -const repositories = [ - { id: 12, name: 'Repository 1' }, - { id: 123, name: 'Repository 2' }, - { id: 41, name: 'Repository 3' }, - { id: 24, name: 'Repository 4' }, - { id: 124, name: 'Repository 5' }, -]; +type Repository = { + id: number; + name: string; +}; -export default function CreateProjectForm({ organizationId }: { organizationId: string }) { - const [selectedRepo, setSelectedRepo] = useState(repositories[0].id); +type CreateProjectFormProps = { + organizationId: string; + repositories: Repository[]; +}; + +export default function CreateProjectForm({ organizationId, repositories }: CreateProjectFormProps) { + const [selectedRepo, setSelectedRepo] = useState(repositories[0]?.id || null); const router = useRouter(); const { control, handleSubmit, setValue, watch } = useForm({ resolver: zodResolver(createProjectFormSchema), defaultValues: { name: "", - repository: repositories[0].id, + repository: repositories[0]?.id || 0, terraformDir: "", managedState: true, labels: [], @@ -84,8 +86,6 @@ export default function CreateProjectForm({ organizationId }: { organizationId: createProjectMutation.mutate(data); }; - - return (
{ @@ -159,31 +159,43 @@ export default function CreateProjectForm({ organizationId }: { organizationId: - -
- {repositories.map((repo, index) => ( - { - setSelectedRepo(repo.id); - setValue("repository", repo.id); - }} - initial={{ opacity: 0, y: 20 }} - animate={{ opacity: 1, y: 0 }} - transition={{ delay: index * 0.1 }} - > - - - {repo.name} - - - ))} + {repositories.length > 0 ? ( + +
+ {repositories.map((repo, index) => ( + { + setSelectedRepo(repo.id); + setValue("repository", repo.id); + }} + initial={{ opacity: 0, y: 20 }} + animate={{ opacity: 1, y: 0 }} + transition={{ delay: index * 0.1 }} + > + + + {repo.name} + + + ))} +
+ +
+ ) : ( +
+
+ +
+ No Repositories Found + + It looks like there are no repositories. +
- - + )} diff --git a/src/data/user/repos.ts b/src/data/user/repos.ts index 6e4ebcb9..45e98862 100644 --- a/src/data/user/repos.ts +++ b/src/data/user/repos.ts @@ -16,3 +16,17 @@ export async function getRepoDetails(repoId: number) { return data; } + +export async function getOrganizationRepos(organizationId: string) { + const supabaseClient = createSupabaseUserServerComponentClient(); + const { data, error } = await supabaseClient + .from('repos') + .select('id, name') + .eq('organization_id', organizationId); + + if (error) { + throw error; + } + + return data; +}