Skip to content

Commit

Permalink
Merge pull request #125 from adzialocha/resources-table
Browse files Browse the repository at this point in the history
Add a resources table under events
  • Loading branch information
adzialocha authored May 9, 2022
2 parents cd239cb + 099a1d5 commit 9e1c012
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
62 changes: 61 additions & 1 deletion app/scripts/views/EventsShow.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class EventsShow extends Component {
static propTypes = {
fetchResource: PropTypes.func.isRequired,
isActive: PropTypes.bool.isRequired,
isAdmin: PropTypes.bool.isRequired,
isError: PropTypes.bool.isRequired,
isLoading: PropTypes.bool.isRequired,
isVisitor: PropTypes.bool.isRequired,
Expand Down Expand Up @@ -219,6 +220,63 @@ class EventsShow extends Component {
)
}

renderResources() {
if (this.props.isLoading) {
return null
}

if (!this.props.resourceData.isOwnerMe && !this.props.isAdmin) {
return null
}

if (this.props.resourceData.resources.length === 0) {
return null
}

return (
<div>
<hr />

<strong>
{ translate('views.events.resourcesTitle') }
</strong>

<table>
<thead>
<tr>
<th>
#
</th>
<th>
{ translate('views.events.resourcesTableItem') }
</th>
<th>
{ translate('views.events.resourcesTableDescription') }
</th>
<th>
{ translate('views.events.resourcesTableOwner') }
</th>
</tr>
</thead>
<tbody>
{this.props.resourceData.resources.map((resource, index) => {
return (
<tr key={resource.id}>
<td>{index + 1}</td>
<td>{resource.title}</td>
<td>{resource.description}</td>
<td>
<AnimalLink animal={resource.animal} />
</td>
</tr>
)
})}
</tbody>
</table>
</div>
)
}

renderContent() {
if (this.props.isLoading) {
return <p>{ translate('common.loading') }</p>
Expand All @@ -239,6 +297,7 @@ class EventsShow extends Component {
{ this.renderTicketUrl() }
{ this.renderWebsiteUrl() }
{ this.renderAdditionalInfo() }
{ this.renderResources() }
</div>
)
}
Expand Down Expand Up @@ -278,13 +337,14 @@ function mapStateToProps(state, ownProps) {
const resourceSlug = ownProps.match.params.slug
const resource = cachedResource('events', resourceSlug)
const { isLoading, isError, object: resourceData } = resource
const { isVisitor, isActive } = state.user
const { isVisitor, isActive, isAdmin } = state.user

return {
isActive,
isError,
isLoading,
isVisitor,
isAdmin,
resourceData,
resourceSlug,
}
Expand Down
4 changes: 4 additions & 0 deletions common/locales/views.en.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export default {
tagSelectorTitle: 'Filter by tags',
datePickerTitle: 'Filter by date',
titlePlaceholder: 'Event',
resourcesTitle: 'Resources',
resourcesTableItem: 'Item',
resourcesTableDescription: 'Description',
resourcesTableOwner: 'Owner',
},
inbox: {
backToConversations: 'Back to inbox',
Expand Down
22 changes: 17 additions & 5 deletions server/controllers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function prepareAnimalResponse(animal, isAnonymous = true) {
name,
}

if (!isAnonymous) {
if (!isAnonymous && animal.user) {
const { user } = animal

data.userId = user.id
Expand All @@ -39,8 +39,22 @@ export function prepareAnimalResponseAll(animals, isAnonymous) {
return animals.map(animal => prepareAnimalResponse(animal, isAnonymous))
}

function filterAnimal(obj, isAnonymous) {
if (obj && typeof obj === 'object') {
Object.keys(obj).forEach(key => {
if (key === 'animal') {
obj.animal = prepareAnimalResponse(obj.animal, isAnonymous)
} else {
obj[key] = filterAnimal(obj[key], isAnonymous)
}
})
}

return obj
}

export function prepareResponse(data, req, isAnonymous) {
const response = 'toJSON' in data ? data.toJSON() : data
let response = 'toJSON' in data ? data.toJSON() : data

// Set owner flag for frontend ui
if (typeof req.isOwnerMe !== 'undefined') {
Expand All @@ -50,9 +64,7 @@ export function prepareResponse(data, req, isAnonymous) {
}

// Remove userId from animal to stay anonymous
if (response.animal) {
response.animal = prepareAnimalResponse(response.animal, isAnonymous)
}
response = filterAnimal(response, isAnonymous)

// Convert markdown to html
response.descriptionHtml = marked(response.description)
Expand Down
3 changes: 2 additions & 1 deletion server/controllers/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ const belongsToManyResources = {
attributes: { exclude: ['createdAt', 'updatedAt'] },
include: [{
association: ResourceBelongsToAnimal,
attributes: ['name', 'id'],
attributes: ['name', 'id', 'userId'],
include: AnimalBelongsToUser,
}],
}

Expand Down

0 comments on commit 9e1c012

Please sign in to comment.