2017-07-26 07:35:38 +09:00
/ *
2018-01-10 18:31:58 +09:00
components / MoviesList . jsx # tutorial - step - 7 -
The component for our list of movies , which we called in to modules / routes . js .
2017-07-26 07:35:38 +09:00
Wrapped with the "withList" and "withCurrentUser" containers .
2018-01-10 18:31:58 +09:00
# tutorial - step - 11 -
Now , we are going to look at this in a bit more detail .
This component is a React component . We only have one but it does a bunch of things ...
2017-07-26 07:35:38 +09:00
* /
import React from 'react' ;
2017-07-29 16:26:34 +09:00
import Helmet from 'react-helmet' ;
2017-09-13 16:26:52 -05:00
import { Components , withList , withCurrentUser , registerComponent } from 'meteor/vulcan:core' ;
2017-07-26 07:35:38 +09:00
import Movies from '../../modules/movies/collection.js' ;
2018-01-10 18:31:58 +09:00
{
/ * T h e s e a r e " p r o p s " . T h e y a r e v a r i a b l e s f o r t h e c o m p o n e n t t h a t a r e p a s s e d b y t h e c o m p o n e n t s p a r e n t .
In this case , to create the parent we wrapped the component in "Higer Order Compoents" ( See the Higer Order Compoents section below . )
By doing this , we can pass on those props to the children of he HOCs and give them access to the props ... * /
}
const MoviesList = ( {
results = [ ] ,
currentUser ,
loading ,
loadMore ,
count ,
totalCount
} ) => (
< div style = { { maxWidth : "500px" , margin : "20px auto" } } >
{ / * F i r s t , t h i s i s a H e l m e n t t a g . I t ' s a R e a c t p a c k a g e t h a t l o a d s h e a d t a g s . W e ' r e u s i n g i t t o l o a d t h e B o o t s t r a p s t y l e s h e e t .
This is not Vulcan specific but it is an easy way to add tags to the head ... * / }
2017-07-29 16:26:34 +09:00
< Helmet >
2018-01-10 18:31:58 +09:00
< link
name = "bootstrap"
rel = "stylesheet"
type = "text/css"
href = "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css"
/ >
2017-07-29 16:26:34 +09:00
< / Helmet >
2017-07-26 07:35:38 +09:00
{ /* user accounts */ }
2018-01-10 18:31:58 +09:00
< div
style = { {
padding : "20px 0" ,
marginBottom : "20px" ,
borderBottom : "1px solid #ccc"
} }
>
{ / * . . . T h i s i s t h e l o g i n f o r m c o m p o n e n t . I t a l l o w e d y o u t o c r e a t e a n a c c o u n t i n o u r w e b a p p .
It takes care of all accounts stuff like changing passwords , signing in and out , and so on
Just pop this in anywhere you want to use it . It ' s in the Vulcan accounts package ... * / }
2017-07-26 07:35:38 +09:00
< Components.AccountsLoginForm / >
< / div >
2018-01-10 18:31:58 +09:00
{ /* ... We have a test for the loding variable (See the "Higher Order Components" section at the bottom and then the "props" section at the top.)... */ }
{ loading ? (
< Components.Loading / >
) : (
2017-07-26 07:35:38 +09:00
< div className = "movies" >
2018-01-10 18:31:58 +09:00
{ / * n e w d o c u m e n t f o r m - t h i s i f f o r i n s e r t i n g n e w d o c u m e n t s . B e c a u s e t h e c o l l e c t i o n h a s t h e s c h e m a , w h e n w e g e n e r a t e t h e f o r m , i t k n o w w h a t t h e c o l l e c i t o n s h o u l d l o o k l i k e
You only need to specify the colleciton . You don ' t need to code any of the form . Validation will work and it will show you fields based on your user permission ... * / }
{ Movies . options . mutations . new . check ( currentUser ) ? (
< div
style = { {
marginBottom : "20px" ,
paddingBottom : "20px" ,
borderBottom : "1px solid #ccc"
} }
>
2017-07-26 07:35:38 +09:00
< h4 > Insert New Document < / h4 >
2018-01-10 18:31:58 +09:00
< Components.SmartForm collection = { Movies } / >
< / div >
) : null }
{ /* documents list - this is another small utility in Vulcan and it will display it as a card... */ }
{ results . map ( movie => (
< Components.Card
fields = { [ "name" , "year" , "review" ] }
key = { movie . _id }
collection = { Movies }
document = { movie }
currentUser = { currentUser }
/ >
) ) }
{ /* load more - this is the load more button. On click we trigger the loadMore function which is passed as a prop by withList... */ }
{ totalCount > results . length ? (
< a
href = "#"
onClick = { e => {
e . preventDefault ( ) ;
loadMore ( ) ;
} }
>
Load More ( { count } / { totalCount } )
< / a >
) : (
2017-07-26 07:35:38 +09:00
< p > No more items . < / p >
2018-01-10 18:31:58 +09:00
) }
2017-07-26 07:35:38 +09:00
< / div >
2018-01-10 18:31:58 +09:00
) }
2017-07-26 07:35:38 +09:00
< / div >
2018-01-10 18:31:58 +09:00
) ;
2017-07-26 07:35:38 +09:00
2018-01-10 18:31:58 +09:00
// ...this is where we specify how to load the data in the options object that we pass to withList
// if you want, you can specify many more options here, like how often to look for data or what fields to query from, filtering and sorting options. ...
2017-07-26 07:35:38 +09:00
const options = {
collection : Movies ,
limit : 5 ,
} ;
2018-01-10 18:31:58 +09:00
// These two functions (withList & withCurrentUser) are Higher Order Components (HOC) and by wrapping our component with this we can give it "props". (See the "props" section at the top.)
2017-09-13 16:26:52 -05:00
registerComponent ( 'MoviesList' , MoviesList , withCurrentUser , [ withList , options ] ) ;
2018-01-10 18:31:58 +09:00
// #tutorial-step-12 - Well. that's it! If you like this, go on to the movies-example, where we get more granular when it comes to data loading.
// Well thanks for tuning in! Come visit us at our chat room at slack.vulcanjs.org. See you soon!