-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #24601 - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/Committing-Changes.md#changes-files) for more information. - [x] A detailed QA plan exists on the associated ticket (if it isn't there, work with the product group's QA engineer to add it) - Click pencil - Edit script - Save - Check script was saved - Check activities - [x] Manual QA for all new/changed functionality
- Loading branch information
1 parent
f035821
commit de58010
Showing
10 changed files
with
253 additions
and
14 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Added modal to edit script contents |
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
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
147 changes: 147 additions & 0 deletions
147
frontend/pages/ManageControlsPage/Scripts/components/EditScriptModal/EditScriptModal.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,147 @@ | ||
import React, { useContext, useState } from "react"; | ||
import { useQuery } from "react-query"; | ||
|
||
import { NotificationContext } from "context/notification"; | ||
import scriptAPI from "services/entities/scripts"; | ||
|
||
import Button from "components/buttons/Button"; | ||
import CustomLink from "components/CustomLink"; | ||
import DataError from "components/DataError"; | ||
import Editor from "components/Editor"; | ||
import Modal from "components/Modal"; | ||
import ModalFooter from "components/ModalFooter"; | ||
import Spinner from "components/Spinner"; | ||
import paths from "router/paths"; | ||
|
||
import { ScriptContent } from "interfaces/script"; | ||
import { DEFAULT_USE_QUERY_OPTIONS } from "utilities/constants"; | ||
import { getErrorMessage } from "../ScriptUploader/helpers"; | ||
|
||
const baseClass = "edit-script-modal"; | ||
|
||
interface IEditScriptModal { | ||
onExit: () => void; | ||
scriptId: number; | ||
scriptName: string; | ||
} | ||
|
||
const validate = (scriptContent: string) => { | ||
if (scriptContent.trim() === "") { | ||
return "Script cannot be empty"; | ||
} | ||
return null; | ||
}; | ||
|
||
const EditScriptModal = ({ | ||
scriptId, | ||
scriptName, | ||
onExit, | ||
}: IEditScriptModal) => { | ||
const { renderFlash } = useContext(NotificationContext); | ||
|
||
// Editable script content | ||
const [scriptFormData, setScriptFormData] = useState(""); | ||
const [isSubmitting, setIsSubmitting] = useState(false); | ||
const [formError, setFormError] = useState<string | null>(null); | ||
|
||
const { | ||
error: isSelectedScriptContentError, | ||
isLoading: isLoadingSelectedScriptContent, | ||
} = useQuery<ScriptContent, Error>( | ||
[scriptId], | ||
() => scriptAPI.downloadScript(scriptId), | ||
{ | ||
...DEFAULT_USE_QUERY_OPTIONS, | ||
onSuccess: (scriptContent) => { | ||
setScriptFormData(scriptContent); | ||
}, | ||
} | ||
); | ||
|
||
const onChange = (value: string) => { | ||
setScriptFormData(value); | ||
setFormError(validate(value)); | ||
}; | ||
|
||
const onSave = async () => { | ||
if (isSubmitting) { | ||
return; | ||
} | ||
try { | ||
setIsSubmitting(true); | ||
await scriptAPI.updateScript(scriptId, scriptFormData, scriptName); | ||
renderFlash("success", "Successfully saved script."); | ||
onExit(); | ||
} catch (e) { | ||
renderFlash("error", getErrorMessage(e)); | ||
} finally { | ||
setIsSubmitting(false); | ||
} | ||
}; | ||
|
||
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
onSave(); | ||
}; | ||
|
||
const renderContent = () => { | ||
if (isLoadingSelectedScriptContent) { | ||
return <Spinner />; | ||
} | ||
|
||
if (isSelectedScriptContentError) { | ||
return <DataError description="Close this modal and try again." />; | ||
} | ||
|
||
return ( | ||
<> | ||
<form onSubmit={onSubmit}> | ||
<Editor | ||
value={scriptFormData} | ||
onChange={onChange} | ||
isFormField | ||
error={formError} | ||
/> | ||
<div className="form-field__help-text"> | ||
To run this script on a host, go to the{" "} | ||
<CustomLink text="Hosts" url={paths.MANAGE_HOSTS} /> page and select | ||
a host. | ||
<br /> | ||
To run the script across multiple hosts, add a policy automation on | ||
the <CustomLink text="Policies" url={paths.MANAGE_POLICIES} /> page. | ||
</div> | ||
</form> | ||
<ModalFooter | ||
primaryButtons={ | ||
<> | ||
<Button onClick={onExit} variant="inverse"> | ||
Cancel | ||
</Button> | ||
<Button | ||
onClick={onSave} | ||
variant="brand" | ||
isLoading={isSubmitting} | ||
disabled={!!formError} | ||
> | ||
Save | ||
</Button> | ||
</> | ||
} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
return ( | ||
<Modal | ||
className={baseClass} | ||
title={scriptName} | ||
width="large" | ||
onExit={onExit} | ||
> | ||
{renderContent()} | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default EditScriptModal; |
1 change: 1 addition & 0 deletions
1
frontend/pages/ManageControlsPage/Scripts/components/EditScriptModal/index.ts
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 @@ | ||
export { default } from "./EditScriptModal"; |
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
Oops, something went wrong.