forked from superdesk/superdesk-planning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Templates modal implementation (superdesk#1909)
- Loading branch information
Showing
6 changed files
with
223 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
client/components/PlanningTemplatesModal/PlanningTemplatesModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import {ICalendar, IEventTemplate} from '../../interfaces'; | ||
import React from 'react'; | ||
import {SearchBar, Modal, Dropdown} from 'superdesk-ui-framework/react'; | ||
import {superdeskApi} from '../../superdeskApi'; | ||
import {TemplatesListView} from './TemplatesListView'; | ||
|
||
interface IProps { | ||
closeModal: () => void; | ||
calendars: Array<ICalendar>; | ||
eventTemplates: Array<IEventTemplate>; | ||
createEventFromTemplate(template: IEventTemplate): void; | ||
} | ||
|
||
interface IState { | ||
activeCalendarFilter?: string; | ||
searchQuery: string; | ||
} | ||
|
||
export default class PlanningTemplatesModal extends React.Component<IProps, IState> { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
activeCalendarFilter: null, | ||
searchQuery: '', | ||
}; | ||
} | ||
|
||
render(): React.ReactNode { | ||
const {gettext} = superdeskApi.localization; | ||
const {closeModal, createEventFromTemplate, calendars, eventTemplates} = this.props; | ||
const allCalendarsLabel = gettext('All Calendars'); | ||
const calendarDropdownItems = []; | ||
const activeCalendarName = this.props.calendars | ||
.find((cal) => cal.qcode === this.state.activeCalendarFilter)?.name; | ||
const dropdownLabel = this.state.activeCalendarFilter | ||
? gettext('Calendar: {{activeCalendarName}}', {activeCalendarName}) | ||
: allCalendarsLabel; | ||
|
||
if (this.state.activeCalendarFilter) { | ||
calendarDropdownItems.push({ | ||
label: allCalendarsLabel, | ||
onSelect: () => { | ||
this.setState({ | ||
activeCalendarFilter: null, | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
if ((calendars?.length ?? 0) > 0) { | ||
calendarDropdownItems.push( | ||
...calendars.map((calendar) => ({ | ||
label: calendar.name, | ||
onSelect: () => { | ||
this.setState({ | ||
activeCalendarFilter: calendar.qcode, | ||
}); | ||
} | ||
})) | ||
); | ||
} | ||
|
||
return ( | ||
<Modal | ||
headerTemplate={gettext('Planning templates')} | ||
visible | ||
contentPadding="medium" | ||
contentBg="medium" | ||
size="medium" | ||
onHide={closeModal} | ||
> | ||
<div className="modal__sticky-header"> | ||
<SearchBar | ||
value={this.state.searchQuery} | ||
onSubmit={(value: string) => { | ||
this.setState({ | ||
searchQuery: value, | ||
}); | ||
}} | ||
placeholder={gettext('Search templates')} | ||
boxed | ||
> | ||
<Dropdown | ||
maxHeight={300} | ||
append | ||
zIndex={2001} | ||
items={calendarDropdownItems} | ||
> | ||
{dropdownLabel} | ||
</Dropdown> | ||
</SearchBar> | ||
<TemplatesListView | ||
calendars={calendars} | ||
createEventFromTemplate={createEventFromTemplate} | ||
eventTemplates={eventTemplates} | ||
searchQuery={this.state.searchQuery} | ||
activeCalendarFilter={this.state.activeCalendarFilter} | ||
closeModal={closeModal} | ||
/> | ||
</div> | ||
</Modal> | ||
); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
client/components/PlanningTemplatesModal/TemplatesListView.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import {ICalendar, IEventTemplate} from 'interfaces'; | ||
import React from 'react'; | ||
import {gettext} from 'superdesk-core/scripts/core/utils'; | ||
import {Heading, BoxedList, BoxedListItem} from 'superdesk-ui-framework/react'; | ||
|
||
type ITemplatesListViewProps = { | ||
closeModal: () => void; | ||
eventTemplates: Array<IEventTemplate>; | ||
calendars: Array<ICalendar>; | ||
activeCalendarFilter?: string; | ||
searchQuery: string; | ||
createEventFromTemplate: (template: IEventTemplate) => void; | ||
} | ||
|
||
export const TemplatesListView: React.FC<ITemplatesListViewProps> = ({ | ||
eventTemplates, | ||
calendars, | ||
closeModal, | ||
activeCalendarFilter, | ||
searchQuery, | ||
createEventFromTemplate, | ||
}: ITemplatesListViewProps) => { | ||
const searchQueryTemplateMatches = eventTemplates | ||
.filter((template) => template.template_name.includes(searchQuery)); | ||
const calendarsFiltered = activeCalendarFilter | ||
? [calendars.find(({qcode}) => activeCalendarFilter === qcode)] | ||
: calendars; | ||
|
||
const filteredTemplates = calendarsFiltered | ||
.map((_calendar) => ({ | ||
calendar: _calendar, | ||
templates: searchQueryTemplateMatches | ||
.filter((template) => template.data.calendars.find(({qcode}) => qcode === _calendar.qcode)), | ||
})) | ||
.filter((group) => activeCalendarFilter | ||
? group.calendar.qcode === activeCalendarFilter | ||
: group.templates.length > 0 | ||
); | ||
|
||
return ( | ||
<> | ||
{filteredTemplates.map(({calendar, templates}) => ( | ||
<React.Fragment key={calendar.qcode}> | ||
<Heading type="h6" className="mt-2 mb-1">{calendar.name}</Heading> | ||
{ | ||
templates?.length > 0 ? ( | ||
<BoxedList> | ||
{templates.map((template) => ( | ||
<BoxedListItem | ||
key={template._id} | ||
clickable={true} | ||
onClick={() => { | ||
createEventFromTemplate(template); | ||
closeModal(); | ||
}} | ||
> | ||
{template.template_name} | ||
</BoxedListItem> | ||
))} | ||
</BoxedList> | ||
) : ( | ||
<BoxedListItem clickable={false}> | ||
{gettext('No templates available in this calendar group.')} | ||
</BoxedListItem> | ||
) | ||
} | ||
|
||
</React.Fragment> | ||
))} | ||
{activeCalendarFilter == null && searchQuery && filteredTemplates.length === 0 && ( | ||
<div className="mt-2 mb-1"> | ||
<BoxedListItem clickable={false}> | ||
{gettext('No templates found.')} | ||
</BoxedListItem> | ||
</div> | ||
)} | ||
</> | ||
); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters