add query collectionListTotal (= list total count) on every collection by default, query list from the ui : getCollectionList(..) { collectionListTotal(..) collection(..) } ; withRemove "automation" based on these queries

This commit is contained in:
xavcz 2016-11-13 13:43:36 +01:00
parent 35302d291f
commit 78802d6790
13 changed files with 53 additions and 18 deletions

View file

@ -21,7 +21,10 @@ const CategoriesNewForm = (props, context) => {
const newCategoriesList = update(prev, {
categories: {
$push: [newCategory]
}
},
categoriesListTotal: {
$set: prev.categoriesListTotal + 1
},
});
// note: 'newCategoriesList' is extended with the category but somehow when the query updates it e
return newCategoriesList;

View file

@ -41,13 +41,16 @@ const CommentsNewForm = (props, context) => {
// console.log('[commentsNew] new post', newPost)
return newPost;
},
getCommentsView: (prev, { mutationResult }) => {
getCommentsList: (prev, { mutationResult }) => {
// console.log('[commentsNew] previous comment list', prev);
const newComment = mutationResult.data.commentsNew;
const newCommentsList = update(prev, {
comments: {
$push: [newComment]
}
},
commentsListTotal: {
$set: prev.commentsListTotal + 1
},
});
// console.log('[commentsNew] new comment list', newCommentsList)
return newCommentsList;

View file

@ -20,14 +20,14 @@ const PostsNewForm = (props, context) => {
mutationName="postsNew"
resultQuery={Posts.graphQLQueries.single}
updateQueries={{
getPostsView: (prev, { mutationResult }) => {
getPostsList: (prev, { mutationResult }) => {
const newPost = mutationResult.data.postsNew;
const newList = update(prev, {
posts: {
$unshift: [newPost],
},
postsViewTotal: {
$set: prev.postsViewTotal + 1
postsListTotal: {
$set: prev.postsListTotal + 1
}
});
return newList;

View file

@ -7,6 +7,7 @@ import gql from 'graphql-tag';
export default function withCategoriesList(component, options) {
return graphql(gql`
query getCategoriesList {
categoriesListTotal
categories {
...fullCategoryInfo
parent {

View file

@ -5,7 +5,8 @@ import gql from 'graphql-tag';
export default function withCommentsList (component, options) {
return graphql(gql`
query getCommentsView ($postId: String) {
query getCommentsList ($postId: String) {
commentsListTotal(postId: $postId)
comments (postId: $postId) {
_id
postId
@ -36,7 +37,7 @@ export default function withCommentsList (component, options) {
loading,
results: comments,
// loadMore() {
// // basically, rerun the query 'getPostsView' with a new offset
// // basically, rerun the query 'getPostsList' with a new offset
// return fetchMore({
// variables: { offset: posts.length },
// updateQuery(previousResults, { fetchMoreResult }) {

View file

@ -6,8 +6,8 @@ import gql from 'graphql-tag';
export default function withPostsList (component, options) {
return graphql(gql`
query getPostsView($terms: Terms, $offset: Int, $limit: Int) {
postsViewTotal(terms: $terms)
query getPostsList($terms: Terms, $offset: Int, $limit: Int) {
postsListTotal(terms: $terms)
posts(terms: $terms, offset: $offset, limit: $limit) {
${Posts.graphQLQueries.list}
}
@ -25,15 +25,15 @@ export default function withPostsList (component, options) {
},
props(props) {
const {data: {loading, posts, postsViewTotal, fetchMore}} = props;
const {data: {loading, posts, postsListTotal, fetchMore}} = props;
return {
loading,
results: posts,
totalCount: postsViewTotal,
totalCount: postsListTotal,
count: posts && posts.length,
loadMore() {
// basically, rerun the query 'getPostsView' with a new offset
// basically, rerun the query 'getPostsList' with a new offset
return fetchMore({
variables: { offset: posts.length },
updateQuery(previousResults, { fetchMoreResult }) {

View file

@ -3,6 +3,7 @@ import Categories from './collection.js';
Telescope.graphQL.addQuery(`
categories: [Category]
categoriesListTotal: Int
category(_id: String): Category
`);

View file

@ -19,6 +19,9 @@ export default resolvers = {
};
return context.Categories.find({}, options).fetch();
},
categoriesListTotal(root, args, context) {
return context.Categories.find({}).count();
},
category(root, args, context) {
return context.Categories.findOne({_id: args._id}, { fields: context.getViewableFields(context.currentUser, context.Categories) });
},

View file

@ -4,6 +4,7 @@ import Comments from './collection.js';
// declare comments queries
Telescope.graphQL.addQuery(`
comments(postId: String): [Comment]
commentsListTotal(postId: String): Int
comment(_id: String): Comment
`);

View file

@ -38,6 +38,9 @@ const resolvers = {
}
return context.Comments.find({postId: postId}, options).fetch();
},
commentsListTotal(root, {postId}, context) {
return context.Comments.find({postId: postId}).count();
},
comment(root, args, context) {
return context.Comments.findOne({_id: args._id}, { fields: context.getViewableFields(context.currentUser, context.Comments) });
},

View file

@ -1,10 +1,11 @@
import React, { Component } from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import hoistStatics from 'hoist-non-react-statics'
import hoistStatics from 'hoist-non-react-statics';
import update from 'immutability-helper';
import { getDisplayName } from './utils';
export default function withEdit(WrappedComponent, options) {
export default function withRemove(WrappedComponent, options) {
class WithRemove extends Component {
constructor(...args) {
@ -27,7 +28,25 @@ export default function withEdit(WrappedComponent, options) {
removeMutation: ({documentId}) => {
return mutate({
variables: {documentId},
// should call updateQueries here
updateQueries: {
// ex: getPostsList
[`get${Telescope.utils.camelToSpaces(collectionName)}List`]: (prev, { mutationResult }) => {
// filter the list to get a new one without the document
const listWithoutDocument = prev[collectionName].filter(doc => doc._id !== documentId);
// update the query
const newList = update(prev, {
// ex: posts
[collectionName]: {
$set: listWithoutDocument,
},
// ex: postsListTotal
[`${collectionName}ListTotal`]: {
$set: prev.postsListTotal - 1
}
});
return newList;
},
}
})
},
}),

View file

@ -3,7 +3,7 @@ import Posts from './collection.js';
Telescope.graphQL.addQuery(`
posts(terms: Terms, offset: Int, limit: Int): [Post]
postsViewTotal(terms: Terms): Int
postsListTotal(terms: Terms): Int
post(_id: String): Post
`);

View file

@ -25,7 +25,7 @@ export default resolvers = {
options.fields = context.getViewableFields(context.currentUser, context.Posts);
return context.Posts.find(selector, options).fetch();
},
postsViewTotal(root, {terms}, context) {
postsListTotal(root, {terms}, context) {
const {selector} = context.Posts.parameters.get(terms);
return context.Posts.find(selector).count();
},