Skip to content

Commit ef6d0cd

Browse files
authored
Merge pull request #1173 from topcoder-platform/pm-1365_1
feat(PM-1365): Populate project field in copilot request form when query param presents
2 parents c58c4e9 + 4da8cb0 commit ef6d0cd

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ workflows:
222222
- LVT-256
223223
- CORE-635
224224
- feat/system-admin
225-
- pm-1506_1
225+
- pm-1365_1
226226

227227
- deployQa:
228228
context: org-global

src/apps/copilots/src/pages/copilot-request-form/index.tsx

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { FC, useContext, useEffect, useMemo, useState } from 'react'
22
import { bind, debounce, isEmpty } from 'lodash'
33
import { toast } from 'react-toastify'
4-
import { Params, useNavigate, useParams } from 'react-router-dom'
4+
import { Params, useNavigate, useParams, useSearchParams } from 'react-router-dom'
55
import classNames from 'classnames'
66

77
import { profileContext, ProfileContextData } from '~/libs/core'
88
import { Button, IconSolid, InputDatePicker, InputMultiselectOption,
99
InputRadio, InputSelect, InputSelectReact, InputText, InputTextarea } from '~/libs/ui'
1010
import { InputSkillSelector } from '~/libs/shared'
1111

12-
import { getProjects, ProjectsResponse, useProjects } from '../../services/projects'
12+
import { getProject, getProjects, ProjectsResponse, useProjects } from '../../services/projects'
1313
import { ProjectTypes, ProjectTypeValues } from '../../constants'
1414
import { CopilotRequestResponse, saveCopilotRequest, useCopilotRequest } from '../../services/copilot-requests'
15+
import { Project } from '../../models/Project'
1516

1617
import styles from './styles.module.scss'
1718

@@ -37,11 +38,13 @@ const CopilotRequestForm: FC<{}> = () => {
3738
const { profile }: ProfileContextData = useContext(profileContext)
3839
const navigate = useNavigate()
3940
const routeParams: Params<string> = useParams()
41+
const [params] = useSearchParams()
4042

4143
const [formValues, setFormValues] = useState<any>({})
4244
const [isFormChanged, setIsFormChanged] = useState(false)
4345
const [formErrors, setFormErrors] = useState<any>({})
4446
const [paymentType, setPaymentType] = useState<string>('')
47+
const [projectFromQuery, setProjectFromQuery] = useState<Project>()
4548

4649
const { data: copilotRequestData }: CopilotRequestResponse = useCopilotRequest(routeParams.requestId)
4750

@@ -51,15 +54,42 @@ const CopilotRequestForm: FC<{}> = () => {
5154
}
5255
}, [copilotRequestData])
5356

57+
const fetchProject = async (): Promise<void> => {
58+
const projectId = params.get('projectId')
59+
60+
if (!projectId) {
61+
return
62+
}
63+
64+
const project = await getProject(projectId as string)
65+
66+
setFormValues((prevValues: any) => ({
67+
...prevValues,
68+
projectId: project.id,
69+
}))
70+
setIsFormChanged(true)
71+
setProjectFromQuery(project)
72+
}
73+
74+
useEffect(() => {
75+
fetchProject()
76+
}, [params])
77+
5478
const { data: projects = [] }: ProjectsResponse = useProjects(undefined, {
5579
filter: { id: copilotRequestData?.projectId },
5680
isPaused: () => !copilotRequestData?.projectId,
5781
})
5882

59-
const projectOptions = useMemo(() => projects.map(p => ({
60-
label: p.name,
61-
value: p.id,
62-
})), [projects])
83+
const projectOptions = useMemo(() => {
84+
const projectsFromResponse = projects.map(p => ({
85+
label: p.name,
86+
value: p.id,
87+
}))
88+
89+
return projectFromQuery
90+
? [...projectsFromResponse, { label: projectFromQuery.name, value: projectFromQuery.id }]
91+
: projectsFromResponse
92+
}, [projects, projectFromQuery])
6393

6494
const projectTypes = ProjectTypes ? ProjectTypes.map(project => ({
6595
label: project,

src/apps/copilots/src/services/projects.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ export const useProjects = (search?: string, config?: {isPaused?: () => boolean,
5858
})
5959
}
6060

61+
export const getProject = (projectId: string): Promise<Project> => {
62+
const url = `${baseUrl}/${projectId}`
63+
return xhrGetAsync<Project>(url)
64+
}
65+
6166
export const getProjects = (search?: string, filter?: any): Promise<Project[]> => {
6267
const params = { name: `"${search}"`, ...filter }
6368
const url = buildUrl(baseUrl, params)

0 commit comments

Comments
 (0)