Skip to content

Commit

Permalink
Template filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
thecalcc committed Oct 31, 2023
1 parent 087f8c2 commit f46f87e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 7 additions & 0 deletions scripts/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ function hasPrivilege(privilege: string): boolean {
return privileges.userHasPrivileges({[privilege]: 1});
}

function getCurrentUserId(): string {
const session = ng.get('session');

return session?.identity?._id;
}

function isLoggedIn() {
const session = ng.get('session');

Expand All @@ -15,4 +21,5 @@ function isLoggedIn() {
export const user = {
hasPrivilege,
isLoggedIn,
getCurrentUserId,
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {IRestApiResponse, ITemplate} from 'superdesk-api';
import {httpRequestJsonLocal} from 'core/helpers/network';
import {DropdownOption} from './dropdown-option';
import {nameof} from 'core/helpers/typescript-helpers';
import {sdApi} from 'api';

interface IProps {
onSelect(template: ITemplate): void;
Expand Down Expand Up @@ -47,6 +48,9 @@ export class MoreTemplates extends React.PureComponent<IProps, IState> {
}

render() {
const currentUserId = sdApi.user.getCurrentUserId();
const currentDeskId = sdApi.desks.getCurrentDeskId();

return (
<div
style={{
Expand Down Expand Up @@ -87,7 +91,22 @@ export class MoreTemplates extends React.PureComponent<IProps, IState> {
<WithPagination
key={this.state.searchString}
getItems={(pageNo, pageSize, signal) => this.fetchData(pageNo, pageSize, signal)
.then((res) => ({items: res._items, itemCount: res._meta.total}))
.then((res) => {
/**
* The check for `template.template_desks == null` is needed
* because the pre-populated templates should be available
* on all desks. The user can't create templates with null `template_desks`.
*/
const templatesFiltered = res._items.filter((template) => (
(template.is_public || template.user === currentUserId)
&& (template.template_desks == null || template.template_desks.includes(currentDeskId))
));

return {
items: templatesFiltered,
itemCount: templatesFiltered.length,
};
})
}
>
{
Expand Down

0 comments on commit f46f87e

Please sign in to comment.