Skip to content

Commit

Permalink
Merge pull request #680 from thematters/update/remove-fallback-cover
Browse files Browse the repository at this point in the history
Revise cover upload notice of `<AddCover>`
  • Loading branch information
robertu7 authored Feb 7, 2020
2 parents 36eef31 + 733e788 commit e694b69
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 57 deletions.
115 changes: 65 additions & 50 deletions src/views/Me/DraftDetail/Sidebar/AddCover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { useMutation } from '~/components/GQL'
import Collapsable from '../Collapsable'
import styles from './styles.css'

import { AddCoverDraft } from './__generated__/AddCoverDraft'
import {
AddCoverDraft,
AddCoverDraft_assets
} from './__generated__/AddCoverDraft'
import { UpdateDraftCover } from './__generated__/UpdateDraftCover'

const fragments = {
Expand All @@ -29,7 +32,7 @@ const fragments = {
}

const UPDATE_COVER = gql`
mutation UpdateDraftCover($id: ID!, $coverAssetId: ID!) {
mutation UpdateDraftCover($id: ID!, $coverAssetId: ID) {
putDraft(input: { id: $id, coverAssetId: $coverAssetId }) {
id
...AddCoverDraft
Expand All @@ -40,47 +43,62 @@ const UPDATE_COVER = gql`

const CoverList = ({
draftId,
update,
updateHeaderState,
cover,
assets
}: {
draftId: string
update: any
updateHeaderState: any
cover: string | null
assets: any
assets: AddCoverDraft_assets[]
}) => {
const uniqAssets = _uniqBy(assets, 'path') as any
return uniqAssets.map((asset: any, index: number) => {
const css = classNames({
'cover-image': true,
'cover-selected': asset.path === cover
})
return (
<div
key={asset.path}
className={css}
style={{ backgroundImage: `url(${asset.path})` }}
onClick={async () => {
updateHeaderState({ type: 'draft', state: 'saving', draftId })
try {
await update({ variables: { id: draftId, coverAssetId: asset.id } })
updateHeaderState({ type: 'draft', state: 'saved', draftId })
} catch (error) {
updateHeaderState({ type: 'draft', state: 'saveFailed', draftId })
}
}}
>
<style jsx>{styles}</style>
</div>
)
})
const [update] = useMutation<UpdateDraftCover>(UPDATE_COVER)
const { updateHeaderState } = useContext(HeaderContext)

const uniqAssets = _uniqBy(assets, 'path')

return (
<section>
{uniqAssets.map((asset, index) => {
const isSelected = asset.path === cover
const coverItemClass = classNames({
'cover-image': true,
selected: isSelected
})

return (
<section
className={coverItemClass}
style={{ backgroundImage: `url(${asset.path})` }}
role="button"
aria-label={`選擇圖 ${index + 1} 作爲作品封面`}
key={asset.path}
onClick={async () => {
updateHeaderState({ type: 'draft', state: 'saving', draftId })
try {
await update({
variables: {
id: draftId,
coverAssetId: asset.id
}
})
updateHeaderState({ type: 'draft', state: 'saved', draftId })
} catch (error) {
updateHeaderState({
type: 'draft',
state: 'saveFailed',
draftId
})
}
}}
>
<style jsx>{styles}</style>
</section>
)
})}
</section>
)
}

const AddCover = ({ draft }: { draft: AddCoverDraft }) => {
const [update] = useMutation<UpdateDraftCover>(UPDATE_COVER)
const { updateHeaderState } = useContext(HeaderContext)
const { id: draftId, cover, assets } = draft
const imageAssets = assets.filter(
({ type }: { type: string }) => type === 'embed'
Expand All @@ -93,30 +111,27 @@ const AddCover = ({ draft }: { draft: AddCoverDraft }) => {
'u-area-disable': isPending || isPublished
})

if (imageAssets.length <= 0) {
return null
}

return (
<Collapsable
title={<Translate zh_hans="封面" zh_hant="封面" />}
defaultCollapsed={!hasAssets}
defaultCollapsed={false}
>
<p className="cover-intro">
<Translate
zh_hant="選擇一張圖片作為封面"
zh_hans="选择一张图片作为封面"
/>
{hasAssets ? (
<Translate
zh_hant="選擇一張圖片作爲作品封面吧!"
zh_hans="选择一张图片作为作品封面吧!"
/>
) : (
<Translate
zh_hant="你的作品還沒有可用作封面的圖片,在文中插入一張試試吧。"
zh_hans="你的作品还没有可用作封面的图片,在文中插入一张试试吧。"
/>
)}
</p>

<section className={containerStyle}>
<CoverList
draftId={draftId}
updateHeaderState={updateHeaderState}
update={update}
cover={cover}
assets={imageAssets}
/>
<CoverList draftId={draftId} cover={cover} assets={imageAssets} />
</section>

<style jsx>{styles}</style>
Expand Down
14 changes: 7 additions & 7 deletions src/views/Me/DraftDetail/Sidebar/AddCover/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
display: flex;
align-items: center;

padding-bottom: 1rem;
overflow-x: scroll;
}

Expand All @@ -14,14 +13,15 @@
}

.cover-image {
border: 4px solid var(--color-white);
cursor: pointer;
display: inline-block;
margin-right: 1rem;
padding: 2.3125rem;
}

.cover-selected {
border: 4px solid var(--color-matters-green);
cursor: initial;
border: 4px solid var(--color-white);
cursor: pointer;

&.selected {
border-color: var(--color-matters-green);
cursor: initial;
}
}

0 comments on commit e694b69

Please sign in to comment.