Skip to content

Commit

Permalink
feat(cms): add Many to One field on create resource form
Browse files Browse the repository at this point in the history
  • Loading branch information
bahdcoder committed Dec 10, 2021
1 parent 7108f22 commit ce8439f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 55 deletions.
14 changes: 6 additions & 8 deletions examples/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,12 @@ export default tensei()
text('Stripe Checkout ID').hideOnIndex(),
belongsToMany('Product')
]),
resource('Order Item')
.fields([
integer('Quantity').min(0).rules('min:0', 'required'),
integer('Total').rules('required', 'min:0'),
belongsTo('Order').rules('required'),
belongsTo('Product').rules('required')
])
.hideFromNavigation(),
resource('Order Item').fields([
integer('Quantity').min(0).rules('min:0', 'required'),
integer('Total').rules('required', 'min:0'),
belongsTo('Order').rules('required'),
belongsTo('Product').rules('required')
]),
resource('Option')
.fields([
text('Name').rules('Required'),
Expand Down
1 change: 1 addition & 0 deletions packages/cms/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Core {
Integer: FormNumber,
Boolean: FormBoolean,
Textarea: FormTextarea,
ManyToOne: FormBelongsToMany,
ManyToMany: FormBelongsToMany
},
index: {}
Expand Down
47 changes: 34 additions & 13 deletions packages/cms/form/belongs-to-many/belongs-to-many.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const BelongsToMany: React.FunctionComponent<FormComponentProps> = ({
editing,
resource
}) => {
const isManyToOne = field.component.form === 'ManyToOne'
const [createFlyOutOpen, setCreateFlyOutOpen] = useState(false)
const [documents, setDocuments] = useState<any[]>([])
const [selectedItems, setSelectedItems] = useState<any[]>([])
Expand Down Expand Up @@ -165,7 +166,25 @@ export const BelongsToMany: React.FunctionComponent<FormComponentProps> = ({
<EuiFlyoutBody>
<Resource
resource={relatedResource}
tableProps={{ onSelect: setSelectedItems }}
tableProps={{
onSelect: setSelectedItems,
inFlyout: true,
hideSelection: isManyToOne,
actions: isManyToOne
? [
{
name: 'Link',
description: 'Link this item',
icon: 'link',
type: 'icon',
onClick: item => {
setDocuments([item])
setFlyoutOpen(false)
}
}
]
: []
}}
/>
</EuiFlyoutBody>

Expand All @@ -180,18 +199,20 @@ export const BelongsToMany: React.FunctionComponent<FormComponentProps> = ({
Close
</EuiButtonEmpty>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButton
disabled={selectedItems.length === 0}
onClick={() => {
setDocuments(selectedItems)
closeFlyout()
}}
fill
>
Add selected {relatedResource?.label}
</EuiButton>
</EuiFlexItem>
{isManyToOne ? null : (
<EuiFlexItem grow={false}>
<EuiButton
disabled={selectedItems.length === 0}
onClick={() => {
setDocuments(selectedItems)
closeFlyout()
}}
fill
>
Add selected {relatedResource?.label}
</EuiButton>
</EuiFlexItem>
)}
</EuiFlexGroup>
</EuiFlyoutFooter>
</EuiFlyout>
Expand Down
81 changes: 47 additions & 34 deletions packages/cms/pages/resources/resource/resource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
EuiBasicTable,
EuiBasicTableProps,
EuiBasicTableColumn,
EuiTableActionsColumnType,
CriteriaWithPagination
} from '@tensei/eui/lib/components/basic_table'
import {
Expand Down Expand Up @@ -219,6 +220,9 @@ interface MetaData {
}
interface TableProps {
search: string
hideSelection?: boolean
inFlyout?: boolean
actions?: EuiTableActionsColumnType<any>['actions']
resource: ResourceContract
filters: ActiveFilter[]
applyFilter: (filter: ActiveFilter) => void
Expand All @@ -229,7 +233,10 @@ export const Table: React.FunctionComponent<TableProps> = ({
search,
filters,
resource,
onSelect
onSelect,
hideSelection,
inFlyout,
actions
}) => {
const { fetchTableData, deleteTableData } = useResourceStore()
const [pageSize, setPageSize] = useState(resource?.perPageOptions[0])
Expand Down Expand Up @@ -316,33 +323,35 @@ export const Table: React.FunctionComponent<TableProps> = ({
})) || []),
{
name: 'Actions',
actions: [
{
name: 'Edit',
description: 'Edit this item',
icon: 'pencil',
type: 'icon',
// color: 'danger',
onClick: item => {
push(
window.Tensei.getPath(
`resources/${resource?.slugPlural}/${item.id}/edit`
)
)
}
},
{
name: 'Delete',
description: 'Delete this item',
icon: 'trash',
type: 'icon',
color: 'danger',
onClick: item => {
setIsModalVisible(true)
setItemsSelectedForDelete([item.id])
}
}
]
actions: inFlyout
? actions || []
: [
{
name: 'Edit',
description: 'Edit this item',
icon: 'pencil',
type: 'icon',
// color: 'danger',
onClick: item => {
push(
window.Tensei.getPath(
`resources/${resource?.slugPlural}/${item.id}/edit`
)
)
}
},
{
name: 'Delete',
description: 'Delete this item',
icon: 'trash',
type: 'icon',
color: 'danger',
onClick: item => {
setIsModalVisible(true)
setItemsSelectedForDelete([item.id])
}
}
]
}
]
}, [resource])
Expand Down Expand Up @@ -431,12 +440,16 @@ export const Table: React.FunctionComponent<TableProps> = ({
columns={columns}
hasActions={true}
loading={loading}
selection={{
selectable: () => true,
onSelectionChange: setSelectedItems,
selectableMessage: selectable =>
selectable ? '' : 'Cannot select this product.'
}}
selection={
hideSelection
? undefined
: {
selectable: () => true,
onSelectionChange: setSelectedItems,
selectableMessage: selectable =>
selectable ? '' : 'Cannot select this product.'
}
}
isSelectable={true}
sorting={{
sort: {
Expand Down

0 comments on commit ce8439f

Please sign in to comment.