-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UI - Add Upgrade Button to Resources (#3387)
* add upgrade button to resources that can be upgraded (non major upgrades only) * update changelog * update ui version * fix cr review comments * Update ui/app/src/components/shared/ConfirmUpgradeResource.tsx Co-authored-by: James Griffin <me@JamesGriff.in> --------- Co-authored-by: James Griffin <me@JamesGriff.in>
- Loading branch information
1 parent
b1544e8
commit 9167c95
Showing
5 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
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
120 changes: 120 additions & 0 deletions
120
ui/app/src/components/shared/ConfirmUpgradeResource.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,120 @@ | ||
import { Dialog, DialogFooter, PrimaryButton, DialogType, Spinner, Dropdown, MessageBar, MessageBarType, DropdownMenuItemType, IDropdownOption, Icon, Stack, Label, IconButton, IDropdownProps } from '@fluentui/react'; | ||
import React, { useContext, useState } from 'react'; | ||
import { AvailableUpgrade, Resource } from '../../models/resource'; | ||
import { HttpMethod, ResultType, useAuthApiCall } from '../../hooks/useAuthApiCall'; | ||
import { WorkspaceContext } from '../../contexts/WorkspaceContext'; | ||
import { ResourceType } from '../../models/resourceType'; | ||
import { APIError } from '../../models/exceptions'; | ||
import { LoadingState } from '../../models/loadingState'; | ||
import { ExceptionLayout } from './ExceptionLayout'; | ||
import { useAppDispatch } from '../../hooks/customReduxHooks'; | ||
import { addUpdateOperation } from '../shared/notifications/operationsSlice'; | ||
|
||
interface ConfirmUpgradeProps { | ||
resource: Resource, | ||
onDismiss: () => void | ||
} | ||
|
||
export const ConfirmUpgradeResource: React.FunctionComponent<ConfirmUpgradeProps> = (props: ConfirmUpgradeProps) => { | ||
const apiCall = useAuthApiCall(); | ||
const [selectedVersion, setSelectedVersion] = useState("") | ||
const [apiError, setApiError] = useState({} as APIError); | ||
const [requestLoadingState, setRequestLoadingState] = useState(LoadingState.Ok); | ||
const workspaceCtx = useContext(WorkspaceContext); | ||
const dispatch = useAppDispatch(); | ||
|
||
const upgradeProps = { | ||
type: DialogType.normal, | ||
title: `Upgrade Template Version?`, | ||
closeButtonAriaLabel: 'Close', | ||
subText: `Are you sure you want upgrade the template version of ${props.resource.properties.display_name} from version ${props.resource.templateVersion}?`, | ||
}; | ||
|
||
const dialogStyles = { main: { maxWidth: 450 } }; | ||
const modalProps = { | ||
titleAriaId: 'labelId', | ||
subtitleAriaId: 'subTextId', | ||
isBlocking: true, | ||
styles: dialogStyles | ||
}; | ||
|
||
const wsAuth = (props.resource.resourceType === ResourceType.WorkspaceService || props.resource.resourceType === ResourceType.UserResource); | ||
|
||
const upgradeCall = async () => { | ||
setRequestLoadingState(LoadingState.Loading); | ||
try { | ||
let body = { templateVersion: selectedVersion } | ||
let op = await apiCall(props.resource.resourcePath, | ||
HttpMethod.Patch, | ||
wsAuth ? workspaceCtx.workspaceApplicationIdURI : undefined, | ||
body, | ||
ResultType.JSON, | ||
undefined, | ||
undefined, | ||
props.resource._etag); | ||
dispatch(addUpdateOperation(op.operation)); | ||
props.onDismiss(); | ||
} catch (err: any) { | ||
err.userMessage = 'Failed to upgrade resource'; | ||
setApiError(err); | ||
setRequestLoadingState(LoadingState.Error); | ||
} | ||
} | ||
|
||
const onRenderOption = (option: any): JSX.Element => { | ||
return ( | ||
<div> | ||
{option.data && option.data.icon && ( | ||
<Icon style={{ marginRight: '8px' }} iconName={option.data.icon} aria-hidden="true" title={option.data.icon} /> | ||
)} | ||
<span>{option.text}</span> | ||
</div> | ||
); | ||
}; | ||
|
||
const convertToDropDownOptions = (upgrade: Array<AvailableUpgrade>) => { | ||
return upgrade.map(upgrade => ({ "key": upgrade.version, "text": upgrade.version, data: { icon: upgrade.forceUpdateRequired ? 'Warning' : '' } })) | ||
} | ||
|
||
const getDropdownOptions = () => { | ||
const options = [] | ||
const nonMajorUpgrades = props.resource.availableUpgrades.filter(upgrade => !upgrade.forceUpdateRequired) | ||
options.push(...convertToDropDownOptions(nonMajorUpgrades)) | ||
return options; | ||
} | ||
|
||
return (<> | ||
<Dialog | ||
hidden={false} | ||
onDismiss={() => props.onDismiss()} | ||
dialogContentProps={upgradeProps} | ||
modalProps={modalProps} | ||
> | ||
{ | ||
requestLoadingState === LoadingState.Ok && | ||
<> | ||
<MessageBar messageBarType={MessageBarType.warning} >Upgrading the template version is irreversible.</MessageBar> | ||
<DialogFooter> | ||
<Dropdown | ||
placeholder='Select Version' | ||
options={getDropdownOptions()} | ||
onRenderOption={onRenderOption} | ||
styles={{ dropdown: { width: 125 } }} | ||
onChange={(event, option) => { option && setSelectedVersion(option.text); }} | ||
selectedKey={selectedVersion} | ||
/> | ||
<PrimaryButton primaryDisabled={!selectedVersion} text="Upgrade" onClick={() => upgradeCall()} /> | ||
</DialogFooter> | ||
</> | ||
} | ||
{ | ||
requestLoadingState === LoadingState.Loading && | ||
<Spinner label="Sending request..." ariaLive="assertive" labelPosition="right" /> | ||
} | ||
{ | ||
requestLoadingState === LoadingState.Error && | ||
<ExceptionLayout e={apiError} /> | ||
} | ||
</Dialog> | ||
</>); | ||
}; |
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