Merge pull request #2084 from automatisch/aut-1228
feat: use REST API endpoint to create flow
This commit is contained in:
@@ -5,7 +5,6 @@ import executeFlow from './mutations/execute-flow.js';
|
|||||||
import updateUser from './mutations/update-user.ee.js';
|
import updateUser from './mutations/update-user.ee.js';
|
||||||
import deleteStep from './mutations/delete-step.js';
|
import deleteStep from './mutations/delete-step.js';
|
||||||
import verifyConnection from './mutations/verify-connection.js';
|
import verifyConnection from './mutations/verify-connection.js';
|
||||||
import createFlow from './mutations/create-flow.js';
|
|
||||||
import deleteCurrentUser from './mutations/delete-current-user.ee.js';
|
import deleteCurrentUser from './mutations/delete-current-user.ee.js';
|
||||||
import updateCurrentUser from './mutations/update-current-user.js';
|
import updateCurrentUser from './mutations/update-current-user.js';
|
||||||
import generateAuthUrl from './mutations/generate-auth-url.js';
|
import generateAuthUrl from './mutations/generate-auth-url.js';
|
||||||
@@ -18,7 +17,6 @@ import updateFlowStatus from './mutations/update-flow-status.js';
|
|||||||
|
|
||||||
const mutationResolvers = {
|
const mutationResolvers = {
|
||||||
createConnection,
|
createConnection,
|
||||||
createFlow,
|
|
||||||
createUser,
|
createUser,
|
||||||
deleteCurrentUser,
|
deleteCurrentUser,
|
||||||
deleteFlow,
|
deleteFlow,
|
||||||
|
@@ -1,45 +0,0 @@
|
|||||||
import App from '../../models/app.js';
|
|
||||||
import Step from '../../models/step.js';
|
|
||||||
|
|
||||||
const createFlow = async (_parent, params, context) => {
|
|
||||||
context.currentUser.can('create', 'Flow');
|
|
||||||
|
|
||||||
const connectionId = params?.input?.connectionId;
|
|
||||||
const appKey = params?.input?.triggerAppKey;
|
|
||||||
|
|
||||||
if (appKey) {
|
|
||||||
await App.findOneByKey(appKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
const flow = await context.currentUser.$relatedQuery('flows').insert({
|
|
||||||
name: 'Name your flow',
|
|
||||||
});
|
|
||||||
|
|
||||||
if (connectionId) {
|
|
||||||
const hasConnection = await context.currentUser
|
|
||||||
.$relatedQuery('connections')
|
|
||||||
.findById(connectionId);
|
|
||||||
|
|
||||||
if (!hasConnection) {
|
|
||||||
throw new Error('The connection does not exist!');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await Step.query().insert({
|
|
||||||
flowId: flow.id,
|
|
||||||
type: 'trigger',
|
|
||||||
position: 1,
|
|
||||||
appKey,
|
|
||||||
connectionId,
|
|
||||||
});
|
|
||||||
|
|
||||||
await Step.query().insert({
|
|
||||||
flowId: flow.id,
|
|
||||||
type: 'action',
|
|
||||||
position: 2,
|
|
||||||
});
|
|
||||||
|
|
||||||
return flow;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default createFlow;
|
|
@@ -3,7 +3,6 @@ type Query {
|
|||||||
}
|
}
|
||||||
type Mutation {
|
type Mutation {
|
||||||
createConnection(input: CreateConnectionInput): Connection
|
createConnection(input: CreateConnectionInput): Connection
|
||||||
createFlow(input: CreateFlowInput): Flow
|
|
||||||
createUser(input: CreateUserInput): UserWithAcceptInvitationUrl
|
createUser(input: CreateUserInput): UserWithAcceptInvitationUrl
|
||||||
deleteCurrentUser: Boolean
|
deleteCurrentUser: Boolean
|
||||||
deleteFlow(input: DeleteFlowInput): Boolean
|
deleteFlow(input: DeleteFlowInput): Boolean
|
||||||
@@ -240,11 +239,6 @@ input VerifyConnectionInput {
|
|||||||
id: String!
|
id: String!
|
||||||
}
|
}
|
||||||
|
|
||||||
input CreateFlowInput {
|
|
||||||
triggerAppKey: String
|
|
||||||
connectionId: String
|
|
||||||
}
|
|
||||||
|
|
||||||
input UpdateFlowStatusInput {
|
input UpdateFlowStatusInput {
|
||||||
id: String!
|
id: String!
|
||||||
active: Boolean!
|
active: Boolean!
|
||||||
|
@@ -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();
|
||||||
|
@@ -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
|
|
||||||
),
|
|
||||||
})}
|
})}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
@@ -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';
|
|
||||||
|
@@ -1,9 +0,0 @@
|
|||||||
import { gql } from '@apollo/client';
|
|
||||||
export const CREATE_FLOW = gql`
|
|
||||||
mutation CreateFlow($input: CreateFlowInput) {
|
|
||||||
createFlow(input: $input) {
|
|
||||||
id
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
23
packages/web/src/hooks/useCreateFlow.js
Normal file
23
packages/web/src/hooks/useCreateFlow.js
Normal 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;
|
||||||
|
}
|
@@ -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}
|
||||||
|
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user