import React, { useState } from 'react'; import { DropdownList, DropdownItem, MenuToggleAction, Spinner, Flex, FlexItem, Modal, Button, } from '@patternfly/react-core'; import useChrome from '@redhat-cloud-services/frontend-components/useChrome'; import { AMPLITUDE_MODULE_NAME } from '../../../../../constants'; import { setBlueprintId } from '../../../../../store/BlueprintSlice'; import { useAppDispatch } from '../../../../../store/hooks'; import { CreateBlueprintRequest, useComposeBlueprintMutation, useCreateBlueprintMutation, } from '../../../../../store/imageBuilderApi'; type CreateDropdownProps = { getBlueprintPayload: () => Promise<'' | CreateBlueprintRequest | undefined>; setIsOpen: (isOpen: boolean) => void; isDisabled?: boolean; }; export const CreateSaveAndBuildBtn = ({ getBlueprintPayload, setIsOpen, isDisabled, }: CreateDropdownProps) => { const { analytics } = useChrome(); const [buildBlueprint] = useComposeBlueprintMutation(); const [createBlueprint] = useCreateBlueprintMutation({ fixedCacheKey: 'createBlueprintKey', }); const dispatch = useAppDispatch(); const onSaveAndBuild = async () => { const requestBody = await getBlueprintPayload(); setIsOpen(false); analytics.track(`${AMPLITUDE_MODULE_NAME}-blueprintCreated`, { module: AMPLITUDE_MODULE_NAME, type: 'createBlueprintAndBuildImages', }); const blueprint = requestBody && (await createBlueprint({ createBlueprintRequest: requestBody, }).unwrap()); // unwrap - access the success payload immediately after a mutation if (blueprint) { buildBlueprint({ id: blueprint.id, body: {} }); dispatch(setBlueprintId(blueprint.id)); } }; return ( Create blueprint and build image(s) ); }; export const CreateSaveButton = ({ setIsOpen, getBlueprintPayload, isDisabled, }: CreateDropdownProps) => { const { analytics } = useChrome(); const [createBlueprint, { isLoading }] = useCreateBlueprintMutation({ fixedCacheKey: 'createBlueprintKey', }); const dispatch = useAppDispatch(); const [showModal, setShowModal] = useState(false); const wasModalSeen = window.localStorage.getItem( 'imageBuilder.saveAndBuildModalSeen' ); const SaveAndBuildImagesModal = () => { const handleClose = () => { setShowModal(false); }; return ( Close , ]} > Building blueprints and images doesn’t need to be a two step process. To build images simultaneously, use the dropdown arrow to the right side of this button. ); }; const onClick = () => { if (!wasModalSeen) { setShowModal(true); window.localStorage.setItem('imageBuilder.saveAndBuildModalSeen', 'true'); } else { onSave(); } }; const onSave = async () => { const requestBody = await getBlueprintPayload(); setIsOpen(false); analytics.track(`${AMPLITUDE_MODULE_NAME}-blueprintCreated`, { module: AMPLITUDE_MODULE_NAME, type: 'createBlueprint', }); const blueprint = requestBody && (await createBlueprint({ createBlueprintRequest: requestBody, }).unwrap()); if (blueprint) { dispatch(setBlueprintId(blueprint?.id)); } }; return ( <> {showModal && } {isLoading && ( )} Create blueprint ); };