Skip to content

Commit

Permalink
Merge pull request #248 from thematters/enhancement/save-to-update-co…
Browse files Browse the repository at this point in the history
…llection

Collection Editor Updates
  • Loading branch information
robertu7 authored Apr 30, 2019
2 parents 49c9754 + b1fb4b0 commit 35adc91
Show file tree
Hide file tree
Showing 11 changed files with 440 additions and 405 deletions.
4 changes: 2 additions & 2 deletions components/CollectionEditor/CollectForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { translate } from '~/common/utils'
import styles from './styles.css'

interface Props {
onAdd: (articleId: string) => void
onAdd: (article: SearchArticles_search_edges_node_Article) => void
}

const debouncedSetSearch = _debounce((value, setSearch) => {
Expand Down Expand Up @@ -70,7 +70,7 @@ const CollectForm: FC<Props> = ({ onAdd }) => {
onClick={(
article: SearchArticles_search_edges_node_Article
) => {
onAdd(article.id)
onAdd(article)
setSearch('')
hideDropdown()
}}
Expand Down
25 changes: 11 additions & 14 deletions components/CollectionEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ import styles from './styles.css'

interface State {
articles: DropdownDigestArticle[]
prevArticleIds: string[]
}

interface Props {
articles: DropdownDigestArticle[]
onEdit: (articleIds: string[]) => void
onEdit: (articles: DropdownDigestArticle[]) => void
}

const reorder = (list: any[], startIndex: number, endIndex: number) => {
Expand All @@ -41,30 +40,28 @@ class CollectionEditor extends React.PureComponent<Props, State> {
super(props)

this.state = {
articles: this.props.articles,
prevArticleIds: this.props.articles.map(({ id }) => id)
articles: this.props.articles
}
}

componentDidUpdate() {
const { prevArticleIds } = this.state
const { articles } = this.state
const prevArticleIds = articles.map(({ id }) => id)
const articleIds = this.props.articles.map(({ id }) => id)

if (_isEqual(prevArticleIds, articleIds)) {
return
}

this.setState({ articles: this.props.articles, prevArticleIds: articleIds })
this.setState({ articles: this.props.articles })
}

onAdd = (articleId: string) => {
const prevArticleIds = this.state.articles.map(({ id }) => id)
this.props.onEdit([...prevArticleIds, articleId])
onAdd = (article: any) => {
this.props.onEdit([...this.state.articles, article])
}

onDelete = (articleId: string) => {
const prevArticleIds = this.state.articles.map(({ id }) => id)
this.props.onEdit(prevArticleIds.filter(id => id !== articleId))
onDelete = (article: any) => {
this.props.onEdit(this.state.articles.filter(({ id }) => id !== article.id))
}

onDragEnd = (result: DropResult) => {
Expand All @@ -85,7 +82,7 @@ class CollectionEditor extends React.PureComponent<Props, State> {
result.destination.index
)
this.setState({ articles: newItems })
this.props.onEdit(newItems.map(({ id }) => id))
this.props.onEdit(newItems)
}

render() {
Expand Down Expand Up @@ -132,7 +129,7 @@ class CollectionEditor extends React.PureComponent<Props, State> {
type="button"
className="delete-handler"
aria-label="刪除"
onClick={() => this.onDelete(article.id)}
onClick={() => this.onDelete(article)}
>
<Icon
id={ICON_DELETE_BLACK_CIRCLE.id}
Expand Down
3 changes: 1 addition & 2 deletions components/NoticeDigest/ArticleNewCollectedNotice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const ArticleNewCollectedNotice = ({ notice }: { notice: NoticeType }) => {
const avatarWrapClasses = classNames({
'avatar-wrap': true
})
console.log(notice)

return (
<section className="container">
Expand All @@ -34,7 +33,7 @@ const ArticleNewCollectedNotice = ({ notice }: { notice: NoticeType }) => {
<NoticeActorName user={notice.actor} />{' '}
<Translate zh_hant="在其作品" zh_hans="在其作品" />{' '}
<NoticeArticle article={notice.collection} />{' '}
<Translate zh_hant="中關聯推薦" zh_hans="中关联推荐" />
<Translate zh_hant="中關聯推薦" zh_hans="中关联推荐" />
</h4>

<NoticeDate notice={notice} />
Expand Down
151 changes: 151 additions & 0 deletions views/ArticleDetail/Collection/CollectionList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import gql from 'graphql-tag'
import _get from 'lodash/get'
import _uniq from 'lodash/uniq'
import { QueryResult } from 'react-apollo'

import { ArticleDigest, Icon, Spinner, TextIcon, Translate } from '~/components'
import { Query } from '~/components/GQL'

import { ANALYTICS_EVENTS, FEED_TYPE } from '~/common/enums'
import { analytics, mergeConnections } from '~/common/utils'
import ICON_ADD from '~/static/icons/add.svg?sprite'
import ICON_MORE_CONTENT from '~/static/icons/more-content.svg?sprite'

import { ArticleDetail_article } from '../__generated__/ArticleDetail'
import { SidebarCollection } from './__generated__/SidebarCollection'
import styles from './styles.css'

export const SIDEBAR_COLLECTION = gql`
query SidebarCollection($mediaHash: String, $cursor: String, $first: Int) {
article(input: { mediaHash: $mediaHash }) {
id
collection(input: { after: $cursor, first: $first })
@connection(key: "articleCollection") {
pageInfo {
startCursor
endCursor
hasNextPage
}
totalCount
edges {
cursor
node {
...PlainDigestArticle
}
}
}
}
}
${ArticleDigest.Plain.fragments.article}
`

const CollectionList = ({
article,
setEditing,
canEdit
}: {
article: ArticleDetail_article
setEditing: (editing: boolean) => void
canEdit?: boolean
}) => (
<Query
query={SIDEBAR_COLLECTION}
variables={{ mediaHash: article.mediaHash, first: 10 }}
>
{({
data,
loading,
error,
fetchMore
}: QueryResult & { data: SidebarCollection }) => {
if (loading) {
return <Spinner />
}

const path = 'article.collection'
const { edges, pageInfo, totalCount } = _get(data, path, {})
const loadRest = () =>
fetchMore({
variables: {
mediaHash: article.mediaHash,
cursor: pageInfo.endCursor,
first: null
},
updateQuery: (previousResult, { fetchMoreResult }) =>
mergeConnections({
oldData: previousResult,
newData: fetchMoreResult,
path
})
})

if (totalCount <= 0 && canEdit) {
return (
<button type="button" onClick={() => setEditing(true)}>
<TextIcon
icon={
<Icon
id={ICON_ADD.id}
viewBox={ICON_ADD.viewBox}
size="xsmall"
/>
}
spacing="xtight"
color="green"
size="sm"
>
<Translate zh_hant="關聯一篇作品" zh_hans="关联一篇作品" />
</TextIcon>
</button>
)
}

return (
<>
<ol>
{edges.map(
({ node, cursor }: { node: any; cursor: any }, i: number) => (
<li
key={cursor}
onClick={() =>
analytics.trackEvent(ANALYTICS_EVENTS.CLICK_COLLECTION, {
type: FEED_TYPE.COLLECTION,
location: i
})
}
>
<ArticleDigest.Plain article={node} hasArchivedTooltip />
</li>
)
)}
</ol>

{pageInfo.hasNextPage && (
<section className="load-more">
<button type="button" onClick={loadRest}>
<TextIcon
icon={
<Icon
id={ICON_MORE_CONTENT.id}
viewBox={ICON_MORE_CONTENT.viewBox}
/>
}
color="green"
size="sm"
textPlacement="left"
spacing="xxtight"
>
<Translate zh_hans="查看全部" zh_hant="查看全部" />
</TextIcon>
</button>
</section>
)}

<style jsx>{styles}</style>
</>
)
}}
</Query>
)

export default CollectionList
Loading

0 comments on commit 35adc91

Please sign in to comment.