From f0af4abbab5b0cb5dd9c71039a27bceb009aed42 Mon Sep 17 00:00:00 2001 From: Kevin Ashworth Date: Sun, 5 Jul 2020 18:44:11 -0700 Subject: [PATCH] Restore fast-json-patch to Offices And clean up some fragment stuff. Need theContacts, theProjects, contacts, projects. We need `fast-json-patch` because of this brokenness: https://github.com/cujojs/jiff/issues/42 --- .../lib/components/patches/OfficeDisplay.jsx | 22 ++++---- .../v8/lib/components/patches/OfficePatch.jsx | 14 ++--- packages/v8/lib/modules/offices/fragments.js | 56 +++++++++---------- .../lib/server/offices/callbacks/patches.js | 12 ++-- 4 files changed, 48 insertions(+), 56 deletions(-) diff --git a/packages/v8/lib/components/patches/OfficeDisplay.jsx b/packages/v8/lib/components/patches/OfficeDisplay.jsx index 2535c24b..8405d007 100644 --- a/packages/v8/lib/components/patches/OfficeDisplay.jsx +++ b/packages/v8/lib/components/patches/OfficeDisplay.jsx @@ -14,7 +14,7 @@ const Flextest = styled.div` flex-wrap: wrap; justify-content: flex-start; align-items: stretch; -align-content: flex-start; + align-content: flex-start; ` function PastProjects (props) { @@ -29,6 +29,7 @@ function PastProjects (props) { } const OfficeDisplay = ({ office }) => { + if (!office) return null const displayDate = 'Office as it was in the database before it was edited ' + moment(office.updatedAt).format(DATE_FORMAT_LONG) return ( @@ -46,18 +47,19 @@ const OfficeDisplay = ({ office }) => { Notes} {office.htmlBody && } - {office.theContacts && office.theContacts.length > 0 && + {office.contacts && office.contacts.length > 0 && Contacts} - {office.theContacts && - office.theContacts.length > 0 && - office.theContacts.map((o, index) => )} + {office.contacts && + office.contacts.length > 0 && + office.contacts.map((o, index) => )} - {office.theProjects && + {office.projects && office.projects.length > 0 && Projects} - {office.theProjects && - - {office.theProjects.map((o, index) => )} - } + {office.projects && + office.projects.length > 0 && + + {office.projects.map((o, index) => )} + } {office.links && diff --git a/packages/v8/lib/components/patches/OfficePatch.jsx b/packages/v8/lib/components/patches/OfficePatch.jsx index e68309f0..26133e29 100644 --- a/packages/v8/lib/components/patches/OfficePatch.jsx +++ b/packages/v8/lib/components/patches/OfficePatch.jsx @@ -5,7 +5,7 @@ import PropTypes from 'prop-types' import Button from 'react-bootstrap/Button' import Collapse from 'react-bootstrap/Collapse' import * as jsonpatch from 'fast-json-patch' -import _ from 'lodash' +import cloneDeep from 'lodash/cloneDeep' import moment from 'moment' import omitDeep from 'omit-deep' import { DATE_FORMAT_LONG } from '../../modules/constants.js' @@ -19,17 +19,14 @@ const OfficePatchDisplay = (props) => { if (!office) { return } - var clonedOffice = _.cloneDeep(omitDeep(office, ['__typename'])) - // console.log('[OfficePatchDisplay] clonedOffice:', clonedOffice) + var clonedOffice = cloneDeep(omitDeep(office, ['__typename'])) - var patchedOffice = office + var patchedOffice = null try { - patchedOffice = jsonpatch.applyPatch(clonedOffice, patch.patch).newDocument + patchedOffice = jsonpatch.applyPatch(clonedOffice, patch, false, false).newDocument } catch (e) { console.log('[OfficePatch] error:', e) } - // console.log('[OfficePatchDisplay] patch:', patch.patch) - // console.log('[OfficePatchDisplay] patchedOffice:', patchedOffice) return } @@ -41,7 +38,6 @@ OfficePatchDisplay.propTypes = { const OfficePatch = ({ office, patch }) => { const [collapseIsOpen, toggleCollapse] = useState(false) const toggle = () => toggleCollapse(!collapseIsOpen) - if (!office) { return
No History (OfficePatch)
} @@ -50,7 +46,7 @@ const OfficePatch = ({ office, patch }) => {
- +
) diff --git a/packages/v8/lib/modules/offices/fragments.js b/packages/v8/lib/modules/offices/fragments.js index b96c89bb..b785c20c 100644 --- a/packages/v8/lib/modules/offices/fragments.js +++ b/packages/v8/lib/modules/offices/fragments.js @@ -76,37 +76,8 @@ registerFragment(/* GraphQL */ ` } `) -// OfficesPatchesFragment includes only directly-editable fields, plus `htmlBody`, and `theProjects` and `theContacts` instead of `projects` and `contacts` -registerFragment(/* GraphQL */ ` - fragment OfficesPatchesFragment on Office { - displayName - body - htmlBody - links { - platformName - profileLink - profileName - } - theProjects { - _id - } - theContacts { - _id - } - addresses { - street1 - street2 - city - state - zip - addressType - } - slug - } -`) -registerFragment(/* GraphQL */ ` - fragment OfficesEditFragment on Office { - _id +registerFragment(/* GraphQL */` + fragment DirectlyEditableFields on Office { displayName body links { @@ -135,6 +106,29 @@ registerFragment(/* GraphQL */ ` zip addressType } + } +`) + +registerFragment(/* GraphQL */ ` + fragment OfficesPatchesFragment on Office { + ...DirectlyEditableFields + htmlBody + theContacts { + _id + displayName + } + theProjects { + _id + projectTitle + } + slug + } +`) + +registerFragment(/* GraphQL */ ` + fragment OfficesEditFragment on Office { + _id + ...DirectlyEditableFields slug } `) diff --git a/packages/v8/lib/server/offices/callbacks/patches.js b/packages/v8/lib/server/offices/callbacks/patches.js index 283c737a..c7f31f23 100644 --- a/packages/v8/lib/server/offices/callbacks/patches.js +++ b/packages/v8/lib/server/offices/callbacks/patches.js @@ -1,17 +1,17 @@ import { Connectors, newMutation } from 'meteor/vulcan:core' import * as jsonpatch from 'fast-json-patch' +import cloneDeep from 'lodash/cloneDeep' +import omitDeep from 'omit-deep' import Patches from '../../../modules/patches/collection.js' export function OfficeEditUpdateHistoryAfter (document, { currentUser, originalDocument }) { const objectId = originalDocument._id - // eslint-disable-next-line no-unused-vars - let myDocument, myOriginalDocument, _id, userId, createdAt, updatedAt, theContact, street, location, fullAddress, theStreet, theCity, theState, theLocation, theProjects; - ({ _id, userId, createdAt, updatedAt, theContact, street, location, fullAddress, theStreet, theCity, theState, theLocation, theProjects, ...myDocument } = document); - ({ _id, userId, createdAt, updatedAt, theContact, street, location, fullAddress, theStreet, theCity, theState, theLocation, theProjects, ...myOriginalDocument } = originalDocument); - + const doNotDiff = ['createdAt', 'updatedAt', 'theContacts', 'theProjects'] + const myDocument = cloneDeep(omitDeep(document, doNotDiff)) + const myOriginalDocument = cloneDeep(omitDeep(originalDocument, doNotDiff)) const patch = jsonpatch.compare(myDocument, myOriginalDocument, true) - if (patch.length > 0) { // If there are no differences, `compare` returns an empty array (length 0) + if (patch.length > 0) { // If there are no differences, jiff.diff returns an empty array (length 0) const patchHistory = Patches.findOne(objectId) if (patchHistory) { Connectors.update(Patches, objectId,