Skip to content

Commit

Permalink
Function to prefill promote modal w/ query params (#434)
Browse files Browse the repository at this point in the history
* Function to prefill promote modal w/ query params

* Remove unused commit query param

---------

Co-authored-by: Kyle Goss <[email protected]>
  • Loading branch information
kgwebsites and Kyle Goss authored Apr 19, 2023
1 parent 07d4d78 commit c89ac05
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
4 changes: 0 additions & 4 deletions src/components/pages/repo/new-build-form/new-build-form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ const NewBuildForm = ({
handleSubmit,
handleCancel,
target,
commit,
parameters
}) => {
const [state, setState] = useState({
target,
commit,
parameters,
});
const [parameterState, setParameterState] = useState({
Expand Down Expand Up @@ -115,15 +113,13 @@ const NewBuildForm = ({

NewBuildForm.defaultProps = {
target: "",
commit: "",
parameters: [],
};

NewBuildForm.propTypes = {
handleSubmit: PropTypes.func.isRequired,
handleCancel: PropTypes.func.isRequired,
target: PropTypes.string,
commit: PropTypes.string,
parameters: PropTypes.arrayOf(
PropTypes.shape({
key: PropTypes.string,
Expand Down
17 changes: 15 additions & 2 deletions src/pages/build/build.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,20 @@ export default function Build({ user, userIsAdminOrHasWritePerm }) {
const [state, setState] = useState(RESOLVED);
const [view, setView] = useState(LOGS_VIEW);

const [isModalShowing, toggleModal] = useModal();
const { search } = useLocation();
const queryParams = new URLSearchParams(search);
let action = 'promote'
let target = '';
let parameters = [];
try {
action = queryParams.get('action') || 'promote';
target = queryParams.get('target') || '';
parameters = queryParams.get('parameters') ? JSON.parse(queryParams.get('parameters')) : [];
} catch (e) {
console.warn('Invalid query parameters', e)
}
// If there is a target url param, show the new build modal on load
const [isModalShowing, toggleModal] = useModal(!!queryParams.get('target'));

const { showError } = useToast();

Expand Down Expand Up @@ -279,7 +292,7 @@ export default function Build({ user, userIsAdminOrHasWritePerm }) {
isShowing={isModalShowing}
hide={toggleModal}
>
<DeploymentForm handleSubmit={handleDeploySubmit} handleCancel={toggleModal} />
<DeploymentForm handleSubmit={handleDeploySubmit} handleCancel={toggleModal} action={action} target={target} parameters={parameters} />
</Modal>
</>
);
Expand Down
23 changes: 19 additions & 4 deletions src/pages/build/deployment-form/deployment-form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import css from './deployment-form.module.scss';

const cx = classNames.bind(css);

const DeploymentForm = ({ handleSubmit, handleCancel }) => {
const DeploymentForm = ({ handleSubmit, handleCancel, action, target, parameters }) => {
const [state, setState] = useState({
action: 'promote',
target: '',
parameters: [],
action,
target,
parameters,
});
const [parameterState, setParameterState] = useState({
key: '',
Expand Down Expand Up @@ -129,9 +129,24 @@ const DeploymentForm = ({ handleSubmit, handleCancel }) => {
);
};

NewBuildForm.defaultProps = {
action: "promote",
target: "",
parameters: [],
};

DeploymentForm.propTypes = {
handleSubmit: PropTypes.func.isRequired,
handleCancel: PropTypes.func.isRequired,
action: PropTypes.string,
target: PropTypes.string,
parameters: PropTypes.arrayOf(
PropTypes.shape({
key: PropTypes.string,
value: PropTypes.string,
id: PropTypes.string,
})
),
};

export default DeploymentForm;
4 changes: 1 addition & 3 deletions src/pages/repo/repo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ const Repo = ({ user }) => {
const { search } = useLocation();
const queryParams = new URLSearchParams(search);
let target = '';
let commit = '';
let parameters = [];
try {
target = queryParams.get('target') || '';
commit = queryParams.get('commit') || '';
parameters = queryParams.get('parameters') ? JSON.parse(queryParams.get('parameters')) : [];
} catch (e) {
console.warn('Invalid query parameters', e)
Expand Down Expand Up @@ -215,7 +213,7 @@ const Repo = ({ user }) => {
isShowing={isModalShowing}
hide={toggleModal}
>
<NewBuildForm handleSubmit={handleNewBuildSubmit} handleCancel={toggleModal} target={target} commit={commit} parameters={parameters} />
<NewBuildForm handleSubmit={handleNewBuildSubmit} handleCancel={toggleModal} target={target} parameters={parameters} />
</Modal>
</>
</Route>
Expand Down

0 comments on commit c89ac05

Please sign in to comment.