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

Feat/added isVisible to page props #1262

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src/adminjs-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TransformOptions as BabelConfig } from 'babel-core'

import BaseResource from './backend/adapters/resource/base-resource'
import BaseDatabase from './backend/adapters/database/base-database'
import { PageContext } from './backend/actions/action.interface'
import { IsFunction, PageContext } from './backend/actions/action.interface'
import { ResourceOptions } from './backend/decorators/resource/resource-options.interface'
import { Locale } from './locale/config'
import { CurrentAdmin } from './current-admin.interface'
Expand Down Expand Up @@ -389,6 +389,11 @@ export type AdminPage = {
* Page icon
*/
icon?: string;

/**
* Page visibility
*/
isVisible?: boolean | IsFunction
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/components/actions/action.props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ export type ActionProps = {
* Sets tag in a header of an action. It is a function taking tag as an argument
*/
setTag?: Dispatch<SetStateAction<string>>;
}
}
8 changes: 8 additions & 0 deletions src/frontend/interfaces/page-json.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { IsFunction } from 'src/backend'
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry I haven't noticed this earlier. Can you change the import path to be relative (not start with src/)?

import { ActionJSON } from './action'

/**
* Representing the page in the sidebar
* @subcategory Frontend
Expand All @@ -16,4 +19,9 @@ export interface PageJSON {
* Page icon
*/
icon?: string;

/**
* Page visibility
*/
isVisible: boolean;
}
23 changes: 17 additions & 6 deletions src/frontend/store/pages-to-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@ import { AdminJSOptions } from '../../adminjs-options.interface'

import { PageJSON } from '../interfaces'

const pagesToStore = (pages: AdminJSOptions['pages'] = {}): Array<PageJSON> => Object.entries(pages)
.map(([key, adminPage]) => ({
name: key,
component: adminPage.component,
icon: adminPage.icon,
}))
const pagesToStore = (pages: AdminJSOptions['pages'] = {}): Array<PageJSON> =>
Object.entries(pages).map(([key, adminPage]) => {
let isVisible = true;
if (adminPage.isVisible) {
if (typeof adminPage.isVisible === 'function') {
isVisible = adminPage.isVisible({ currentAdmin })
} else {
isVisible = adminPage.isVisible
}
}
return {
name: key,
component: adminPage.component,
icon: adminPage.icon,
isVisible,
}
});

export default pagesToStore