Skip to content
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

feat(create-modal): add value after creation input select value #1209

Merged
merged 7 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const props: ContainerRegistryCreateEditModalProps = {
describe('ContainerRegistryCreateEditModal', () => {
beforeEach(() => {
useCreateContainerRegistryMockSpy.mockReturnValue({
mutateAsync: jest.fn(),
mutateAsync: jest.fn().mockResolvedValue({ id: '000' }),
})
useEditContainerRegistryMockSpy.mockReturnValue({
mutateAsync: jest.fn(),
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('ContainerRegistryCreateEditModal', () => {
},
})

expect(props.onClose).toHaveBeenCalled()
expect(props.onClose).toHaveBeenCalledWith({ id: '000' })
})

it('should submit the form to edit a registry', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useCreateContainerRegistry } from '../hooks/use-create-container-regist
import { useEditContainerRegistry } from '../hooks/use-edit-container-registry/use-edit-container-registry'

export interface ContainerRegistryCreateEditModalProps {
onClose: () => void
onClose: (response?: ContainerRegistryResponse) => void
organizationId: string
isEdit?: boolean
registry?: ContainerRegistryResponse
Expand Down Expand Up @@ -73,18 +73,18 @@ export function ContainerRegistryCreateEditModal({
const onSubmit = methods.handleSubmit(async (containerRegistryRequest) => {
try {
if (registry) {
await editContainerRegistry({
const response = await editContainerRegistry({
organizationId: organizationId,
containerRegistryId: registry.id,
containerRegistryRequest,
})
onClose()
onClose(response)
} else {
await createContainerRegistry({
const response = await createContainerRegistry({
organizationId: organizationId,
containerRegistryRequest,
})
onClose()
onClose(response)
}
} catch (error) {
console.error(error)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type GitAuthProvider, type GitTokenResponse } from 'qovery-typescript-axios'
import { Controller, useFormContext } from 'react-hook-form'
import { Controller, type ControllerRenderProps, type FieldValues, useFormContext } from 'react-hook-form'
import { useParams } from 'react-router-dom'
import { Icon, InputSelect, useModal } from '@qovery/shared/ui'
import { upperCaseFirstLetter } from '@qovery/shared/util-js'
Expand Down Expand Up @@ -49,6 +49,13 @@ export function GitProviderSetting({ disabled }: GitProviderSettingProps) {
]
: mergeProviders(authProviders, gitTokens)

const onChange = (field: ControllerRenderProps<FieldValues, 'provider'>, event: string | string[]) => {
field.onChange(event)
// Reset children fields
setValue('repository', undefined)
setValue('branch', undefined)
}

return (
<Controller
name="provider"
Expand All @@ -60,18 +67,21 @@ export function GitProviderSetting({ disabled }: GitProviderSettingProps) {
<InputSelect
label="Git repository"
options={providerOptions}
onChange={(event) => {
field.onChange(event)
// Reset children fields
setValue('repository', undefined)
setValue('branch', undefined)
}}
onChange={(event) => onChange(field, event)}
menuListButton={{
title: 'Select repository',
label: 'New git access',
onClick: () => {
openModal({
content: <GitTokenCreateEditModal organizationId={organizationId} onClose={closeModal} />,
content: (
<GitTokenCreateEditModal
organizationId={organizationId}
onClose={(response) => {
response && onChange(field, response.id)
closeModal()
}}
/>
),
})
},
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const props: GitTokenCreateEditModalProps = {
describe('GitTokenCreateEditModal', () => {
beforeEach(() => {
useCreateGitTokenMockSpy.mockReturnValue({
mutateAsync: jest.fn(),
mutateAsync: jest.fn().mockResolvedValue({ id: '000' }),
})
useEditGitTokenMockSpy.mockReturnValue({
mutateAsync: jest.fn(),
Expand Down Expand Up @@ -59,7 +59,7 @@ describe('GitTokenCreateEditModal', () => {
},
})

expect(props.onClose).toHaveBeenCalled()
expect(props.onClose).toHaveBeenCalledWith({ id: '000' })
})

it('should submit the form to edit a git token', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useCreateGitToken } from '../hooks/use-create-git-token/use-create-git-
import { useEditGitToken } from '../hooks/use-edit-git-token/use-edit-git-token'

export interface GitTokenCreateEditModalProps {
onClose: () => void
onClose: (response?: GitTokenResponse) => void
organizationId: string
isEdit?: boolean
gitToken?: GitTokenResponse
Expand All @@ -31,18 +31,19 @@ export function GitTokenCreateEditModal({ isEdit, gitToken, organizationId, onCl
const onSubmit = methods.handleSubmit(async (data) => {
try {
if (isEdit) {
await editGitToken({
const response = await editGitToken({
organizationId,
gitTokenId: gitToken?.id ?? '',
gitTokenRequest: data,
})
onClose(response)
} else {
await createGitToken({
const response = await createGitToken({
organizationId,
gitTokenRequest: data,
})
onClose(response)
}
onClose()
} catch (error) {
console.error(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useCreateHelmRepository } from '../hooks/use-create-helm-repository/use
import { useEditHelmRepository } from '../hooks/use-edit-helm-repository/use-edit-helm-repository'

export interface HelmRepositoryCreateEditModalProps {
onClose: () => void
onClose: (helmRepositoryResponse?: HelmRepositoryResponse) => void
organizationId: string
isEdit?: boolean
repository?: HelmRepositoryResponse
Expand Down Expand Up @@ -49,18 +49,19 @@ export function HelmRepositoryCreateEditModal({
const onSubmit = methods.handleSubmit(async (helmRepositoryRequest) => {
try {
if (repository) {
await editHelmRepository({
const response = await editHelmRepository({
organizationId: organizationId,
helmRepositoryId: repository.id,
helmRepositoryRequest,
})
onClose(response)
} else {
await createHelmRepository({
const response = await createHelmRepository({
organizationId: organizationId,
helmRepositoryRequest,
})
onClose(response)
}
onClose()
} catch (error) {
console.error(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ export function SourceSetting({ disabled = false }: { disabled?: boolean }) {
onClick: () => {
openModal({
content: (
<HelmRepositoryCreateEditModal organizationId={organizationId} onClose={closeModal} />
<HelmRepositoryCreateEditModal
organizationId={organizationId}
onClose={(response) => {
response && field.onChange(response.id)
closeModal()
}}
/>
),
})
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ export function ClusterCredentialsSettingsFeature({ cloudProvider }: ClusterCred
cloudProvider,
})

const openCredentialsModal = (id?: string) => {
const openCredentialsModal = (id?: string, onChange?: (e: string | string[]) => void) => {
openModal({
content: (
<CreateEditCredentialsModalFeature
currentCredential={credentials.find((currentCredentials: ClusterCredentials) => currentCredentials.id === id)}
cloudProvider={cloudProvider!}
onClose={closeModal}
onClose={(response) => {
response && onChange?.(response.id)
closeModal()
}}
organizationId={organizationId}
/>
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useModal } from '@qovery/shared/ui'
import CreateEditCredentialsModal from '../../ui/create-edit-credentials-modal/create-edit-credentials-modal'

export interface CreateEditCredentialsModalFeatureProps {
onClose: () => void
onClose: (response?: ClusterCredentials) => void
cloudProvider: CloudProviderEnum
organizationId: string
currentCredential?: ClusterCredentials
Expand Down Expand Up @@ -77,18 +77,18 @@ export function CreateEditCredentialsModalFeature(props: CreateEditCredentialsMo

try {
if (currentCredential) {
await editCloudProviderCredential({
const response = await editCloudProviderCredential({
organizationId,
credentialId: currentCredential.id,
...credentials,
})
onClose()
onClose(response)
} else {
await createCloudProviderCredential({
const response = await createCloudProviderCredential({
organizationId,
...credentials,
})
onClose()
onClose(response)
}
} catch (error) {
console.error(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Icon, InputSelect, LoaderSpinner } from '@qovery/shared/ui'

export interface ClusterCredentialsSettingsProps {
credentials?: ClusterCredentials[]
openCredentialsModal: (id?: string) => void
openCredentialsModal: (id?: string, onChange?: (e: string | string[]) => void) => void
loading: boolean
}

Expand Down Expand Up @@ -45,7 +45,7 @@ export function ClusterCredentialsSettings(props: ClusterCredentialsSettingsProp
title: 'Select credential',
label: 'New credential',
icon: <Icon iconName="circle-plus" className="text-brand-500" />,
onClick: () => openCredentialsModal(),
onClick: () => openCredentialsModal(undefined, field.onChange),
}}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ export function GeneralContainerSettings({ organization, className }: GeneralCon
onClick: () => {
openModal({
content: organization && (
<ContainerRegistryCreateEditModal organizationId={organization.id} onClose={closeModal} />
<ContainerRegistryCreateEditModal
organizationId={organization.id}
onClose={(response) => {
response && field.onChange(response.id)
closeModal()
}}
/>
),
})
},
Expand Down
Loading