Vulcan/packages/framework-demo/lib/components/MoviesList.jsx

77 lines
2 KiB
React
Raw Normal View History

2016-11-22 16:46:32 +09:00
/*
List of movies.
Wrapped with the "withList" and "withCurrentUser" containers.
*/
import React, { PropTypes, Component } from 'react';
import { Button } from 'react-bootstrap';
import gql from 'graphql-tag';
import Movies from '../collection.js';
import { Components, registerComponent, ModalTrigger, withList, withCurrentUser } from 'meteor/nova:core';
const LoadMore = props => <a href="#" className="load-more button button--primary" onClick={e => {e.preventDefault(); props.loadMore();}}>Load More ({props.count}/{props.totalCount})</a>
class MoviesList extends Component {
renderNew() {
const component = (
<div className="add-movie">
<ModalTrigger
title="Add Movie"
component={<Button bsStyle="primary">Add Movie</Button>}
>
<Components.MoviesNewForm />
</ModalTrigger>
<hr/>
</div>
)
return !!this.props.currentUser ? component : null;
}
render() {
const canCreateNewMovie = Movies.options.mutations.new.check(this.props.currentUser);
2016-11-25 11:46:31 +09:00
if (this.props.loading) {
return <Components.Loading />
} else {
2016-11-19 18:14:33 +09:00
const hasMore = this.props.totalCount > this.props.results.length;
return (
<div className="movies">
{canCreateNewMovie ? this.renderNew() : null}
{this.props.results.map(movie => <Components.MoviesItem key={movie._id} {...movie} currentUser={this.props.currentUser} refetch={this.props.refetch} />)}
2016-11-19 18:14:33 +09:00
{hasMore ? <LoadMore {...this.props}/> : <p>No more movies</p>}
</div>
)
}
}
}
MoviesList.displayName = 'MoviesList';
export const MoviesListFragment = gql`
fragment moviesItemFragment on Movie {
_id
name
year
createdAt
user {
displayName
}
}
`;
2016-11-19 19:00:17 +09:00
const listOptions = {
collection: Movies,
queryName: 'moviesListQuery',
fragment: MoviesListFragment,
limit: 5,
2016-11-19 19:00:17 +09:00
};
registerComponent('MoviesList', MoviesList, withList(listOptions), withCurrentUser);