@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
129 lines (127 loc) • 5.1 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, { useState } from 'react';
import RepoStatusSkeleton from './RepoStatusSkeleton';
import RepoStatusUI from './RepoStatusUI';
import CommitResolutionDialog from '../../CommitResolutionDialog/CommitResolutionDialog';
import { cancelFailedPull, resolveConflict } from '../../../services/repositories';
import { useDispatch } from 'react-redux';
import { showSystemNotification } from '../../../state/actions/system';
import { showErrorDialog } from '../../../state/reducers/dialogs/error';
import { useIntl } from 'react-intl';
import ConflictedPathDiffDialog from '../../ConflictedPathDiffDialog';
import { useActiveSiteId } from '../../../hooks/useActiveSiteId';
import { messages } from './translations';
export function RepoStatus(props) {
const { status, onCommitSuccess: onCommitSuccessProp, onFailedPullCancelled, onConflictResolved } = props;
const siteId = useActiveSiteId();
const [openCommitResolutionDialog, setOpenCommitResolutionDialog] = useState(false);
const [openRemoteRepositoriesDiffDialog, setOpenRemoteRepositoriesDiffDialog] = useState(false);
const [diffPath, setDiffPath] = useState(null);
const dispatch = useDispatch();
const [fetching, setFetching] = useState(false);
const { formatMessage } = useIntl();
if (!props.status || fetching) {
return React.createElement(RepoStatusSkeleton, null);
}
const onRevertPull = () => {
setFetching(true);
cancelFailedPull(siteId).subscribe({
next(status) {
onFailedPullCancelled === null || onFailedPullCancelled === void 0 ? void 0 : onFailedPullCancelled(status);
setFetching(false);
dispatch(
showSystemNotification({
message: formatMessage(messages.revertPullSuccessMessage)
})
);
},
error({ response }) {
dispatch(showErrorDialog({ error: response }));
}
});
};
const onCommitSuccess = (status) => {
onCommitSuccessProp === null || onCommitSuccessProp === void 0 ? void 0 : onCommitSuccessProp(status);
setFetching(false);
setOpenCommitResolutionDialog(false);
dispatch(
showSystemNotification({
message: formatMessage(messages.commitSuccessMessage)
})
);
};
const onResolveConflict = (resolution, path) => {
setFetching(true);
resolveConflict(siteId, path, resolution).subscribe({
next(status) {
onConflictResolved === null || onConflictResolved === void 0 ? void 0 : onConflictResolved(status);
setFetching(false);
setOpenRemoteRepositoriesDiffDialog(false);
},
error({ response }) {
dispatch(showErrorDialog({ error: response }));
}
});
};
const onCommitError = (response) => {
dispatch(showErrorDialog({ error: response }));
};
const openDiffDialog = (path) => {
setDiffPath(path);
setOpenRemoteRepositoriesDiffDialog(true);
};
return React.createElement(
React.Fragment,
null,
React.createElement(RepoStatusUI, {
status: status,
onRevertPull: onRevertPull,
onCommitClick: () => setOpenCommitResolutionDialog(true),
onResolveConflict: onResolveConflict,
onDiffClick: openDiffDialog
}),
React.createElement(CommitResolutionDialog, {
open: openCommitResolutionDialog,
onClose: () => setOpenCommitResolutionDialog(false),
onCommitRequestSent: () => setFetching(true),
onCommitSuccess: onCommitSuccess,
onCommitError: onCommitError
}),
React.createElement(ConflictedPathDiffDialog, {
open: openRemoteRepositoriesDiffDialog,
path: diffPath,
onResolveConflict: onResolveConflict,
onClose: () => setOpenRemoteRepositoriesDiffDialog(false)
})
);
}
export default RepoStatus;