Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
"eslint-plugin-storybook": "^9.1.4",
"eslint-plugin-testing-library": "^7.6.8",
"fishery": "^2.3.1",
"graphql": "^16.11.0",
"graphql-request": "^7.2.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^30.1.3",
"jest-environment-jsdom": "^30.1.2",
Expand Down
128 changes: 126 additions & 2 deletions src/components/TableToolsTable/TableToolsTable.stories.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import React, { useCallback, useEffect } from 'react';
import propTypes from 'prop-types';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { gql, request } from 'graphql-request';

import defaultStoryMeta from '~/support/defaultStoryMeta';
import mswGraphQlHandlers from '~/support/api/graphql';
import mswRestHandlers from '~/support/api/rest';

import columns from '~/support/factories/columns';
import filters, {
customNumberFilterType,
Expand All @@ -16,10 +20,12 @@ import CustomEmptyState from '~/support/components/CustomEmptyState';
import DetailsRow from '~/support/components/DetailsRow';
import DedicatedAction from '~/support/components/DedicatedAction';
import { actions, rowActionResolver } from '~/support/constants';
import { selectedItemIds } from '~/support/api';

// TODO fix preselection
// import { selectedItemIds } from '~/support/api';
const selectedItemIds = [];
import { TableToolsTable, TableStateProvider } from '~/components';
import { useFullTableState, useStateCallbacks } from '~/hooks';
import { useQueryWithUtilities } from '~/utilities';

const queryClient = new QueryClient();

Expand Down Expand Up @@ -208,6 +214,124 @@ export const Common = {
],
render: (args) => <CommonExample {...args} />,
};
const GET_TRACKS_QUERY = gql`
query GetTracks($sort: String) {
tracks(sort: $sort) {
id
}
}
`;

const GraphQLExample = ({
debug,
columns,
filters,
filtered,
enableDefaultFilter,
defaultFilter,
sortable,
initialSort,
enableInitialSort,
manageColumns,
enableRowActions,
enableActions,
dedicatedAction,
customEmptyRows,
customEmptyState,
enableExport,
enableDetails,
enableBulkSelect,
enablePreselection,
enableSimpleBulkSelect,
}) => {
const fetchFn = useCallback(async (params) => {
console.log('params gql', params);
return await request('http://local.com/graphql', GET_TRACKS_QUERY, params);
}, []);

const {
loading,
result: { data, meta: { total } = {} } = {},
error,
exporter,
itemIdsInTable,
itemIdsOnPage,
} = useQueryWithUtilities({
fetchFn,
useTableState: true,
});

return (
<TableToolsTable
loading={loading}
items={data}
total={total}
error={error}
columns={
sortable
? columns
: columns.map((column) => ({ ...column, sortable: undefined }))
}
{...(filters && filtered
? {
filters: {
filterConfig: [...filters, customNumberFilter],
customFilterTypes: {
number: customNumberFilterType,
},
...(enableDefaultFilter ? { activeFilters: defaultFilter } : {}),
},
}
: {})}
options={{
...defaultOptions,
debug,
manageColumns,
...(enableInitialSort ? { sortBy: initialSort } : {}),
...(enableRowActions
? {
actionResolver: rowActionResolver,
}
: {}),
...(enableActions ? { actions } : {}),
...(dedicatedAction ? { dedicatedAction: DedicatedAction } : {}),
...(customEmptyRows ? { emptyRows: emptyRows(columns?.length) } : {}),
...(customEmptyState ? { EmptyState: CustomEmptyState } : {}),
...(enableExport ? { exporter } : {}),
...(enableDetails ? { detailsComponent: DetailsRow } : {}),
...(enableBulkSelect
? {
...(enablePreselection ? { selected: selectedItemIds } : {}),
onSelect,
itemIdsInTable,
itemIdsOnPage,
}
: {}),
...(enableSimpleBulkSelect ? { onSelect: true } : {}),
}}
/>
);
};

