Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getChangeHistory #805

Merged
merged 1 commit into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/components/edit/RecentChangeHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Link from 'next/link'
import { PlusIcon, UserCircleIcon, MinusIcon, PencilIcon, PencilSquareIcon, MinusCircleIcon, PlusCircleIcon } from '@heroicons/react/24/outline'
import { formatDistanceToNow } from 'date-fns'

import { ChangesetType, ChangeType, AreaType, ClimbType } from '../../js/types'
import { ChangesetType, ChangeType, AreaType, ClimbType, OrganizationType, DocumentTypeName } from '../../js/types'

export interface RecentChangeHistoryProps {
history: ChangesetType[]
Expand Down Expand Up @@ -43,6 +43,7 @@ const ChangesetRow = ({ changeset }: ChangsetRowProps): JSX.Element => {
<React.Fragment key={change.changeId}>
<AreaChange {...change} />
<ClimbChange {...change} />
<OrganizationChange {...change} />
</React.Fragment>))}
</div>
</div>
Expand All @@ -51,7 +52,7 @@ const ChangesetRow = ({ changeset }: ChangsetRowProps): JSX.Element => {
}

const ClimbChange = ({ changeId, fullDocument, updateDescription, dbOp }: ChangeType): JSX.Element | null => {
if ((fullDocument as ClimbType)?.name == null) {
if (fullDocument.__typeName !== DocumentTypeName.Climb) {
Copy link
Contributor Author

@zichongkao zichongkao Apr 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More robust to use __typename than unique document fields: https://www.apollographql.com/docs/apollo-server/schema/unions-interfaces/#querying-a-union

return null
}
const { name, id } = fullDocument as ClimbType
Expand All @@ -76,9 +77,7 @@ const ClimbChange = ({ changeId, fullDocument, updateDescription, dbOp }: Change
}

const AreaChange = ({ changeId, fullDocument, updateDescription, dbOp }: ChangeType): JSX.Element | null => {
// @ts-expect-error
// eslint-disable-next-line
if (fullDocument?.areaName == null) {
if (fullDocument.__typeName !== DocumentTypeName.Area) {
return null
}
const { areaName, uuid } = fullDocument as AreaType
Expand All @@ -103,6 +102,28 @@ const AreaChange = ({ changeId, fullDocument, updateDescription, dbOp }: ChangeT
)
}

const OrganizationChange = ({ changeId, fullDocument, updateDescription, dbOp }: ChangeType): JSX.Element | null => {
if (fullDocument.__typeName !== DocumentTypeName.Organization) {
return null
}
const { displayName } = fullDocument as OrganizationType

return (
<div className='ml-2 flex gap-x-2'>
<div className='flex gap-2'>{dbOpIcon[dbOp]} <span className='badge badge-sm badge-warning'>Organization</span></div>

<div className=''>
<div className=''>
<span>{displayName}</span>
</div>
<div className='text-xs text-base-300'>
<UpdatedFields fields={updateDescription?.updatedFields} />
</div>
</div>
</div>
)
}

interface UpdatedFieldsProps {
fields: string[] | undefined
}
Expand Down
4 changes: 4 additions & 0 deletions src/js/graphql/gql/contribs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const FRAGMENT_CHANGE_HISTORY = gql`
updatedFields
}
fullDocument {
__typename
... on Area {
areaName
uuid
Expand All @@ -112,6 +113,9 @@ export const FRAGMENT_CHANGE_HISTORY = gql`
name
uuid
}
... on Organization {
displayName
}
}
}
}`
Expand Down
32 changes: 31 additions & 1 deletion src/js/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export enum SafetyType {
X = 'X',
}

export enum OrgType {
LOCAL_CLIMBING_ORGANIZATION = 'LOCAL_CLIMBING_ORGANIZATION',
}

export interface ClimbMetadataType {
lat: number
lng: number
Expand Down Expand Up @@ -112,6 +116,22 @@ export interface EditMetadataType {
createdBy?: string
}

export type OrganizationType = EditMetadataType & {
orgId: string
orgType: OrgType
associatedAreaIds: string[]
excludedAreaIds: string[]
displayName: string
content: {
facebookLink: string
instagramLink: string
donationLink: string
description: string
website: string
email: string
}
}

export type AreaType = EditMetadataType & {
id: string
uuid: string
Expand Down Expand Up @@ -282,10 +302,20 @@ export interface UpdateDescriptionType {
truncatedArrays?: any[]
}

export enum DocumentTypeName {
Area = 'Area',
Climb = 'Climb',
Organization = 'Organization',
}

export interface WithDocumentTypeName {
__typeName: DocumentTypeName
}

export interface ChangeType {
dbOp: string
changeId: string
fullDocument: AreaType | ClimbType
fullDocument: (AreaType | ClimbType | OrganizationType) & WithDocumentTypeName
updateDescription: UpdateDescriptionType
}

Expand Down