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

69 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 Telescope from 'meteor/nova:lib';
import React, { PropTypes, Component } from 'react';
import NovaForm from "meteor/nova:forms";
import { Button } from 'react-bootstrap';
import { ModalTrigger } from "meteor/nova:core";
import MoviesItem from './MoviesItem.jsx';
import Movies from '../collection.js';
2016-11-22 16:46:32 +09:00
import MoviesNewForm from './MoviesNewForm.jsx';
import { compose } from 'react-apollo';
2016-11-29 14:08:24 +01:00
import { withList } from 'meteor/nova:core';
import { withCurrentUser } from 'meteor/nova:users';
const LoadMore = props => <a href="#" className="load-more button button--primary" onClick={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>}
>
<MoviesNewForm refetch={this.props.refetch}/>
</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) {
2016-11-26 11:17:01 +09:00
return <Telescope.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 => <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>
)
}
}
};
2016-11-19 19:00:17 +09:00
const listOptions = {
collection: Movies,
queryName: 'moviesListQuery',
2016-11-24 16:58:08 +09:00
fragmentName: MoviesItem.fragmentName,
fragment: MoviesItem.fragment,
2016-11-19 19:00:17 +09:00
};
export default compose(withList(listOptions), withCurrentUser)(MoviesList);