2
2
3
3
import { GithubRepoDto } from "@/app/api/github/repo/types/githubRepo.dto" ;
4
4
import BreadCrumb from "@/components/breadcrumbs" ;
5
- import { GithubRepoCard } from "@/components/githubRepoCard " ;
6
- import ProjectGithubRepositoriesBadge from "@/components/repo/projectGithubRepositoriesBadge " ;
7
- import { ProjectRepoSearchInput } from "@/components/repo/projectRepoSearchInput " ;
5
+ import ProjectTeamNameForm from "@/components/projects/projectRepoNameForm " ;
6
+ import ProjectRepoSelect from "@/components/projects/projectRepoSelect " ;
7
+ import { TeamCalculationCreated } from "@/components/teams/teamCalculationCreated " ;
8
8
import { Button } from "@/components/ui/button" ;
9
9
import { Heading } from "@/components/ui/heading" ;
10
- import { Skeleton } from "@/components/ui/skeleton" ;
11
10
import { Separator } from "@radix-ui/react-separator" ;
12
11
import axios from "axios" ;
13
12
import { useEffect , useState } from "react" ;
13
+ import { toast } from "sonner" ;
14
14
15
15
const breadcrumbItems = [
16
16
{ title : "Projects" , link : "/projects" } ,
17
17
{ title : "New" , link : "/projects/new" } ,
18
18
] ;
19
19
20
20
export default function CreateNewProjectPage ( ) {
21
- const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ;
22
- const [ githubRepos , setGithubRepos ] = useState < GithubRepoDto [ ] > ( [ ] ) ;
21
+ const [ createTeamName , setCreateTeamName ] = useState < string > ( "" ) ;
23
22
const [ selectedGithubRepos , setSelectedGithubRepos ] = useState <
24
23
GithubRepoDto [ ]
25
24
> ( [ ] ) ;
26
- const [ page , setPage ] = useState < number > ( 1 ) ;
27
- const [ canPrevious , setCanPrevious ] = useState < boolean > ( false ) ;
28
- const [ canNext , setCanNext ] = useState < boolean > ( false ) ;
29
- const [ registeredRepositoryNameArray , setRegisteredRepositoryNameArray ] =
30
- useState < string [ ] > ( [ ] ) ;
25
+ const [ currentStep , setCurrentStep ] = useState ( 0 ) ;
31
26
32
27
const handleOnRepoSelect = async (
33
28
repo : GithubRepoDto ,
@@ -52,27 +47,48 @@ export default function CreateNewProjectPage() {
52
47
return repos . filter ( ( repo ) => repo . id !== repoIdToRemove ) ;
53
48
} ;
54
49
55
- const handleQueryGithubRepos = async ( page : number = 1 ) => {
56
- setIsLoading ( true ) ;
57
- const { data } = await axios . get ( `/api/github/repo?page=${ page } ` ) ;
58
- if ( data . success && data . gitRepos . length > 0 ) {
59
- setGithubRepos ( data . gitRepos ) ;
50
+ const handleCreateTeam = async ( ) => {
51
+ nextStep ( ) ;
52
+ const { data } = await axios . post ( "/api/teams" , { name : createTeamName } ) ;
53
+ if ( data . success ) {
54
+ await handleRegisterRepos ( data . createdTeam . id ) ;
55
+ handleGenerateReport ( data . createdTeam . id ) ;
56
+ } else {
57
+ previousStep ( ) ;
60
58
}
61
- setIsLoading ( false ) ;
62
59
} ;
63
60
64
- useEffect ( ( ) => {
65
- setCanNext ( githubRepos . length === 10 ) ;
66
- setCanPrevious ( page > 1 ) ;
67
- } , [ githubRepos , page ] ) ;
61
+ const handleRegisterRepos = async ( projectId : string ) => {
62
+ await axios . post ( `/api/projects/repo` , {
63
+ projectId : projectId ,
64
+ repos : selectedGithubRepos . map ( ( repo ) => ( {
65
+ name : repo . name ,
66
+ full_name : repo . full_name ,
67
+ } ) ) ,
68
+ } ) ;
69
+ } ;
68
70
69
- useEffect ( ( ) => {
70
- handleQueryGithubRepos ( ) ;
71
- } , [ ] ) ;
71
+ const handleGenerateReport = async ( projectId : string ) => {
72
+ const { data } = await axios . get (
73
+ `/api/github/repo/registered?team_id=${ projectId } ` ,
74
+ ) ;
75
+ if ( data . success && data . registeredRepos . length == 0 ) {
76
+ toast ( "Failed to start calculation" , {
77
+ description : "Please select at least one repository to continue" ,
78
+ } ) ;
79
+ previousStep ( ) ;
80
+ } else {
81
+ const { data } = await axios . get ( `/api/credmanager?team_id=${ projectId } ` ) ;
82
+ }
83
+ } ;
72
84
73
- useEffect ( ( ) => {
74
- handleQueryGithubRepos ( page ) ;
75
- } , [ page ] ) ;
85
+ const nextStep = ( ) => {
86
+ setCurrentStep ( currentStep + 1 ) ;
87
+ } ;
88
+
89
+ const previousStep = ( ) => {
90
+ setCurrentStep ( currentStep - 1 ) ;
91
+ } ;
76
92
77
93
useEffect ( ( ) => {
78
94
console . log ( selectedGithubRepos ) ;
@@ -82,81 +98,48 @@ export default function CreateNewProjectPage() {
82
98
< div className = "flex-1 space-y-4 p-4 md:p-8 pt-6" >
83
99
< BreadCrumb items = { breadcrumbItems } />
84
100
< div className = "flex items-start justify-between" >
85
- < Heading title = { `Add repositories` } description = "" />
86
- < div className = "flex items-start justify-between w-4/12" >
87
- < ProjectRepoSearchInput
88
- onSelectRepo = { handleOnRepoSelect }
89
- > </ ProjectRepoSearchInput >
101
+ { currentStep === 0 ? (
102
+ < Heading title = { `Add repositories` } description = "" />
103
+ ) : currentStep === 1 ? (
104
+ < Heading title = { `Enter project name` } description = "" />
105
+ ) : (
106
+ < Heading title = { `Calculating` } description = "" />
107
+ ) }
108
+ < div className = "flex items-start" >
109
+ { currentStep == 1 ? (
110
+ < Button className = "items-center ml-4" onClick = { previousStep } >
111
+ Previous step
112
+ </ Button >
113
+ ) : currentStep === 0 ? (
114
+ < Button
115
+ className = "items-center ml-4"
116
+ onClick = { nextStep }
117
+ disabled = { selectedGithubRepos . length == 0 }
118
+ >
119
+ Next Step
120
+ </ Button >
121
+ ) : (
122
+ < > </ >
123
+ ) }
90
124
</ div >
91
125
</ div >
92
- < div className = "flex items-center py-2 mb-2" >
93
- { selectedGithubRepos . map ( ( registeredGitRepo ) => (
94
- < ProjectGithubRepositoriesBadge
95
- githubRepoDto = { registeredGitRepo }
96
- handleUnregisterRepo = { handleOnRepoSelect }
97
- > </ ProjectGithubRepositoriesBadge >
98
- ) ) }
99
- </ div >
100
126
< Separator />
101
127
102
- { ! isLoading ? (
103
- < div >
104
- < div className = "grid gap-4 lg:grid-cols-2" >
105
- { githubRepos
106
- . filter (
107
- ( repo ) =>
108
- ! registeredRepositoryNameArray . includes ( repo . full_name ) ,
109
- )
110
- . map ( ( repo ) => {
111
- return (
112
- < GithubRepoCard
113
- githubRepoDto = { repo }
114
- onSelectRepo = { handleOnRepoSelect }
115
- selected = { selectedGithubRepos . includes ( repo ) }
116
- > </ GithubRepoCard >
117
- ) ;
118
- } ) }
119
- </ div >
120
- < div className = "flex justify-between pt-16" >
121
- < div >
122
- < Button
123
- variant = "default"
124
- onClick = { ( ) => {
125
- setPage ( page - 1 ) ;
126
- } }
127
- disabled = { ! canPrevious }
128
- >
129
- Previous Page
130
- </ Button >
131
- </ div >
132
- < div className = "pl-6" >
133
- < Button
134
- variant = "default"
135
- onClick = { ( ) => {
136
- setPage ( page + 1 ) ;
137
- } }
138
- disabled = { ! canNext }
139
- >
140
- Next Page
141
- </ Button >
142
- </ div >
143
- </ div >
144
- </ div >
128
+ { currentStep === 0 ? (
129
+ < ProjectRepoSelect
130
+ onSelectRepo = { handleOnRepoSelect }
131
+ selectedGithubRepos = { selectedGithubRepos }
132
+ > </ ProjectRepoSelect >
133
+ ) : currentStep === 1 ? (
134
+ < ProjectTeamNameForm
135
+ createTeamName = { createTeamName }
136
+ handleCreateTeam = { handleCreateTeam }
137
+ setCreateTeamName = { setCreateTeamName }
138
+ selectedGithubRepos = { selectedGithubRepos }
139
+ onSelectRepo = { handleOnRepoSelect }
140
+ > </ ProjectTeamNameForm >
145
141
) : (
146
- < div className = "grid gap-4 lg:grid-cols-2" >
147
- < div className = "flex flex-col space-y-3" >
148
- < Skeleton className = "h-[165px] w-full rounded-xl" />
149
- </ div >
150
- < div className = "flex flex-col space-y-3" >
151
- < Skeleton className = "h-[165px] w-full rounded-xl" />
152
- </ div >
153
- < div className = "flex flex-col space-y-3" >
154
- < Skeleton className = "h-[165px] w-full rounded-xl" />
155
- </ div >
156
- < div className = "flex flex-col space-y-3" >
157
- < Skeleton className = "h-[165px] w-full rounded-xl" />
158
- </ div >
159
- </ div >
142
+ < TeamCalculationCreated > </ TeamCalculationCreated >
160
143
) }
161
144
</ div >
162
145
) ;
0 commit comments