Skip to content

Commit

Permalink
Restore fast-json-patch to Offices
Browse files Browse the repository at this point in the history
And clean up some fragment stuff. Need theContacts, theProjects, contacts, projects.

We need `fast-json-patch` because of this brokenness:
cujojs/jiff#42
  • Loading branch information
kevinashworth committed Jul 6, 2020
1 parent b9765a2 commit 8370366
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 56 deletions.
22 changes: 12 additions & 10 deletions packages/v8/lib/components/patches/OfficeDisplay.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 (
Expand All @@ -46,18 +47,19 @@ const OfficeDisplay = ({ office }) => {
<Card.Title className='mt-5'><b>Notes</b></Card.Title>}
{office.htmlBody &&
<Interweave content={office.htmlBody} transform={transformLinks} />}
{office.theContacts && office.theContacts.length > 0 &&
{office.contacts && office.contacts.length > 0 &&
<Card.Title className='mt-5'><b>Contacts</b></Card.Title>}
{office.theContacts &&
office.theContacts.length > 0 &&
office.theContacts.map((o, index) => <Components.ContactMini key={`ContactMini${index}`} documentId={o._id} />)}
{office.contacts &&
office.contacts.length > 0 &&
office.contacts.map((o, index) => <Components.ContactMini key={`ContactMini${index}`} documentId={o.contactId} />)}
<Components.ErrorBoundary>
{office.theProjects &&
{office.projects && office.projects.length > 0 &&
<Card.Title className='mt-5'><b>Projects</b></Card.Title>}
{office.theProjects &&
<Flextest>
{office.theProjects.map((o, index) => <Components.ProjectMini key={`ProjectMini-${index}`} documentId={o._id} />)}
</Flextest>}
{office.projects &&
office.projects.length > 0 &&
<Flextest>
{office.projects.map((o, index) => <Components.ProjectMini key={`ProjectMini-${index}`} documentId={o.projectId} />)}
</Flextest>}
</Components.ErrorBoundary>
{office.links &&
<Card.Text className='mt-5'>
Expand Down
14 changes: 5 additions & 9 deletions packages/v8/lib/components/patches/OfficePatch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -19,17 +19,14 @@ const OfficePatchDisplay = (props) => {
if (!office) {
return <FormattedMessage id='patches.missing_document' />
}
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 <OfficeDisplay office={patchedOffice} />
}
Expand All @@ -41,7 +38,6 @@ OfficePatchDisplay.propTypes = {
const OfficePatch = ({ office, patch }) => {
const [collapseIsOpen, toggleCollapse] = useState(false)
const toggle = () => toggleCollapse(!collapseIsOpen)

if (!office) {
return <div>No History (OfficePatch)</div>
}
Expand All @@ -50,7 +46,7 @@ const OfficePatch = ({ office, patch }) => {
<div>
<Button variant='secondary' onClick={toggle} className='mb-1'>Toggle {moment(patch.date).format(DATE_FORMAT_LONG)} Version</Button>
<Collapse isOpen={collapseIsOpen}>
<OfficePatchDisplay office={office} patch={patch} collapseIsOpen={collapseIsOpen} />
<OfficePatchDisplay office={office} patch={patch.patch} collapseIsOpen={collapseIsOpen} />
</Collapse>
</div>
)
Expand Down
56 changes: 25 additions & 31 deletions packages/v8/lib/modules/offices/fragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
`)
12 changes: 6 additions & 6 deletions packages/v8/lib/server/offices/callbacks/patches.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 8370366

Please sign in to comment.