@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
179 lines (177 loc) • 7.06 kB
JavaScript
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import React, { useEffect, useState } from 'react';
import DialogBody from '../../DialogBody/DialogBody';
import DialogFooter from '../../DialogFooter/DialogFooter';
import { FormattedMessage } from 'react-intl';
import { makeStyles } from 'tss-react/mui';
import InputLabel from '@mui/material/InputLabel';
import FormControl from '@mui/material/FormControl';
import MenuItem from '@mui/material/MenuItem';
import Select from '@mui/material/Select';
import { push } from '../../../services/repositories';
import SecondaryButton from '../../SecondaryButton';
import PrimaryButton from '../../PrimaryButton';
import { isBlank } from '../../../utils/string';
import { FormControlLabel, Switch } from '@mui/material';
import useActiveSite from '../../../hooks/useActiveSite';
import useActiveUser from '../../../hooks/useActiveUser';
import useUpdateRefs from '../../../hooks/useUpdateRefs';
import FormHelperText from '@mui/material/FormHelperText';
import { useTheme } from '@mui/material/styles';
import { getStoredPushBranch, removeStoredPushBranch, setStoredPushBranch } from '../../../utils/state';
const useStyles = makeStyles()(() => ({
formControl: {
marginBottom: '15px'
}
}));
export function PushDialogContainer(props) {
const { branches, remoteName, onClose, onPushSuccess, onPushError, onSubmittingChange, isSubmitting } = props;
const [selectedBranch, setSelectedBranch] = useState('');
const { classes } = useStyles();
const { id: siteId, uuid } = useActiveSite();
const { username } = useActiveUser();
const [forcePush, setForcePush] = useState(false);
const fnRefs = useUpdateRefs({ onSubmittingChange, onPushSuccess, onPushError });
const theme = useTheme();
const onChange = (e) => setSelectedBranch(e.target.value);
const onCloseButtonClick = (e) => onClose(e, null);
const onSubmit = (e) => {
e.preventDefault();
if (!isBlank(selectedBranch)) {
onSubmittingChange(true);
push(siteId, remoteName, selectedBranch, forcePush).subscribe({
next() {
var _a, _b;
(_b = (_a = fnRefs.current).onPushSuccess) === null || _b === void 0 ? void 0 : _b.call(_a);
fnRefs.current.onSubmittingChange(false);
},
error({ response }) {
onPushError === null || onPushError === void 0 ? void 0 : onPushError(response.response);
fnRefs.current.onSubmittingChange(false);
}
});
}
};
useEffect(() => {
if (!selectedBranch && (branches === null || branches === void 0 ? void 0 : branches.length)) {
const storedPushBranch = getStoredPushBranch(uuid, username);
if (storedPushBranch) {
if (branches.includes(storedPushBranch)) {
setSelectedBranch(storedPushBranch);
} else {
removeStoredPushBranch(uuid, username);
}
} else {
setSelectedBranch(branches[0]);
}
}
}, [branches, selectedBranch, uuid, username]);
useEffect(() => {
if (selectedBranch) {
setStoredPushBranch(uuid, username, selectedBranch);
}
}, [branches, selectedBranch, uuid, username]);
return React.createElement(
'form',
{ onSubmit: onSubmit },
React.createElement(
DialogBody,
null,
React.createElement(
FormControl,
{ variant: 'outlined', fullWidth: true, className: classes.formControl, disabled: isSubmitting },
React.createElement(
InputLabel,
{ id: 'remoteBranchToPushLabel' },
React.createElement(FormattedMessage, {
id: 'repositories.remoteBranchToPush',
defaultMessage: 'Remote Branch to Push'
})
),
React.createElement(
Select,
{
labelId: 'remoteBranchToPullLabel',
name: 'branch',
value: selectedBranch,
onChange: onChange,
label: React.createElement(FormattedMessage, {
id: 'repositories.remoteBranchToPush',
defaultMessage: 'Remote Branch to Push'
}),
fullWidth: true
},
branches.map((branch) => React.createElement(MenuItem, { key: branch, value: branch }, branch))
)
),
React.createElement(
FormControl,
{ component: 'fieldset', variant: 'standard' },
React.createElement(FormControlLabel, {
disabled: isSubmitting,
control: React.createElement(Switch, {
checked: forcePush,
onChange: (e) => setForcePush(e.target.checked),
color: 'primary'
}),
label: React.createElement(FormattedMessage, {
id: 'pushToRemoteDialog.forcePush',
defaultMessage: 'Force push'
})
}),
React.createElement(
FormHelperText,
{ sx: { color: forcePush ? `error.${theme.palette.mode === 'light' ? 'dark' : 'light'}` : void 0 } },
React.createElement(FormattedMessage, {
id: 'repositories.forcePushWarning',
defaultMessage: "Force push will overwrite what's in the remote repository branch."
})
)
)
),
React.createElement(
DialogFooter,
null,
React.createElement(
SecondaryButton,
{ onClick: onCloseButtonClick, disabled: isSubmitting },
React.createElement(FormattedMessage, { id: 'words.cancel', defaultMessage: 'Cancel' })
),
React.createElement(
PrimaryButton,
{ type: 'submit', disabled: isBlank(selectedBranch), loading: isSubmitting },
React.createElement(FormattedMessage, { id: 'words.push', defaultMessage: 'Push' })
)
)
);
}
export default PushDialogContainer;