Skip to content

Commit

Permalink
feat: add page style dialog and its routing
Browse files Browse the repository at this point in the history
  • Loading branch information
harshpatel-crest committed Apr 1, 2021
1 parent 2d4d7dd commit a475235
Show file tree
Hide file tree
Showing 11 changed files with 351 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ControlWrapper from './ControlWrapper';
import { getUnifiedConfigs } from '../util/util';
import Validator, { SaveValidator } from '../util/Validator';
import { MODE_CLONE, MODE_CREATE, MODE_EDIT, MODE_CONFIG } from '../constants/modes';
import { PAGE_INPUT } from '../constants/pages';
import { axiosCallWrapper } from '../util/axiosCallWrapper';
import TableContext from '../context/TableContext';
import { parseErrorMsg } from '../util/messageUtil';
Expand Down Expand Up @@ -42,7 +43,7 @@ class BaseFormView extends PureComponent {
utilCustomFunctions: this.util,
};

if (props.page === 'inputs') {
if (props.page === PAGE_INPUT) {
globalConfig.pages.inputs.services.forEach((service) => {
if (service.name === props.serviceName) {
this.entities = service.entity;
Expand Down Expand Up @@ -80,7 +81,7 @@ class BaseFormView extends PureComponent {
const temState = {};
this.entities.forEach((e) => {
const tempEntity = {};

if (props.mode === MODE_CREATE) {
tempEntity.value = typeof e.defaultValue !== 'undefined' ? e.defaultValue : null;
tempEntity.display =
Expand Down Expand Up @@ -113,7 +114,7 @@ class BaseFormView extends PureComponent {
tempEntity.disabled = false;
temState[e.field] = tempEntity;
} else if (props.mode === MODE_CONFIG) {
e.defaultValue = typeof e.defaultValue !== 'undefined' ? e.defaultValue : null;
e.defaultValue = typeof e.defaultValue !== 'undefined' ? e.defaultValue : null;
tempEntity.value =
typeof this.currentInput[e.field] !== 'undefined'
? this.currentInput[e.field]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import BaseFormView from './BaseFormView';
import { axiosCallWrapper } from '../util/axiosCallWrapper';
import { MODE_CONFIG } from '../constants/modes';
import { WaitSpinnerWrapper } from './table/CustomTableStyle';
import { PAGE_CONF } from '../constants/pages';

function ConfigurationFormView({ serviceName }) {
const form = useRef();
Expand Down Expand Up @@ -46,7 +47,7 @@ function ConfigurationFormView({ serviceName }) {
<>
<BaseFormView
ref={form}
page="configuration"
page={PAGE_CONF}
stanzaName={serviceName}
serviceName="settings"
mode={MODE_CONFIG}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TableContextProvider } from '../context/TableContext';
import TableWrapper from './table/TableWrapper';
import EntityModal from './EntityModal';
import { MODE_CREATE } from '../constants/modes';
import { PAGE_CONF } from '../constants/pages';

function ConfigurationTable({ serviceName, serviceTitle }) {
const [open, setOpen] = useState(false);
Expand All @@ -23,7 +24,7 @@ function ConfigurationTable({ serviceName, serviceTitle }) {
if (open) {
return (
<EntityModal
page="configuration"
page={PAGE_CONF}
open={open}
handleRequestClose={handleRequestClose}
handleSaveData={() => {}}
Expand All @@ -39,7 +40,7 @@ function ConfigurationTable({ serviceName, serviceTitle }) {
<>
<TableContextProvider value={null}>
<TableWrapper
page="configuration"
page={PAGE_CONF}
serviceName={serviceName}
handleRequestModalOpen={() => handleRequestOpen()}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import React, { memo, useRef, useState } from 'react';
import PropTypes from 'prop-types';

import Button from '@splunk/react-ui/Button';
import Link from '@splunk/react-ui/Link';
import WaitSpinner from '@splunk/react-ui/WaitSpinner';
import ColumnLayout from '@splunk/react-ui/ColumnLayout';
import { _ } from '@splunk/ui-utils/i18n';
import { useSplunkTheme } from '@splunk/themes';

import { MODE_CLONE, MODE_CREATE, MODE_EDIT } from '../constants/modes';
import { PAGE_INPUT } from '../constants/pages';
import BaseFormView from './BaseFormView';
import { TitleComponent } from '../pages/Input/InputPageStyle';

function EntityPage({ handleRequestClose, serviceName, mode, stanzaName, formLabel }) {
const form = useRef();
const [isSubmitting, setIsSubmitting] = useState(false);
let buttonText = _('Submit');

if (mode === MODE_CREATE) {
buttonText = _('Add');
} else if (mode === MODE_CLONE) {
buttonText = _('Clone Input');
} else if (mode === MODE_EDIT) {
buttonText = _('Update');
}

const { embossShadow } = useSplunkTheme();
const colStyle = {
boxShadow: embossShadow,
padding: '5%',
};

const handleSubmit = () => {
const result = form.current.handleSubmit();
if (result) {
handleRequestClose();
}
};

const handleFormSubmit = (set, close) => {
setIsSubmitting(set);
if (close) {
handleRequestClose();
}
};
return (
<ColumnLayout gutter={8}>
<ColumnLayout.Row style={{ padding: '5px 0px' }}>
<ColumnLayout.Column>
<TitleComponent>
<Link onClick={handleRequestClose}>{_('Inputs')}</Link>
{' > '}
{_(formLabel)}
</TitleComponent>
</ColumnLayout.Column>
</ColumnLayout.Row>
<ColumnLayout.Row>
<ColumnLayout.Column span={2} />
<ColumnLayout.Column span={8} style={colStyle}>
<BaseFormView
ref={form}
page={PAGE_INPUT}
serviceName={serviceName}
mode={mode}
stanzaName={stanzaName}
handleFormSubmit={handleFormSubmit}
/>
</ColumnLayout.Column>
<ColumnLayout.Column span={2} />
</ColumnLayout.Row>
<ColumnLayout.Row>
<ColumnLayout.Column span={7} />
<ColumnLayout.Column span={3} style={{ textAlign: 'right' }}>
<Button
appearance="secondary"
onClick={handleRequestClose}
label={_('Cancel')}
disabled={isSubmitting}
/>
<Button
appearance="primary"
label={isSubmitting ? <WaitSpinner /> : buttonText}
onClick={handleSubmit}
disabled={isSubmitting}
/>
</ColumnLayout.Column>
<ColumnLayout.Column span={2} />
</ColumnLayout.Row>
</ColumnLayout>
);
}

EntityPage.propTypes = {
handleRequestClose: PropTypes.func,
serviceName: PropTypes.string,
mode: PropTypes.string,
stanzaName: PropTypes.string,
formLabel: PropTypes.string,
};

export default memo(EntityPage);
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ import WaitSpinner from '@splunk/react-ui/WaitSpinner';

import useQuery from '../../hooks/useQuery';
import { MODE_CLONE, MODE_EDIT } from '../../constants/modes';
import { PAGE_INPUT } from '../../constants/pages';
import { ActionButtonComponent } from './CustomTableStyle';
import { getUnifiedConfigs } from '../../util/util';
import { getExpansionRow } from './TableExpansionRow';
import EntityModal from '../EntityModal';
import DeleteModal from '../DeleteModal';
import CustomCell from '../CustomCell';
import { STYLE_MODAL, STYLE_PAGE } from '../../constants/dialogStyles';

function CustomTable({
page,
serviceName,
data,
handleToggleActionClick,
handleOpenPageStyleDialog,
handleSort,
sortDir,
sortKey,
Expand All @@ -35,7 +38,7 @@ function CustomTable({

const unifiedConfigs = getUnifiedConfigs();
const tableConfig =
page === 'inputs'
page === PAGE_INPUT
? unifiedConfigs.pages.inputs.table
: unifiedConfigs.pages.configuration.tabs.filter((x) => x.name === serviceName)[0]
.table;
Expand All @@ -44,6 +47,11 @@ function CustomTable({
// TODO: add multi field mapping support
const statusMapping = moreInfo?.filter((a) => a.mapping);

const serviceToStyleMap = {};
unifiedConfigs.pages.inputs.services.forEach((x) => {
serviceToStyleMap[x.name] = x.style === STYLE_PAGE ? STYLE_PAGE : STYLE_MODAL;
});

const history = useHistory();
const query = useQuery();

Expand Down Expand Up @@ -100,7 +108,7 @@ function CustomTable({
const generateModalDialog = () => {
if (entityModal.open) {
let label;
if (page === 'inputs') {
if (page === PAGE_INPUT) {
const { services } = unifiedConfigs.pages?.inputs;
label =
services[services.findIndex((x) => x.name === entityModal.serviceName)]?.title;
Expand Down Expand Up @@ -164,26 +172,34 @@ function CustomTable({
};

const handleEditActionClick = (row) => {
setEntityModal({
...entityModal,
open: true,
serviceName: row.serviceName,
stanzaName: row.name,
mode: MODE_EDIT,
});
// set query and push to history
query.set('record', row.name);
history.push({ search: query.toString() });
if (serviceToStyleMap[row.serviceName] === 'page') {
handleOpenPageStyleDialog(row, MODE_EDIT);
} else {
setEntityModal({
...entityModal,
open: true,
serviceName: row.serviceName,
stanzaName: row.name,
mode: MODE_EDIT,
});
// set query and push to history
query.set('record', row.name);
history.push({ search: query.toString() });
}
};

const handleCloneActionClick = (row) => {
setEntityModal({
...entityModal,
open: true,
serviceName: row.serviceName,
stanzaName: row.name,
mode: MODE_CLONE,
});
if (serviceToStyleMap[row.serviceName] === 'page') {
handleOpenPageStyleDialog(row, MODE_CLONE);
} else {
setEntityModal({
...entityModal,
open: true,
serviceName: row.serviceName,
stanzaName: row.name,
mode: MODE_CLONE,
});
}
};

const handleDeleteActionClick = (row) => {
Expand Down Expand Up @@ -331,6 +347,7 @@ CustomTable.propTypes = {
serviceName: PropTypes.string,
data: PropTypes.array.isRequired,
handleToggleActionClick: PropTypes.func,
handleOpenPageStyleDialog: PropTypes.func,
handleSort: PropTypes.func,
sortDir: PropTypes.string,
sortKey: PropTypes.string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { _ } from '@splunk/ui-utils/i18n';

import TableFilter from './TableFilter';
import TableContext from '../../context/TableContext';
import { PAGE_INPUT } from '../../constants/pages';
import { TableCaptionComponent, TableSelectBoxWrapper } from './CustomTableStyle';

function TableHeader({ page, services, totalElement, handleRequestModalOpen }) {
Expand All @@ -21,7 +22,7 @@ function TableHeader({ page, services, totalElement, handleRequestModalOpen }) {
setSearchType,
setSearchText,
} = useContext(TableContext);
const itemLabel = page === 'inputs' ? 'Input' : 'Item';
const itemLabel = page === PAGE_INPUT ? 'Input' : 'Item';
const getSearchTypeDropdown = () => {
if (services.length < 2) {
return null;
Expand All @@ -48,7 +49,7 @@ function TableHeader({ page, services, totalElement, handleRequestModalOpen }) {
<ColumnLayout gutter={8}>
<ColumnLayout.Row
style={
page === 'inputs'
page === PAGE_INPUT
? {
borderTop: '1px solid #e1e6eb',
padding: '5px 0px',
Expand All @@ -64,7 +65,7 @@ function TableHeader({ page, services, totalElement, handleRequestModalOpen }) {
<div>
{totalElement}
{totalElement > 1 ? _(` ${itemLabel}s`) : _(` ${itemLabel}`)}
{page === 'inputs' ? (
{page === PAGE_INPUT ? (
<TableSelectBoxWrapper>
<Select
value={pageSize}
Expand Down Expand Up @@ -118,7 +119,7 @@ function TableHeader({ page, services, totalElement, handleRequestModalOpen }) {
marginRight: '30px',
}}
/>
{page === 'inputs' ? null : (
{page === PAGE_INPUT ? null : (
<Button
label={_('Add')}
appearance="primary"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import CustomTable from './CustomTable';
import TableHeader from './TableHeader';
import TableContext from '../../context/TableContext';

function TableWrapper({ page, serviceName, handleRequestModalOpen }) {

function TableWrapper({ page, serviceName, handleRequestModalOpen, handleOpenPageStyleDialog }) {
const [sortKey, setSortKey] = useState('name');
const [sortDir, setSortDir] = useState('asc');
const [loading, setLoading] = useState(true);
Expand Down Expand Up @@ -226,6 +225,7 @@ function TableWrapper({ page, serviceName, handleRequestModalOpen }) {
handleSort={handleSort}
sortDir={sortDir}
sortKey={sortKey}
handleOpenPageStyleDialog={handleOpenPageStyleDialog}
/>
</>
);
Expand All @@ -235,6 +235,7 @@ TableWrapper.propTypes = {
page: PropTypes.string,
serviceName: PropTypes.string,
handleRequestModalOpen: PropTypes.func,
handleOpenPageStyleDialog: PropTypes.func,
};

export default memo(TableWrapper);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const STYLE_PAGE = 'page';
export const STYLE_MODAL = 'modal';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const PAGE_INPUT = 'inputs';
export const PAGE_CONF = 'configuration';
Loading

0 comments on commit a475235

Please sign in to comment.