Skip to content

Commit

Permalink
Prepopulate the new-build-form with query params (#432)
Browse files Browse the repository at this point in the history
Co-authored-by: Kyle Goss <[email protected]>
  • Loading branch information
kgwebsites and Kyle Goss authored Apr 13, 2023
1 parent 62843e8 commit c5348e8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
29 changes: 25 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 @@ -11,11 +11,17 @@ import css from './new-build-form.module.scss';

const cx = classNames.bind(css);

const NewBuildForm = ({ handleSubmit, handleCancel }) => {
const NewBuildForm = ({
handleSubmit,
handleCancel,
target,
commit,
parameters
}) => {
const [state, setState] = useState({
target: '',
commit: '',
parameters: [],
target,
commit,
parameters,
});
const [parameterState, setParameterState] = useState({
key: '',
Expand Down Expand Up @@ -107,9 +113,24 @@ const NewBuildForm = ({ handleSubmit, handleCancel }) => {
);
};

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,
value: PropTypes.string,
id: PropTypes.string,
})
),
};

export default NewBuildForm;
4 changes: 2 additions & 2 deletions src/components/shared/modal/modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import styles from './modal.module.scss';

const cx = classNames.bind(styles);

export const useModal = () => {
const [isShowing, setIsShowing] = useState(false);
export const useModal = (isShowingInit = false) => {
const [isShowing, setIsShowing] = useState(isShowingInit);
const toggle = () => setIsShowing(!isShowing);
return [isShowing, toggle];
};
Expand Down
19 changes: 16 additions & 3 deletions src/pages/repo/repo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, {
useCallback, useMemo, useContext,
} from 'react';
import {
Route, Switch, NavLink, useRouteMatch,
Route, Switch, NavLink, useRouteMatch, useLocation,
} from 'react-router-dom';

import { VISIBILITY_LEVELS } from '_constants';
Expand Down Expand Up @@ -62,7 +62,20 @@ const Repo = ({ user }) => {
} = params;
const [context] = useContext(AppContext);
const { isRepoNavDisabled } = context;
const [isModalShowing, toggleModal] = useModal();
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)
}
// If there is a target url param, show the new build modal on load
const [isModalShowing, toggleModal] = useModal(!!queryParams.get('target'));

const { data: repo, isLoading: isRepoDataLoading, isError } = useRepo({ namespace, name });

Expand Down Expand Up @@ -202,7 +215,7 @@ const Repo = ({ user }) => {
isShowing={isModalShowing}
hide={toggleModal}
>
<NewBuildForm handleSubmit={handleNewBuildSubmit} handleCancel={toggleModal} />
<NewBuildForm handleSubmit={handleNewBuildSubmit} handleCancel={toggleModal} target={target} commit={commit} parameters={parameters} />
</Modal>
</>
</Route>
Expand Down

0 comments on commit c5348e8

Please sign in to comment.