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