GraphQLExample.propTypes = argProps;

export const GraphQL = {
parameters: {
msw: {
handlers: [...mswGraphQlHandlers, ...mswRestHandlers],
},
},
decorators: [
(Story) => (
<QueryClientProvider client={queryClient}>
<TableStateProvider>
<Story />
</TableStateProvider>
</QueryClientProvider>
),
],
render: (args) => <GraphQLExample {...args} />,
};

const WithTableTreeExample = ({
debug,
Expand Down
22 changes: 22 additions & 0 deletions src/support/api/backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { DatabaseSync } = require('node:sqlite');

Check failure on line 1 in src/support/api/backend/index.js

View workflow job for this annotation

GitHub Actions / build (21.x)

A `require()` style import is forbidden

Check failure on line 1 in src/support/api/backend/index.js

View workflow job for this annotation

GitHub Actions / build (22.x)

A `require()` style import is forbidden
Copy link

Choose a reason for hiding this comment

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

A require() style import is forbidden. [eslint:@typescript-eslint/no-require-imports]

const database = new DatabaseSync(':memory:');

const initialise = () => {
database.exec(`
CREATE TABLE data(
key INTEGER PRIMARY KEY,
value TEXT
) STRICT
`);
};

const query = (sql) => {

Check failure on line 13 in src/support/api/backend/index.js

View workflow job for this annotation

GitHub Actions / build (21.x)

'sql' is defined but never used

Check failure on line 13 in src/support/api/backend/index.js

View workflow job for this annotation

GitHub Actions / build (21.x)

'sql' is defined but never used

Check failure on line 13 in src/support/api/backend/index.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'sql' is defined but never used

Check failure on line 13 in src/support/api/backend/index.js

View workflow job for this annotation

GitHub Actions / build (22.x)

'sql' is defined but never used
Copy link

Choose a reason for hiding this comment

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

Found 2 issues:

1. 'sql' is defined but never used. [eslint:@typescript-eslint/no-unused-vars]


2. 'sql' is defined but never used. [eslint:no-unused-vars]

Suggested change
const query = (sql) => {
const query = () => {

Remove unused variable 'sql'.

const dbQuery = database.prepare('SELECT * FROM data ORDER BY key');

console.log(dbQuery.all());
};

export default {
initialise,
query,
};
17 changes: 17 additions & 0 deletions src/support/api/graphql/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { graphql, HttpResponse } from 'msw';

const api = graphql.link('http://local.com/graphql');

export default [
api.query('GetTracks', ({ variables, ...rest }) => {
console.log('gql rest', variables, rest);
return HttpResponse.json({
data: {
user: {
id: variables.id,
name: 'John Maverick',
},
},
});
}),
];
3 changes: 1 addition & 2 deletions src/support/api.js → src/support/api/rest/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ const queriedItems = (itemsToQuery) => {
const totalItems = itemsToQuery.slice(0, total);
const query = buildQuery(filters, sort);
const items = query.length ? jsonquery(totalItems, query) : totalItems;
const actualLimit = limit === 'max' ? items.length : limit;
const data = items.slice(
parseInt(offset),
parseInt(offset) + parseInt(actualLimit),
parseInt(offset) + parseInt(limit),
);

return {
Expand Down
Empty file added src/support/api/rest/helpers.js
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
apiTreehandler,
apiGenresHandler,
apiSelectionHandler,
} from './api';
} from './handlers';

const DEFAULT_DELAY = 500;

Expand Down
4 changes: 2 additions & 2 deletions src/support/defaultStoryMeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
PanelMainBody,
} from '@patternfly/react-core';

import mswHandlers from './mswHandler';
import mswRestHandlers from './api/rest';

const meta = {
parameters: {
msw: {
handlers: mswHandlers,
handlers: mswRestHandlers,
},
},
decorators: [
Expand Down
Loading