mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 10:01:40 -05:00
Call Utils.convertDates when loading and sorting data to convert any date field stored as a string into an actual date object
This commit is contained in:
parent
2707b8348d
commit
e9bc54426f
3 changed files with 31 additions and 10 deletions
|
@ -28,7 +28,7 @@ export default function withDocument (options) {
|
|||
const { ownProps, data } = returnedProps;
|
||||
return {
|
||||
loading: data.networkStatus === 1,
|
||||
document: data[singleResolverName],
|
||||
document: Utils.convertDates(collection, data[singleResolverName]),
|
||||
fragmentName,
|
||||
fragment,
|
||||
};
|
||||
|
|
|
@ -36,7 +36,7 @@ import React, { PropTypes, Component } from 'react';
|
|||
import { graphql } from 'react-apollo';
|
||||
import gql from 'graphql-tag';
|
||||
import update from 'immutability-helper';
|
||||
import { getSetting } from 'meteor/nova:core';
|
||||
import { getSetting, Utils } from 'meteor/nova:core';
|
||||
import Mingo from 'mingo';
|
||||
import { compose, withState } from 'recompose';
|
||||
|
||||
|
@ -80,11 +80,7 @@ const withList = (options) => {
|
|||
|
||||
// graphql query options
|
||||
options({terms, paginationTerms}) {
|
||||
console.log('terms', terms);
|
||||
console.log('paginationTerms', paginationTerms);
|
||||
const mergedTerms = {...terms, ...paginationTerms};
|
||||
console.log(mergedTerms);
|
||||
// console.log(ownProps)
|
||||
return {
|
||||
variables: {
|
||||
terms: mergedTerms,
|
||||
|
@ -104,7 +100,7 @@ const withList = (options) => {
|
|||
props(props) {
|
||||
|
||||
const refetch = props.data.refetch,
|
||||
results = props.data[listResolverName],
|
||||
results = Utils.convertDates(collection, props.data[listResolverName]),
|
||||
totalCount = props.data[totalResolverName],
|
||||
networkStatus = props.data.networkStatus;
|
||||
|
||||
|
@ -193,16 +189,17 @@ const queryReducer = (previousResults, action, collection, mergedTerms, listReso
|
|||
|
||||
// reorder results according to a sort
|
||||
const reorderResults = (results, sort) => {
|
||||
const cursor = mingoQuery.find(results[listResolverName]);
|
||||
const list = results[listResolverName];
|
||||
const convertedList = Utils.convertDates(collection, list); // convert date strings to date objects
|
||||
const cursor = mingoQuery.find(convertedList);
|
||||
const sortedList = cursor.sort(sort).all();
|
||||
// console.log('sortedList: ', sortedList)
|
||||
results[listResolverName] = sortedList;
|
||||
return results;
|
||||
}
|
||||
|
||||
// console.log('// withList reducer');
|
||||
// console.log('queryName: ', queryName);
|
||||
// console.log('terms: ', ownProps.terms);
|
||||
// console.log('terms: ', mergedTerms);
|
||||
// console.log('selector: ', selector);
|
||||
// console.log('options: ', options);
|
||||
// console.log('previousResults: ', previousResults);
|
||||
|
|
|
@ -410,3 +410,27 @@ Utils.arrayToFields = (fieldsArray) => {
|
|||
Utils.getComponentDisplayName = (WrappedComponent) => {
|
||||
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Take a collection and a list of documents, and convert all their date fields to date objects
|
||||
* This is necessary because Apollo doesn't support custom scalars, and stores dates as strings
|
||||
* @param {Object} collection
|
||||
* @param {Array} list
|
||||
*/
|
||||
Utils.convertDates = (collection, list) => {
|
||||
if (!list || !list.length) return list;
|
||||
|
||||
const schema = collection.simpleSchema()._schema;
|
||||
const dateFields = _.filter(_.keys(schema), fieldName => schema[fieldName].type === Date);
|
||||
const convertedList = list.map(result => {
|
||||
dateFields.forEach(fieldName => {
|
||||
if (result[fieldName] && typeof result[fieldName] === 'string') {
|
||||
result[fieldName] = new Date(result[fieldName]);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
});
|
||||
|
||||
return convertedList;
|
||||
}
|
Loading…
Add table
Reference in a new issue