-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Canvas] Move Templates to be stored as Saved Objects #69438
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React, { useState, useEffect, useRef } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Paginate as Component } from './paginate'; | ||
|
||
interface InPaginateProps { | ||
perPage?: number; | ||
startPage?: number; | ||
rows: any[]; | ||
children: (props: PaginateChildProps) => React.ReactNode; | ||
} | ||
|
||
export type PaginateProps = Omit<InPaginateProps, 'startPage'> & { | ||
pageNumber: number; | ||
totalPages: number; | ||
nextPageEnabled: boolean; | ||
prevPageEnabled: boolean; | ||
setPage: (num: number) => void; | ||
nextPage: () => void; | ||
prevPage: () => void; | ||
}; | ||
|
||
export type PaginateChildProps = Omit<PaginateProps, 'children'>; | ||
|
||
export const Paginate: React.FunctionComponent<InPaginateProps> = ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: you can import |
||
perPage = 10, | ||
startPage = 0, | ||
rows, | ||
children, | ||
}) => { | ||
const totalPages = Math.ceil(rows.length / perPage); | ||
const initialCurrentPage = totalPages > 0 ? Math.min(startPage, totalPages - 1) : 0; | ||
const [currentPage, setPage] = useState(initialCurrentPage); | ||
const hasRenderedRef = useRef<boolean>(false); | ||
const maxPage = totalPages - 1; | ||
const start = currentPage * perPage; | ||
const end = currentPage === 0 ? perPage : perPage * (currentPage + 1); | ||
const nextPageEnabled = currentPage < maxPage; | ||
const prevPageEnabled = currentPage > 0; | ||
const partialRows = rows.slice(start, end); | ||
|
||
const nextPage = () => { | ||
if (nextPageEnabled) { | ||
setPage(currentPage + 1); | ||
} | ||
}; | ||
|
||
const prevPage = () => { | ||
if (prevPageEnabled) { | ||
setPage(currentPage - 1); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (!hasRenderedRef.current) { | ||
hasRenderedRef.current = true; | ||
} else { | ||
setPage(0); | ||
} | ||
}, [perPage, hasRenderedRef]); | ||
|
||
return ( | ||
<Component | ||
rows={partialRows} | ||
perPage={perPage} | ||
pageNumber={currentPage} | ||
totalPages={totalPages} | ||
setPage={setPage} | ||
nextPage={nextPage} | ||
prevPage={prevPage} | ||
nextPageEnabled={nextPageEnabled} | ||
prevPageEnabled={prevPageEnabled} | ||
children={children} | ||
/> | ||
); | ||
}; | ||
|
||
Paginate.propTypes = { | ||
rows: PropTypes.array.isRequired, | ||
perPage: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
startPage: PropTypes.number, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,25 +4,31 @@ | |
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { PaginateProps } from './'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I would put this props interface in this file and export the other direction... I've never liked how Redux expects the opposite. YMMV. |
||
|
||
export const Paginate = (props) => { | ||
return props.children({ | ||
rows: props.partialRows, | ||
perPage: props.perPage, | ||
pageNumber: props.pageNumber, | ||
totalPages: props.totalPages, | ||
nextPageEnabled: props.nextPageEnabled, | ||
prevPageEnabled: props.prevPageEnabled, | ||
setPage: (num) => props.setPage(num), | ||
nextPage: props.nextPage, | ||
prevPage: props.prevPage, | ||
}); | ||
export const Paginate: React.FunctionComponent<PaginateProps> = (props) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: destructure this object so you can avoid the |
||
return ( | ||
<React.Fragment> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: you can use |
||
{props.children({ | ||
rows: props.rows, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: doesn't this collection match
...or...
|
||
perPage: props.perPage, | ||
pageNumber: props.pageNumber, | ||
totalPages: props.totalPages, | ||
nextPageEnabled: props.nextPageEnabled, | ||
prevPageEnabled: props.prevPageEnabled, | ||
setPage: props.setPage, | ||
nextPage: props.nextPage, | ||
prevPage: props.prevPage, | ||
})} | ||
</React.Fragment> | ||
); | ||
}; | ||
|
||
Paginate.propTypes = { | ||
children: PropTypes.func.isRequired, | ||
partialRows: PropTypes.array.isRequired, | ||
rows: PropTypes.array.isRequired, | ||
perPage: PropTypes.number.isRequired, | ||
pageNumber: PropTypes.number.isRequired, | ||
totalPages: PropTypes.number.isRequired, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
// TODO: We should fully build out this interface for our router | ||
// or switch to a different router that is already typed | ||
interface Router { | ||
navigateTo: ( | ||
name: string, | ||
params: Record<string, number | string>, | ||
state?: Record<string, string> | ||
) => void; | ||
} | ||
|
||
export const RouterContext = React.createContext<Router | undefined>(undefined); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { action } from '@storybook/addon-actions'; | ||
import { WorkpadTemplates } from '../workpad_templates'; | ||
import { CanvasTemplate } from '../../../../types'; | ||
|
||
const templates: Record<string, CanvasTemplate> = { | ||
test1: { | ||
id: 'test1-id', | ||
name: 'test1', | ||
help: 'This is a test template', | ||
tags: ['tag1', 'tag2'], | ||
template_key: 'test1-key', | ||
}, | ||
test2: { | ||
id: 'test2-id', | ||
name: 'test2', | ||
help: 'This is a second test template', | ||
tags: ['tag2', 'tag3'], | ||
template_key: 'test2-key', | ||
}, | ||
}; | ||
|
||
storiesOf('components/WorkpadTemplates', module) | ||
.addDecorator((story) => <div style={{ width: '500px' }}>{story()}</div>) | ||
.add('default', () => { | ||
const onCreateFromTemplateAction = action('onCreateFromTemplate'); | ||
return ( | ||
<WorkpadTemplates | ||
templates={templates} | ||
onClose={action('onClose')} | ||
onCreateFromTemplate={(template) => { | ||
onCreateFromTemplateAction(template); | ||
return Promise.resolve(); | ||
}} | ||
/> | ||
); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd consider changing this to
childFn
... I was confused about this being a function producing children as opposed to a collection of children.