Skip to content

Commit

Permalink
feat: 🎸 limit query results
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejrybaniec committed Jul 31, 2020
1 parent 41c49da commit eea8325
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 70 deletions.
13 changes: 10 additions & 3 deletions lib/js/app/modules/queries/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export const setQueryCacheLimitError = (error: Error): QueriesActions => ({
payload: { error },
});

export const saveQuery = (name: string, body: Record<string, any>): QueriesActions => ({
export const saveQuery = (
name: string,
body: Record<string, any>
): QueriesActions => ({
type: SAVE_QUERY,
payload: { name, body },
});
Expand Down Expand Up @@ -73,7 +76,9 @@ export const getSavedQueries = (): QueriesActions => ({
type: GET_SAVED_QUERIES,
});

export const getSavedQueriesSuccess = (queries: Record<string, any>[]): QueriesActions => ({
export const getSavedQueriesSuccess = (
queries: Record<string, any>[]
): QueriesActions => ({
type: GET_SAVED_QUERIES_SUCCESS,
payload: { queries },
});
Expand All @@ -88,7 +93,9 @@ export const runQuery = (body: Record<string, any>): QueriesActions => ({
payload: { body },
});

export const runQuerySuccess = (results: Record<string, any>): QueriesActions => ({
export const runQuerySuccess = (
results: Record<string, any>
): QueriesActions => ({
type: RUN_QUERY_SUCCESS,
payload: { results },
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ exports[`renders date picker for timeframe end 1`] = `
box-shadow: 0 0 3px 1px rgba(119,163,187,0.5);
}
.c0 .DateInput_input:disabled {
border: 1px solid rgba(39,86,109,0.5);
background: #FFFFFF;
}
.c0 .DateInput_input:disabled::-webkit-input-placeholder {
color: rgba(39,52,55,0.4);
}
.c0 .DateInput_input:disabled::-moz-placeholder {
color: rgba(39,52,55,0.4);
}
.c0 .DateInput_input:disabled:-ms-input-placeholder {
color: rgba(39,52,55,0.4);
}
.c0 .DateInput_input:disabled::placeholder {
color: rgba(39,52,55,0.4);
}
.c0 .DateInput_fang {
display: none;
}
Expand Down Expand Up @@ -141,6 +162,27 @@ exports[`renders date picker for timeframe start 1`] = `
box-shadow: 0 0 3px 1px rgba(119,163,187,0.5);
}
.c0 .DateInput_input:disabled {
border: 1px solid rgba(39,86,109,0.5);
background: #FFFFFF;
}
.c0 .DateInput_input:disabled::-webkit-input-placeholder {
color: rgba(39,52,55,0.4);
}
.c0 .DateInput_input:disabled::-moz-placeholder {
color: rgba(39,52,55,0.4);
}
.c0 .DateInput_input:disabled:-ms-input-placeholder {
color: rgba(39,52,55,0.4);
}
.c0 .DateInput_input:disabled::placeholder {
color: rgba(39,52,55,0.4);
}
.c0 .DateInput_fang {
display: none;
}
Expand Down
6 changes: 1 addition & 5 deletions lib/js/app/queryCreator/components/GroupBy/GroupBy.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import React from 'react';
import { Provider } from 'react-redux';
import {
render as rtlRender,
fireEvent,
} from '@testing-library/react';
import { render as rtlRender, fireEvent } from '@testing-library/react';
import configureStore from 'redux-mock-store';

import GroupBy from './GroupBy';


const render = (storeState: any = {}, overProps: any = {}) => {
const mockStore = configureStore([]);
const store = mockStore({ ...storeState });
Expand Down
9 changes: 9 additions & 0 deletions lib/js/app/queryCreator/components/Input/Input.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ export const inputMixin = () => css`
&:focus {
box-shadow: 0 0 3px 1px rgba(119, 163, 187, 0.5);
}
&:disabled {
border: 1px solid ${transparentize(0.5, colors.blue[500])};
background: ${colors.white[500]};
}
&:disabled::placeholder {
color: ${transparentize(0.6, colors.black[400])};
}
`;

export const Input = styled.input`
Expand Down
65 changes: 63 additions & 2 deletions lib/js/app/queryCreator/components/Limit/Limit.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,41 @@ const render = (storeState: any = {}) => {
};
};

test('allows user to set limit', () => {
const {
wrapper: { getByTestId },
store,
} = render({
query: {
groupBy: ['category'],
orderBy: [{ propertyName: 'result', direction: 'DESC' }],
limit: undefined,
},
});
const input = getByTestId('limit');

fireEvent.change(input, { target: { value: 80 } });

expect(store.getActions()).toMatchInlineSnapshot(`
Array [
Object {
"payload": Object {
"limit": 80,
},
"type": "@query-creator/SET_LIMIT",
},
]
`);
});

test('allows user to set limit', () => {
const {
wrapper: { container },
store,
} = render({
query: {
groupBy: ['category'],
orderBy: [{ propertyName: 'result', direction: 'DESC' }],
limit: undefined,
},
});
Expand All @@ -46,16 +75,48 @@ test('allows user to set limit', () => {
`);
});

test('do not allows user to set limit without order settings', () => {
const {
wrapper: { getByTestId },
} = render({
query: {
groupBy: ['category'],
orderBy: undefined,
limit: undefined,
},
});
const input = getByTestId('limit');

expect(input).toBeDisabled();
});

test('do not allows user to set limit without group by settings', () => {
const {
wrapper: { getByTestId },
} = render({
query: {
groupBy: undefined,
orderBy: [{ propertyName: 'result', direction: 'DESC' }],
limit: undefined,
},
});
const input = getByTestId('limit');

expect(input).toBeDisabled();
});

test('allows user to remove limit', () => {
const {
wrapper: { container },
wrapper: { getByTestId },
store,
} = render({
query: {
limit: 100,
groupBy: ['category'],
orderBy: [{ propertyName: 'result', direction: 'DESC' }],
},
});
const input = container.querySelector('input[type="number"]');
const input = getByTestId('limit');

fireEvent.change(input, { target: { value: null } });

Expand Down
28 changes: 23 additions & 5 deletions lib/js/app/queryCreator/components/Limit/Limit.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React, { FC, useCallback, useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Label, Input } from '@keen.io/ui-core';

import { setLimit, getLimit } from '../../modules/query';
import Title from '../Title';
import Input from '../Input';

import {
setLimit,
getLimit,
getGroupBy,
getOrderBy,
} from '../../modules/query';

import text from './text.json';

Expand All @@ -11,6 +18,9 @@ type Props = {};
const Limit: FC<Props> = () => {
const dispatch = useDispatch();
const limit = useSelector(getLimit);
const groupBy = useSelector(getGroupBy);
const orderBy = useSelector(getOrderBy);
const isDisabled = !groupBy || !orderBy;

const changeHandler = useCallback((eventValue) => {
if (eventValue) {
Expand All @@ -25,14 +35,22 @@ const Limit: FC<Props> = () => {
return () => dispatch(setLimit(undefined));
}, []);

useEffect(() => {
if (isDisabled) {
dispatch(setLimit(undefined));
}
}, [isDisabled]);

return (
<>
<Label htmlFor="limit">{text.label}</Label>
<Title isDisabled={isDisabled}>{text.label}</Title>
<Input
disabled={isDisabled}
type="number"
data-testid="limit"
id="limit"
variant="solid"
value={limit ? limit : undefined}
placeholder={text.placeholder}
value={limit ? limit : ''}
onChange={(e) => changeHandler(e.target.value)}
/>
</>
Expand Down
3 changes: 2 additions & 1 deletion lib/js/app/queryCreator/components/Limit/text.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"label": "Limit"
"label": "Limit results to",
"placeholder": "Limit to"
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ test('allows user to remove order by settings', () => {
Array [
Object {
"payload": Object {
"orderBy": Array [],
"orderBy": undefined,
},
"type": "@query-creator/SET_ORDER_BY",
},
Expand Down
Loading

0 comments on commit eea8325

Please sign in to comment.