-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jonathan Perchoc <[email protected]>
- Loading branch information
Showing
9 changed files
with
316 additions
and
139 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
packages/manager/apps/pci-ai-notebooks/src/data/api/test/test.api.ts
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,31 @@ | ||
import { apiClient } from '@ovh-ux/manager-core-api'; | ||
import { PCIAi } from '..'; | ||
|
||
export interface TestType { | ||
id?: string; | ||
delay: number; | ||
email: string; | ||
monthlyThreshold: number; | ||
} | ||
|
||
export interface TestPostType extends PCIAi { | ||
test: TestType; | ||
} | ||
|
||
export const getTest = async ({ projectId }: PCIAi) => | ||
apiClient.v6 | ||
.get(`/cloud/project/${projectId}/alerting`, { | ||
headers: { | ||
'X-Pagination-Mode': 'CachedObjectList-Pages', | ||
'X-Pagination-Size': '50000', | ||
}, | ||
}) | ||
.then((res) => res.data as string[]); | ||
|
||
export const postTest = async ({ projectId, test }: TestPostType) => | ||
apiClient.v6 | ||
.post(`/cloud/project/${projectId}/alerting`, test) | ||
.then((res) => res.data as TestType); | ||
|
||
export const deleteTest = async ({ projectId, test }: TestPostType) => | ||
apiClient.v6.delete(`/cloud/project/${projectId}/alerting/${test.id}`); |
85 changes: 85 additions & 0 deletions
85
packages/manager/apps/pci-ai-notebooks/src/hooks/api/test/useTest.hook.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,85 @@ | ||
import { useParams } from 'react-router-dom'; | ||
import { | ||
QueryObserverOptions, | ||
UseQueryResult, | ||
useMutation, | ||
useQueryClient, | ||
} from '@tanstack/react-query'; | ||
|
||
import { | ||
TestType, | ||
deleteTest, | ||
getTest, | ||
postTest, | ||
} from '@/data/api/test/test.api'; | ||
import { AIError } from '@/data/api'; | ||
import { useQueryImmediateRefetch } from '../useImmediateRefetch'; | ||
|
||
export function useGetTest( | ||
projectId: string, | ||
options: Omit<QueryObserverOptions, 'queryKey'> = {}, | ||
) { | ||
const queryKey = [projectId, 'alerting']; | ||
return useQueryImmediateRefetch({ | ||
queryKey, | ||
queryFn: () => getTest({ projectId }), | ||
...options, | ||
}) as UseQueryResult<TestType[], Error>; | ||
} | ||
|
||
export interface PostMutateTestProps { | ||
onError: (cause: AIError) => void; | ||
onSuccess: (test?: TestType) => void; | ||
} | ||
|
||
export function usePostTest({ onError, onSuccess }: PostMutateTestProps) { | ||
const queryClient = useQueryClient(); | ||
const { projectId } = useParams(); | ||
const mutation = useMutation({ | ||
mutationFn: (testInfo: TestType) => { | ||
return postTest({ projectId, test: testInfo }); | ||
}, | ||
onError, | ||
onSuccess: (data) => { | ||
// invalidate notebook list to avoid displaying | ||
// old list | ||
queryClient.invalidateQueries({ | ||
queryKey: [projectId, 'alerting'], | ||
}); | ||
onSuccess(data); | ||
}, | ||
}); | ||
|
||
return { | ||
postTest: (postInfo: TestType) => { | ||
return mutation.mutate(postInfo); | ||
}, | ||
...mutation, | ||
}; | ||
} | ||
|
||
export function useDeleteTest({ onError, onSuccess }: PostMutateTestProps) { | ||
const queryClient = useQueryClient(); | ||
const { projectId } = useParams(); | ||
const mutation = useMutation({ | ||
mutationFn: (testInfo: TestType) => { | ||
return deleteTest({ projectId, test: testInfo }); | ||
}, | ||
onError, | ||
onSuccess: () => { | ||
// invalidate notebook list to avoid displaying | ||
// old list | ||
queryClient.invalidateQueries({ | ||
queryKey: [projectId, 'alerting'], | ||
}); | ||
onSuccess(); | ||
}, | ||
}); | ||
|
||
return { | ||
deleteTest: (postInfo: TestType) => { | ||
return mutation.mutate(postInfo); | ||
}, | ||
...mutation, | ||
}; | ||
} |
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
36 changes: 36 additions & 0 deletions
36
packages/manager/apps/pci-ai-notebooks/src/pages/WithAuth.layout.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,36 @@ | ||
import { Outlet, redirect } from 'react-router-dom'; | ||
import Breadcrumb from '@/components/breadcrumb/Breadcrumb.component'; | ||
import queryClient from '@/query.client'; | ||
import { getTest } from '@/data/api/test/test.api'; | ||
|
||
interface WithAuthLayoutProps { | ||
params: { | ||
projectId: string; | ||
}; | ||
request: Request; | ||
} | ||
// check if the user has authentication | ||
export const Loader = async ({ params }: WithAuthLayoutProps) => { | ||
const { projectId } = params; | ||
try { | ||
const authResult = await queryClient.fetchQuery({ | ||
queryKey: [projectId, 'alerting'], | ||
queryFn: () => getTest({ projectId }), | ||
}); | ||
if (authResult.length === 0) { | ||
return redirect(`/pci/projects/${projectId}/ai/notebooks/auth`); | ||
} | ||
} catch (_error) { | ||
return redirect(`/pci/projects/${projectId}/ai/notebooks/auth`); | ||
} | ||
return null; | ||
}; | ||
|
||
export default function Layout() { | ||
return ( | ||
<> | ||
<Breadcrumb /> | ||
<Outlet /> | ||
</> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,16 +1,24 @@ | ||
import { useTranslation } from 'react-i18next'; | ||
import { useNavigate, useParams } from 'react-router-dom'; | ||
import { AlertCircle, ArrowRight } from 'lucide-react'; | ||
import { redirect, useNavigate, useParams } from 'react-router-dom'; | ||
import { AlertCircle, ArrowRight, X } from 'lucide-react'; | ||
import { Button } from '@/components/ui/button'; | ||
import { useToast } from '@/components/ui/use-toast'; | ||
import { | ||
PostMutateAuthorizationProps, | ||
usePostAuthorization, | ||
} from '@/hooks/api/ai/authorization/usePostAuthorization.hook'; | ||
// import { | ||
// PostMutateAuthorizationProps, | ||
// usePostAuthorization, | ||
// } from '@/hooks/api/ai/authorization/usePostAuthorization.hook'; | ||
import { Alert, AlertDescription } from '@/components/ui/alert'; | ||
import OvhLink from '@/components/links/OvhLink.component'; | ||
import usePciProject from '@/hooks/api/project/usePciProject.hook'; | ||
import { PlanCode } from '@/configuration/project'; | ||
import { | ||
PostMutateTestProps, | ||
useDeleteTest, | ||
useGetTest, | ||
usePostTest, | ||
} from '@/hooks/api/test/useTest.hook'; | ||
import { TestType } from '@/data/api/test/test.api'; | ||
import Link from '@/components/links/Link.component'; | ||
|
||
export default function Auth() { | ||
const { t } = useTranslation('pci-ai-notebooks/auth'); | ||
|
@@ -19,10 +27,13 @@ export default function Auth() { | |
const { projectId } = useParams(); | ||
const projectData = usePciProject(); | ||
|
||
const tests = useGetTest(projectId); | ||
|
||
const isProjectDiscoveryMode = | ||
projectData.data?.planCode === PlanCode.DISCOVERY; | ||
|
||
const PostAuthorizationProps: PostMutateAuthorizationProps = { | ||
// const PostAuthorizationProps: PostMutateAuthorizationProps = { | ||
const PostTestProps: PostMutateTestProps = { | ||
onError(err) { | ||
toast.toast({ | ||
title: t(`formActiveUserToastErrorTitle`), | ||
|
@@ -39,12 +50,21 @@ export default function Auth() { | |
}, | ||
}; | ||
|
||
const { postAuthorization } = usePostAuthorization(PostAuthorizationProps); | ||
// const { postAuthorization } = usePostAuthorization(PostAuthorizationProps); | ||
const { postTest } = usePostTest(PostTestProps); | ||
const { deleteTest } = useDeleteTest(PostTestProps); | ||
|
||
const activateProject = () => { | ||
postAuthorization({ | ||
projectId, | ||
}); | ||
// postAuthorization({ | ||
// projectId, | ||
// }); | ||
|
||
const test: TestType = { | ||
delay: 3600, | ||
email: '[email protected]', | ||
monthlyThreshold: 1, | ||
}; | ||
postTest(test); | ||
}; | ||
|
||
return ( | ||
|
@@ -92,6 +112,21 @@ export default function Auth() { | |
> | ||
{t('authActivateProjectButton')} | ||
</Button> | ||
|
||
<ul> | ||
{tests.isSuccess && | ||
tests.data.map((test) => ( | ||
<li key={test.id}> | ||
{test.id}{' '} | ||
<Button variant="ghost" onClick={() => deleteTest(test)}> | ||
<X /> | ||
</Button> | ||
</li> | ||
))} | ||
</ul> | ||
{tests.isSuccess && tests.data.length > 0 && ( | ||
<Link to={'../'}>Go to notebooks</Link> | ||
)} | ||
</div> | ||
</> | ||
); | ||
|
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.