diff --git a/app/hooks/projects/index.ts b/app/hooks/projects/index.ts
index 34d0a7f9e8..cc013a6498 100644
--- a/app/hooks/projects/index.ts
+++ b/app/hooks/projects/index.ts
@@ -24,6 +24,8 @@ import {
UseUploadProjectPAProps,
UploadProjectPAProps,
UsePublishedProjectsProps,
+ UseDuplicateProjectProps,
+ DuplicateProjectProps,
} from './types';
export function useProjects(options: UseProjectsOptionsProps): UseProjectsResponse {
@@ -365,3 +367,36 @@ export function usePublishedProject(id) {
};
}, [query, data?.data]);
}
+
+export function useDuplicateProject({
+ requestConfig = {
+ method: 'POST',
+ },
+}: UseDuplicateProjectProps) {
+ const queryClient = useQueryClient();
+ const [session] = useSession();
+
+ const duplicateProject = ({ id }: DuplicateProjectProps) => {
+ return PROJECTS.request({
+ // Pending endpoint
+ url: `/${id}`,
+ headers: {
+ Authorization: `Bearer ${session.accessToken}`,
+ },
+ ...requestConfig,
+ });
+ };
+
+ return useMutation(duplicateProject, {
+ onSuccess: (data: any, variables, context) => {
+ const { id } = data;
+ queryClient.invalidateQueries('projects');
+ queryClient.invalidateQueries(['projects', id]);
+ console.info('Succces', data, variables, context);
+ },
+ onError: (error, variables, context) => {
+ // An error happened!
+ console.info('Error', error, variables, context);
+ },
+ });
+}
diff --git a/app/hooks/projects/types.ts b/app/hooks/projects/types.ts
index 97d630a6f2..6c8979eeee 100644
--- a/app/hooks/projects/types.ts
+++ b/app/hooks/projects/types.ts
@@ -44,3 +44,12 @@ export interface UsePublishedProjectsProps {
sort?: string,
filters?: Record
}
+
+// useDuplicateProject
+export interface UseDuplicateProjectProps {
+ requestConfig?: AxiosRequestConfig;
+}
+
+export interface DuplicateProjectProps {
+ id: string | string[];
+}
diff --git a/app/hooks/scenarios/index.ts b/app/hooks/scenarios/index.ts
index 514e90d02f..2275a8eb5d 100644
--- a/app/hooks/scenarios/index.ts
+++ b/app/hooks/scenarios/index.ts
@@ -29,6 +29,8 @@ import {
UploadScenarioPUProps,
UseSaveScenarioPUProps,
SaveScenarioPUProps,
+ UseDuplicateScenarioProps,
+ DuplicateScenarioProps,
} from './types';
export function useScenarios(pId, options: UseScenariosOptionsProps = {}) {
@@ -355,3 +357,36 @@ export function useSaveScenarioPU({
},
});
}
+
+export function useDuplicateScenario({
+ requestConfig = {
+ method: 'POST',
+ },
+}: UseDuplicateScenarioProps) {
+ const queryClient = useQueryClient();
+ const [session] = useSession();
+
+ const duplicateScenario = ({ id }: DuplicateScenarioProps) => {
+ // Pending endpoint
+ return SCENARIOS.request({
+ url: `/${id}`,
+ headers: {
+ Authorization: `Bearer ${session.accessToken}`,
+ },
+ ...requestConfig,
+ });
+ };
+
+ return useMutation(duplicateScenario, {
+ onSuccess: (data: any, variables, context) => {
+ const { id, projectId } = data;
+ queryClient.invalidateQueries(['scenarios', projectId]);
+ queryClient.invalidateQueries(['scenarios', id]);
+ console.info('Succces', data, variables, context);
+ },
+ onError: (error, variables, context) => {
+ // An error happened!
+ console.info('Error', error, variables, context);
+ },
+ });
+}
diff --git a/app/hooks/scenarios/types.ts b/app/hooks/scenarios/types.ts
index 81634f136e..c6d2583f3a 100644
--- a/app/hooks/scenarios/types.ts
+++ b/app/hooks/scenarios/types.ts
@@ -56,3 +56,12 @@ export interface SaveScenarioPUProps {
id?: string,
data: any
}
+
+// useDuplicateScenario
+export interface UseDuplicateScenarioProps {
+ requestConfig?: AxiosRequestConfig
+}
+
+export interface DuplicateScenarioProps {
+ id: string | string[];
+}
diff --git a/app/layout/community/published-projects/detail/component.tsx b/app/layout/community/published-projects/detail/component.tsx
index 5cac3e029f..eb3e3c4e09 100644
--- a/app/layout/community/published-projects/detail/component.tsx
+++ b/app/layout/community/published-projects/detail/component.tsx
@@ -23,6 +23,7 @@ export interface CommunityProjectsDetailProps {
export const CommunityProjectsDetail: React.FC = () => {
const { query } = useRouter();
const { pid } = query;
+
const {
data: publishedProject,
isFetching: publishedProjectIsFetching,
@@ -78,10 +79,12 @@ export const CommunityProjectsDetail: React.FC = (
+
+
{timesDuplicated && (
Duplicated
diff --git a/app/layout/community/published-projects/duplicate-button/component.tsx b/app/layout/community/published-projects/duplicate-button/component.tsx
index aca8733016..91747e3d08 100644
--- a/app/layout/community/published-projects/duplicate-button/component.tsx
+++ b/app/layout/community/published-projects/duplicate-button/component.tsx
@@ -1,5 +1,6 @@
-import React from 'react';
+import React, { useCallback } from 'react';
+import { useDuplicateProject } from 'hooks/projects';
import { useToasts } from 'hooks/toast';
import Button from 'components/button';
@@ -18,32 +19,62 @@ export const DuplicateButton: React.FC = ({
}: DuplicateButtonProps) => {
const { addToast } = useToasts();
- const handleDuplicated = () => {
- addToast(`success-project-duplicated-${id}`, (
- <>
- Success!
-
- Project
- {' '}
- {name}
- {' '}
- duplicated
-
- >
- ), {
- level: 'success',
+ const duplicateProjectMutation = useDuplicateProject({
+ requestConfig: {
+ method: 'POST',
+ },
+ });
+
+ const onDuplicate = useCallback(() => {
+ // Name must be the new one defined by the user
+ duplicateProjectMutation.mutate({ id }, {
+ onSuccess: ({ data: { data: s } }) => {
+ addToast('success-duplicate-project', (
+ <>
+ Success!
+
+ Project
+ {' '}
+ {name}
+ {' '}
+ duplicated
+
+ >
+ ), {
+ level: 'success',
+ });
+
+ console.info('Project duplicated succesfully', s);
+ },
+ onError: () => {
+ addToast('error-duplicate-project', (
+ <>
+ Error!
+
+ Project
+ {' '}
+ {name}
+ {' '}
+ not duplicated
+
+ >
+ ), {
+ level: 'error',
+ });
+
+ console.error('Project not duplicated');
+ },
});
- };
+ }, [id, addToast, duplicateProjectMutation, name]);
return (