All Downloads are FREE. Search and download functionalities are using the official Maven repository.

apps.websight-package-manager.web-resources.components.modals.SchedulePackageActionsModal.js Maven / Gradle / Ivy

The newest version!
import React from "/apps/websight-atlaskit-esm/web-resources/react.js";
import Button, { ButtonGroup } from "/apps/websight-atlaskit-esm/web-resources/@atlaskit/button.js";
import { ErrorMessage, Fieldset } from "/apps/websight-atlaskit-esm/web-resources/@atlaskit/form.js";
import ModalDialog, { ModalTransition } from "/apps/websight-atlaskit-esm/web-resources/@atlaskit/modal-dialog.js";
import Tooltip from "/apps/websight-atlaskit-esm/web-resources/@atlaskit/tooltip.js";
import { colors } from "/apps/websight-admin/web-resources/theme.js";
import Form, { FormFooter } from "/apps/websight-rest-atlaskit-client/web-resources/js/Form.js";
import { DateTimeOffset } from "/apps/websight-admin/web-resources/DateUtils.js";
import PackageService from "../../services/PackageService.js";
import SchedulePackageActionRow from "./SchedulePackageActionRow.js";

const ActionsErrors = props => {
  const {
    errors = [],
    path
  } = props;

  if (!errors.length) {
    return null;
  }

  return /*#__PURE__*/React.createElement(React.Fragment, null, errors.map(error => error.path === path ? /*#__PURE__*/React.createElement(ErrorMessage, null, error.message) : null));
};

const infoIconStyle = {
  display: 'inline-block',
  cursor: 'default',
  fontSize: '18px',
  verticalAlign: 'top',
  paddingLeft: '6px',
  color: colors.grey
};

const convertActionsToViewModel = schedulePackageActions => {
  return (schedulePackageActions || []).map(action => {
    return { ...action,
      schedules: (action.schedules || []).map(schedule => {
        if (schedule.at) {
          const dateTimeOffset = new DateTimeOffset(schedule.at);
          return {
            at: {
              date: dateTimeOffset.getIsoDateString(),
              time: dateTimeOffset.getSimpleTimeString()
            }
          };
        }

        return schedule;
      })
    };
  });
};

const convertActionsToServerModel = schedulePackageActions => {
  return (schedulePackageActions || []).map(action => {
    return { ...action,
      schedules: (action.schedules || []).map(schedule => schedule.at ? {
        at: `${schedule.at.date}T${schedule.at.time}:00`
      } : schedule)
    };
  });
};

