Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Param fix #3528

Merged
merged 5 commits into from
Mar 6, 2019
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 39 additions & 10 deletions client/app/components/EditParameterSettingsDialog.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { includes, words, capitalize, clone, isNull } from 'lodash';

import { includes, startsWith, words, capitalize, clone, isNull } from 'lodash';
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import Modal from 'antd/lib/modal';
import Form from 'antd/lib/form';
import Checkbox from 'antd/lib/checkbox';
import Button from 'antd/lib/button';
import Select from 'antd/lib/select';
import Input from 'antd/lib/input';
import Divider from 'antd/lib/divider';
Expand All @@ -18,8 +20,16 @@ function getDefaultTitle(text) {
return capitalize(words(text).join(' ')); // humanize
}

function NameInput({ name, onChange, existingNames, setValidation }) {
let helpText = `This is what will be added to your query editor {{ ${name} }}`;
function isTypeDate(type) {
return startsWith('date', type) && !isTypeDateRange(type);
}

function isTypeDateRange(type) {
return /-range/.test(type);
arikfr marked this conversation as resolved.
Show resolved Hide resolved
}

function NameInput({ name, type, onChange, existingNames, setValidation }) {
let helpText = '';
let validateStatus = '';

if (!name) {
Expand All @@ -30,6 +40,16 @@ function NameInput({ name, onChange, existingNames, setValidation }) {
setValidation(false);
validateStatus = 'error';
} else {
if (isTypeDateRange(type)) {
helpText = (
<React.Fragment>
Appears in query as {' '}
<code style={{ display: 'inline-block', color: 'inherit' }}>
{`{{${name}.start}} {{${name}.end}}`}
</code>
</React.Fragment>
);
}
setValidation(true);
}

Expand All @@ -41,7 +61,7 @@ function NameInput({ name, onChange, existingNames, setValidation }) {
validateStatus={validateStatus}
{...formItemProps}
>
<Input onChange={e => onChange(e.target.value)} />
<Input onChange={e => onChange(e.target.value)} autoFocus />
</Form.Item>
);
}
Expand All @@ -51,6 +71,7 @@ NameInput.propTypes = {
onChange: PropTypes.func.isRequired,
existingNames: PropTypes.arrayOf(PropTypes.string).isRequired,
setValidation: PropTypes.func.isRequired,
type: PropTypes.string.isRequired,
};

function EditParameterSettingsDialog(props) {
Expand Down Expand Up @@ -89,7 +110,7 @@ function EditParameterSettingsDialog(props) {
return true;
}

function onConfirm() {
function onConfirm(e) {
// update title to default
if (!param.title) {
// forced to do this cause param won't update in time for save
Expand All @@ -98,23 +119,31 @@ function EditParameterSettingsDialog(props) {
}

props.dialog.close(param);

e.preventDefault(); // stops form redirect
}

return (
<Modal
{...props.dialog.props}
title={isNew ? 'Add Parameter' : param.name}
onOk={onConfirm}
okText={isNew ? 'Add Parameter' : null}
okButtonProps={{ disabled: !isFulfilled() }}
width={600}
footer={[(
<Button key="cancel" onClick={props.dialog.dismiss}>Cancel</Button>
), (
<Button key="submit" htmlType="submit" disabled={!isFulfilled()} type="primary" form="paramForm">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is way better to handle Forms in Modals 🚀

{isNew ? 'Add Parameter' : 'OK'}
</Button>
)]}
>
<Form layout="horizontal">
<Form layout="horizontal" onSubmit={onConfirm} id="paramForm">
{isNew && (
<NameInput
name={param.name}
onChange={name => setParam({ ...param, name })}
setValidation={setIsNameValid}
existingNames={props.existingParams}
type={param.type}
/>
)}
<Form.Item label="Title" {...formItemProps}>
Expand Down Expand Up @@ -143,7 +172,7 @@ function EditParameterSettingsDialog(props) {
<Option value="datetime-range-with-seconds">Date and Time Range (with seconds)</Option>
</Select>
</Form.Item>
{includes(['date', 'datetime-local', 'datetime-with-seconds'], param.type) && (
{isTypeDate(param.type) && (
arikfr marked this conversation as resolved.
Show resolved Hide resolved
<Form.Item label=" " colon={false} {...formItemProps}>
<Checkbox
defaultChecked={param.useCurrentDateTime}
Expand Down