Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(project): modal to create project (#426)
- Loading branch information
1 parent
861c047
commit 5286300
Showing
23 changed files
with
323 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useEffect } from 'react' | ||
import { useLocation } from 'react-router-dom' | ||
|
||
export default function ScrollToTop() { | ||
const { pathname } = useLocation() | ||
|
||
useEffect(() => { | ||
window.scrollTo(0, 0) | ||
}, [pathname]) | ||
|
||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...console-shared/src/lib/create-project-modal/feature/create-project-modal-feature.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { act, fireEvent, render } from '__tests__/utils/setup-jest' | ||
import * as storeProjects from '@qovery/domains/projects' | ||
import CreateProjectModalFeature, { CreateProjectModalFeatureProps } from './create-project-modal-feature' | ||
|
||
import SpyInstance = jest.SpyInstance | ||
|
||
const mockedUsedNavigate = jest.fn() | ||
jest.mock('react-router-dom', () => ({ | ||
...(jest.requireActual('react-router-dom') as any), | ||
useNavigate: () => mockedUsedNavigate, | ||
})) | ||
|
||
jest.mock('@qovery/domains/projects', () => ({ | ||
...jest.requireActual('@qovery/domains/projects'), | ||
postProject: jest.fn().mockImplementation(() => Promise.resolve()), | ||
})) | ||
|
||
const mockDispatch = jest.fn() | ||
jest.mock('react-redux', () => ({ | ||
...jest.requireActual('react-redux'), | ||
useDispatch: () => mockDispatch, | ||
})) | ||
|
||
describe('CreateProjectModalFeature', () => { | ||
const props: CreateProjectModalFeatureProps = { | ||
onClose: jest.fn(), | ||
organizationId: '0', | ||
goToEnvironment: false, | ||
} | ||
|
||
it('should render successfully', () => { | ||
const { baseElement } = render(<CreateProjectModalFeature {...props} />) | ||
expect(baseElement).toBeTruthy() | ||
}) | ||
|
||
it('should dispatch postProject if form is submitted', async () => { | ||
const postProjectSpy: SpyInstance = jest.spyOn(storeProjects, 'postProject') | ||
mockDispatch.mockImplementation(() => ({ | ||
unwrap: () => | ||
Promise.resolve({ | ||
data: {}, | ||
}), | ||
})) | ||
|
||
const { getByTestId } = render(<CreateProjectModalFeature {...props} />) | ||
|
||
await act(() => { | ||
const inputName = getByTestId('input-name') | ||
fireEvent.input(inputName, { target: { value: 'hello-world' } }) | ||
}) | ||
|
||
await act(() => { | ||
const inputName = getByTestId('input-description') | ||
fireEvent.input(inputName, { target: { value: 'description' } }) | ||
}) | ||
|
||
expect(getByTestId('submit-button')).not.toBeDisabled() | ||
|
||
await act(() => { | ||
getByTestId('submit-button').click() | ||
}) | ||
|
||
expect(postProjectSpy).toHaveBeenCalledWith({ | ||
name: 'hello-world', | ||
description: 'description', | ||
organizationId: '0', | ||
}) | ||
}) | ||
}) |
51 changes: 51 additions & 0 deletions
51
...ared/console-shared/src/lib/create-project-modal/feature/create-project-modal-feature.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useState } from 'react' | ||
import { FieldValues, FormProvider, useForm } from 'react-hook-form' | ||
import { useDispatch } from 'react-redux' | ||
import { useNavigate } from 'react-router-dom' | ||
import { postProject } from '@qovery/domains/projects' | ||
import { ENVIRONMENTS_GENERAL_URL, ENVIRONMENTS_URL } from '@qovery/shared/router' | ||
import { AppDispatch } from '@qovery/store' | ||
import CreateProjectModal from '../ui/create-project-modal' | ||
|
||
export interface CreateProjectModalFeatureProps { | ||
onClose: () => void | ||
organizationId: string | ||
} | ||
|
||
export function CreateProjectModalFeature(props: CreateProjectModalFeatureProps) { | ||
const { onClose, organizationId } = props | ||
|
||
const navigate = useNavigate() | ||
const dispatch = useDispatch<AppDispatch>() | ||
const methods = useForm({ | ||
mode: 'onChange', | ||
}) | ||
const [loading, setLoading] = useState(false) | ||
|
||
const onSubmit = methods.handleSubmit((data: FieldValues) => { | ||
setLoading(true) | ||
|
||
dispatch( | ||
postProject({ | ||
organizationId: organizationId, | ||
name: data['name'], | ||
description: data['description'], | ||
}) | ||
) | ||
.unwrap() | ||
.then((project) => { | ||
navigate(ENVIRONMENTS_URL(organizationId, project.id) + ENVIRONMENTS_GENERAL_URL) | ||
setLoading(false) | ||
onClose() | ||
}) | ||
.catch(() => setLoading(false)) | ||
}) | ||
|
||
return ( | ||
<FormProvider {...methods}> | ||
<CreateProjectModal closeModal={onClose} onSubmit={onSubmit} loading={loading} /> | ||
</FormProvider> | ||
) | ||
} | ||
|
||
export default CreateProjectModalFeature |
40 changes: 40 additions & 0 deletions
40
libs/shared/console-shared/src/lib/create-project-modal/ui/create-project-modal.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { act, fireEvent, render, waitFor } from '__tests__/utils/setup-jest' | ||
import { wrapWithReactHookForm } from '__tests__/utils/wrap-with-react-hook-form' | ||
import CreateProjectModal, { CreateProjectModalProps } from './create-project-modal' | ||
|
||
describe('CreateProjectModal', () => { | ||
const props: CreateProjectModalProps = { | ||
onSubmit: jest.fn(), | ||
closeModal: jest.fn(), | ||
loading: false, | ||
} | ||
|
||
it('should render successfully', () => { | ||
const { baseElement } = render(wrapWithReactHookForm(<CreateProjectModal {...props} />)) | ||
expect(baseElement).toBeTruthy() | ||
}) | ||
|
||
it('should submit the form', async () => { | ||
const spy = jest.fn().mockImplementation((e) => e.preventDefault()) | ||
props.onSubmit = spy | ||
|
||
const { getByTestId } = render(wrapWithReactHookForm(<CreateProjectModal {...props} />)) | ||
|
||
const inputName = getByTestId('input-name') | ||
const inputDescription = getByTestId('input-description') | ||
const button = getByTestId('submit-button') | ||
|
||
expect(button).toBeDisabled() | ||
|
||
await act(() => { | ||
fireEvent.input(inputName, { target: { value: 'hello world' } }) | ||
fireEvent.input(inputDescription, { target: { value: 'hello' } }) | ||
}) | ||
|
||
await waitFor(() => { | ||
button.click() | ||
expect(button).not.toBeDisabled() | ||
expect(spy).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
59 changes: 59 additions & 0 deletions
59
libs/shared/console-shared/src/lib/create-project-modal/ui/create-project-modal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Controller, useFormContext } from 'react-hook-form' | ||
import { InputText, ModalCrud } from '@qovery/shared/ui' | ||
|
||
export interface CreateProjectModalProps { | ||
onSubmit: () => void | ||
closeModal: () => void | ||
loading: boolean | ||
} | ||
|
||
export function CreateProjectModal(props: CreateProjectModalProps) { | ||
const { onSubmit, closeModal, loading } = props | ||
const { control } = useFormContext() | ||
|
||
return ( | ||
<ModalCrud | ||
title="New project" | ||
description="You will have the possibility to modify the parameters once created" | ||
onClose={closeModal} | ||
onSubmit={onSubmit} | ||
submitLabel="Create" | ||
loading={loading} | ||
> | ||
<Controller | ||
name="name" | ||
control={control} | ||
rules={{ | ||
required: 'Please enter a name.', | ||
}} | ||
render={({ field, fieldState: { error } }) => ( | ||
<InputText | ||
className="mb-3" | ||
dataTestId="input-name" | ||
label="Project name" | ||
name={field.name} | ||
onChange={field.onChange} | ||
value={field.value} | ||
error={error?.message} | ||
/> | ||
)} | ||
/> | ||
<Controller | ||
name="description" | ||
control={control} | ||
render={({ field }) => ( | ||
<InputText | ||
className="mb-6" | ||
dataTestId="input-description" | ||
label="Description" | ||
name={field.name} | ||
onChange={field.onChange} | ||
value={field.value} | ||
/> | ||
)} | ||
/> | ||
</ModalCrud> | ||
) | ||
} | ||
|
||
export default CreateProjectModal |
3 changes: 1 addition & 2 deletions
3
...shared/console-shared/src/lib/git-repository-settings/ui/git-repository-settings.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.