export default class SchedulePackageActionsModal extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isOpen: false,
      schedulePackageActions: [],
      validationErrors: []
    };
    this.open = this.open.bind(this);
    this.close = this.close.bind(this);
    this.onSubmitSuccess = this.onSubmitSuccess.bind(this);
    this.onFormSubmit = this.onFormSubmit.bind(this);
    this.addScheduleAction = this.addScheduleAction.bind(this);
    this.updateScheduleAction = this.updateScheduleAction.bind(this);
    this.addSchedule = this.addSchedule.bind(this);
    this.deleteSchedule = this.deleteSchedule.bind(this);
    this.updateSchedule = this.updateSchedule.bind(this);
  }

  open() {
    PackageService.getScheduledPackageActions(this.props.packagePath, ({
      entity
    }) => {
      this.setState({
        isOpen: true,
        validationErrors: [],
        serverDateTimeOffset: new DateTimeOffset(entity.currentSystemDateTime),
        schedulePackageActions: convertActionsToViewModel(entity.scheduledPackageActions)
      });
    });
  }

  close() {
    this.setState({
      isOpen: false
    });
  }

  formActions() {
    return [{
      appearance: 'primary',
      type: 'submit',
      text: 'Save',
      onSubmit: this.onFormSubmit
    }, {
      appearance: 'subtle',
      text: 'Cancel',
      onClick: this.close
    }];
  }

  onFormSubmit(requestData, onSuccess, onValidationFailure, onComplete) {
    const newRequestData = { ...requestData,
      path: this.props.packagePath,
      actions: JSON.stringify(convertActionsToServerModel(this.state.schedulePackageActions))
    };
    const originalOnValidationFailure = onValidationFailure;

    onValidationFailure = data => {
      this.setState({
        validationErrors: data.entity || []
      });
      originalOnValidationFailure(data);
    };

    const originalOnComplete = onComplete;

    onComplete = data => {
      this.setState({
        validationErrors: []
      });
      originalOnComplete(data);
    };

    PackageService.schedulePackageActions(newRequestData, onSuccess, onValidationFailure, onComplete);
  }

  deleteSchedule(scheduleAction, indexToRemove) {
    this.setState(prevState => {
      const schedulePackageActions = prevState.schedulePackageActions;
      const scheduleActionToUpdateIndex = schedulePackageActions.indexOf(scheduleAction);

      if (scheduleActionToUpdateIndex >= 0 && schedulePackageActions[scheduleActionToUpdateIndex].schedules.length > indexToRemove) {
        if (schedulePackageActions[scheduleActionToUpdateIndex].schedules.length === 1) {
          schedulePackageActions.splice(scheduleActionToUpdateIndex, 1);
        } else {
          schedulePackageActions[scheduleActionToUpdateIndex].schedules.splice(indexToRemove, 1);
        }

        return {
          schedulePackageActions: schedulePackageActions
        };
      }
    });
  }

  updateSchedule(scheduleAction, schedule, indexOfScheduleToUpdate) {
    this.setState(prevState => {
      const schedulePackageActions = prevState.schedulePackageActions;
      const scheduleActionToUpdateIndex = schedulePackageActions.indexOf(scheduleAction);

      if (scheduleActionToUpdateIndex >= 0) {
        schedulePackageActions[scheduleActionToUpdateIndex].schedules[indexOfScheduleToUpdate] = schedule;
        return {
          schedulePackageActions: schedulePackageActions
        };
      }
    });
  }

  addSchedule(scheduleAction) {
    this.setState(prevState => {
      const schedulePackageActions = prevState.schedulePackageActions;
      const scheduleActionToUpdateIndex = schedulePackageActions.indexOf(scheduleAction);

      if (scheduleActionToUpdateIndex >= 0) {
        scheduleAction.schedules.push({
          at: {
            date: prevState.serverDateTimeOffset.getIsoDateString(),
            time: prevState.serverDateTimeOffset.getSimpleTimeString()
          }
        });
        schedulePackageActions[scheduleActionToUpdateIndex] = scheduleAction;
        return {
          schedulePackageActions: schedulePackageActions
        };
      }
    });
  }

  addScheduleAction() {
    this.setState(prevState => {
      return {
        schedulePackageActions: [...prevState.schedulePackageActions, {
          action: 'BUILD',
          schedules: [{
            at: {
              date: prevState.serverDateTimeOffset.getIsoDateString(),
              time: prevState.serverDateTimeOffset.getSimpleTimeString()
            }
          }]
        }]
      };
    });
  }

  updateScheduleAction(updatedScheduleAction, index) {
    this.setState(prevState => {
      const schedulePackageActionToUpdate = prevState.schedulePackageActions[index];

      if (schedulePackageActionToUpdate) {
        schedulePackageActionToUpdate.action = updatedScheduleAction;
        return {
          schedulePackageActions: prevState.schedulePackageActions
        };
      }
    });
  }

  setSuspended(isSuspended, index) {
    this.setState(prevState => {
      const schedulePackageAction = prevState.schedulePackageActions[index];

      if (schedulePackageAction) {
        schedulePackageAction.suspended = isSuspended;
        return {
          schedulePackageActions: prevState.schedulePackageActions
        };
      }
    });
  }

  onSubmitSuccess() {
    this.close();
    this.props.onScheduleSuccess && this.props.onScheduleSuccess();
  }

  render() {
    const {
      packageName
    } = this.props;
    const {
      schedulePackageActions,
      validationErrors,
      serverDateTimeOffset
    } = this.state;
    return /*#__PURE__*/React.createElement(ModalTransition, null, this.state.isOpen && /*#__PURE__*/React.createElement(ModalDialog, {
      heading: "Schedule actions",
      onClose: this.close
    }, /*#__PURE__*/React.createElement("p", null, "Package: ", /*#__PURE__*/React.createElement("b", null, packageName)), /*#__PURE__*/React.createElement("p", null, "Current server time: ", serverDateTimeOffset.getEnGbDateTimeOffsetString(), /*#__PURE__*/React.createElement(Tooltip, {
      tag: "span",
      content: "Scheduled actions times are defined in server time zone"
    }, /*#__PURE__*/React.createElement("i", {
      className: "material-icons",
      style: infoIconStyle
    }, "info_outline"))), /*#__PURE__*/React.createElement(Form, {
      onSubmit: this.onFormSubmit,
      onSuccess: this.onSubmitSuccess
    }, ({
      submitted
    }) => /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Fieldset, {
      legend: "Actions"
    }, schedulePackageActions.map((schedulePackageAction, index) => {
      return /*#__PURE__*/React.createElement(SchedulePackageActionRow, {
        key: index,
        serverDateTimeOffset: serverDateTimeOffset,
        scheduleAction: schedulePackageAction,
        updateScheduleAction: updatedScheduleAction => this.updateScheduleAction(updatedScheduleAction, index),
        setSuspendingScheduleAction: isSuspended => this.setSuspended(isSuspended, index),
        deleteSchedule: indexToRemove => this.deleteSchedule(schedulePackageAction, indexToRemove),
        addSchedule: () => this.addSchedule(schedulePackageAction),
        updateSchedule: (updatedSchedule, indexOfUpdatedSchedule) => this.updateSchedule(schedulePackageAction, updatedSchedule, indexOfUpdatedSchedule)
      });
    }), /*#__PURE__*/React.createElement(ActionsErrors, {
      errors: validationErrors,
      path: "actions"
    })), /*#__PURE__*/React.createElement(Button, {
      onClick: this.addScheduleAction
    }, "Add Action Schedule"), /*#__PURE__*/React.createElement(FormFooter, null, /*#__PURE__*/React.createElement(ButtonGroup, null, /*#__PURE__*/React.createElement(Button, {
      appearance: "primary",
      type: "submit",
      isLoading: submitted
    }, "Save"), /*#__PURE__*/React.createElement(Button, {
      appearance: "subtle",
      onClick: this.close,
      isDisabled: submitted
    }, "Cancel")))))));
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy