-
Notifications
You must be signed in to change notification settings - Fork 16
feat(PM-1365): Populate project field in copilot request form when query param presents #1173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
import { FC, useContext, useEffect, useMemo, useState } from 'react' | ||
import { bind, debounce, isEmpty } from 'lodash' | ||
import { toast } from 'react-toastify' | ||
import { Params, useNavigate, useParams } from 'react-router-dom' | ||
import { Params, useNavigate, useParams, useSearchParams } from 'react-router-dom' | ||
import classNames from 'classnames' | ||
|
||
import { profileContext, ProfileContextData } from '~/libs/core' | ||
import { Button, IconSolid, InputDatePicker, InputMultiselectOption, | ||
InputRadio, InputSelect, InputSelectReact, InputText, InputTextarea } from '~/libs/ui' | ||
import { InputSkillSelector } from '~/libs/shared' | ||
|
||
import { getProjects, ProjectsResponse, useProjects } from '../../services/projects' | ||
import { getProject, getProjects, ProjectsResponse, useProjects } from '../../services/projects' | ||
import { ProjectTypes, ProjectTypeValues } from '../../constants' | ||
import { CopilotRequestResponse, saveCopilotRequest, useCopilotRequest } from '../../services/copilot-requests' | ||
import { Project } from '../../models/Project' | ||
|
||
import styles from './styles.module.scss' | ||
|
||
|
@@ -37,11 +38,13 @@ const CopilotRequestForm: FC<{}> = () => { | |
const { profile }: ProfileContextData = useContext(profileContext) | ||
const navigate = useNavigate() | ||
const routeParams: Params<string> = useParams() | ||
const [params] = useSearchParams() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using a more descriptive name for the |
||
|
||
const [formValues, setFormValues] = useState<any>({}) | ||
const [isFormChanged, setIsFormChanged] = useState(false) | ||
const [formErrors, setFormErrors] = useState<any>({}) | ||
const [paymentType, setPaymentType] = useState<string>('') | ||
const [projectFromQuery, setProjectFromQuery] = useState<Project>() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
const { data: copilotRequestData }: CopilotRequestResponse = useCopilotRequest(routeParams.requestId) | ||
|
||
|
@@ -51,15 +54,42 @@ const CopilotRequestForm: FC<{}> = () => { | |
} | ||
}, [copilotRequestData]) | ||
|
||
const fetchProject = async (): Promise<void> => { | ||
const projectId = params.get('projectId') | ||
|
||
if (!projectId) { | ||
return | ||
} | ||
|
||
const project = await getProject(projectId as string) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding error handling for the |
||
|
||
setFormValues((prevValues: any) => ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid using |
||
...prevValues, | ||
projectId: project.id, | ||
})) | ||
setIsFormChanged(true) | ||
setProjectFromQuery(project) | ||
} | ||
|
||
useEffect(() => { | ||
fetchProject() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that |
||
}, [params]) | ||
|
||
const { data: projects = [] }: ProjectsResponse = useProjects(undefined, { | ||
filter: { id: copilotRequestData?.projectId }, | ||
isPaused: () => !copilotRequestData?.projectId, | ||
}) | ||
|
||
const projectOptions = useMemo(() => projects.map(p => ({ | ||
label: p.name, | ||
value: p.id, | ||
})), [projects]) | ||
const projectOptions = useMemo(() => { | ||
const projectsFromResponse = projects.map(p => ({ | ||
label: p.name, | ||
value: p.id, | ||
})) | ||
|
||
return projectFromQuery | ||
? [...projectsFromResponse, { label: projectFromQuery.name, value: projectFromQuery.id }] | ||
: projectsFromResponse | ||
}, [projects, projectFromQuery]) | ||
|
||
const projectTypes = ProjectTypes ? ProjectTypes.map(project => ({ | ||
label: project, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,11 @@ export const useProjects = (search?: string, config?: {isPaused?: () => boolean, | |
}) | ||
} | ||
|
||
export const getProject = (projectId: string): Promise<Project> => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding error handling to the |
||
const url = `${baseUrl}/${projectId}` | ||
return xhrGetAsync<Project>(url) | ||
} | ||
|
||
export const getProjects = (search?: string, filter?: any): Promise<Project[]> => { | ||
const params = { name: `"${search}"`, ...filter } | ||
const url = buildUrl(baseUrl, params) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
useSearchParams
hook is imported but not used in the code. Please ensure that it is utilized if needed, or remove the import to keep the code clean.