feat: use REST API endpoint to create flow

This commit is contained in:
Ali BARIN
2024-09-18 14:51:23 +00:00
parent 1790ef0ee6
commit 2e5dfdbb0d
6 changed files with 39 additions and 47 deletions

View File

@@ -27,11 +27,12 @@ import useLazyApps from 'hooks/useLazyApps';
function createConnectionOrFlow(appKey, supportsConnections = false) { function createConnectionOrFlow(appKey, supportsConnections = false) {
if (!supportsConnections) { if (!supportsConnections) {
return URLS.CREATE_FLOW_WITH_APP(appKey); return URLS.CREATE_FLOW;
} }
return URLS.APP_ADD_CONNECTION(appKey); return URLS.APP_ADD_CONNECTION(appKey);
} }
function AddNewAppConnection(props) { function AddNewAppConnection(props) {
const { onClose } = props; const { onClose } = props;
const theme = useTheme(); const theme = useTheme();

View File

@@ -43,10 +43,7 @@ function AppFlows(props) {
text={formatMessage('app.noFlows')} text={formatMessage('app.noFlows')}
data-test="flows-no-results" data-test="flows-no-results"
{...(allowed && { {...(allowed && {
to: URLS.CREATE_FLOW_WITH_APP_AND_CONNECTION( to: URLS.CREATE_FLOW,
appKey,
connectionId
),
})} })}
/> />
)} )}

View File

@@ -41,19 +41,6 @@ export const APP_FLOWS_FOR_CONNECTION = (appKey, connectionId) =>
export const APP_FLOWS_PATTERN = '/app/:appKey/flows'; export const APP_FLOWS_PATTERN = '/app/:appKey/flows';
export const EDITOR = '/editor'; export const EDITOR = '/editor';
export const CREATE_FLOW = '/editor/create'; export const CREATE_FLOW = '/editor/create';
export const CREATE_FLOW_WITH_APP = (appKey) =>
`/editor/create?appKey=${appKey}`;
export const CREATE_FLOW_WITH_APP_AND_CONNECTION = (appKey, connectionId) => {
const params = {};
if (appKey) {
params.appKey = appKey;
}
if (connectionId) {
params.connectionId = connectionId;
}
const searchParams = new URLSearchParams(params).toString();
return `/editor/create?${searchParams}`;
};
export const FLOW_EDITOR = (flowId) => `/editor/${flowId}`; export const FLOW_EDITOR = (flowId) => `/editor/${flowId}`;
export const FLOWS = '/flows'; export const FLOWS = '/flows';
// TODO: revert this back to /flows/:flowId once we have a proper single flow page // TODO: revert this back to /flows/:flowId once we have a proper single flow page
@@ -100,5 +87,4 @@ export const DASHBOARD = FLOWS;
// External links and paths // External links and paths
// The paths are sensitive for their relativity. // The paths are sensitive for their relativity.
export const WEBHOOK_DOCS_PATH = export const WEBHOOK_DOCS_PATH = './apps/webhooks/connection';
'./apps/webhooks/connection';

View File

@@ -0,0 +1,23 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import api from 'helpers/api';
export default function useCreateFlow() {
const queryClient = useQueryClient();
const query = useMutation({
mutationFn: async () => {
const { data } = await api.post('/v1/flows');
return data;
},
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: ['flows'],
});
},
});
return query;
}

View File

@@ -134,10 +134,7 @@ export default function Application() {
color="primary" color="primary"
size="large" size="large"
component={Link} component={Link}
to={URLS.CREATE_FLOW_WITH_APP_AND_CONNECTION( to={URLS.CREATE_FLOW}
appKey,
connectionId,
)}
fullWidth fullWidth
icon={<AddIcon />} icon={<AddIcon />}
disabled={!allowed} disabled={!allowed}

View File

@@ -1,42 +1,30 @@
import * as React from 'react'; import * as React from 'react';
import { useNavigate, useSearchParams } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { useMutation } from '@apollo/client';
import CircularProgress from '@mui/material/CircularProgress'; import CircularProgress from '@mui/material/CircularProgress';
import Typography from '@mui/material/Typography'; import Typography from '@mui/material/Typography';
import * as URLS from 'config/urls'; import * as URLS from 'config/urls';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import { CREATE_FLOW } from 'graphql/mutations/create-flow'; import useCreateFlow from 'hooks/useCreateFlow';
import Box from '@mui/material/Box'; import Box from '@mui/material/Box';
export default function CreateFlow() { export default function CreateFlow() {
const [searchParams] = useSearchParams();
const navigate = useNavigate(); const navigate = useNavigate();
const formatMessage = useFormatMessage(); const formatMessage = useFormatMessage();
const [createFlow, { error }] = useMutation(CREATE_FLOW); const { mutateAsync: createFlow, isError } = useCreateFlow();
const appKey = searchParams.get('appKey');
const connectionId = searchParams.get('connectionId');
React.useEffect(() => { React.useEffect(() => {
async function initiate() { async function initiate() {
const variables = {}; const response = await createFlow();
if (appKey) {
variables.triggerAppKey = appKey; const flowId = response.data?.id;
}
if (connectionId) {
variables.connectionId = connectionId;
}
const response = await createFlow({
variables: {
input: variables,
},
});
const flowId = response.data?.createFlow?.id;
navigate(URLS.FLOW_EDITOR(flowId), { replace: true }); navigate(URLS.FLOW_EDITOR(flowId), { replace: true });
} }
initiate();
}, [createFlow, navigate, appKey, connectionId]);
if (error) { initiate();
}, [createFlow, navigate]);
if (isError) {
return null; return null;
} }