diff --git a/src/components/Campaign/Editor/index.tsx b/src/components/Campaign/Editor/index.tsx index f4745a84..5807675d 100644 --- a/src/components/Campaign/Editor/index.tsx +++ b/src/components/Campaign/Editor/index.tsx @@ -2,11 +2,13 @@ import * as React from 'react' import { Mutation } from 'react-apollo' import { Button, DatePicker, Input, message } from 'antd' import gql from 'graphql-tag' +import moment from 'moment' import Uploader from './Uploader' import Section from '../../../components/DescriptionList' import Divider from '../../../components/Divider' import { CampaignDetail } from '../../../definitions' +import { PATH } from '../../../constants' const PUT_CAMPAIGN = gql` mutation PutWritingChallenge($input: PutWritingChallengeInput!) { @@ -28,6 +30,37 @@ type DetailState = { error: any } & CampaignDetail +const normalizeDescription = (description: string) => { + // remove leading and trailing

+ description = description.replace(/^(

(
)?<\/p>)+/, '') + description = description.replace(/(

(
)?<\/p>)+$/, '') + + // replace

or
with \n + description = description.replace(/<\/p>/g, '\n') + description = description.replace(/


<\/p>/g, '\n') + + // remove HTML tags + description = description.replace(/<[^>]*>/g, '') + + // remove trailing \n + description = description.replace(/\n+$/, '') + + return description +} + +const serializeDescription = (description: string) => { + description = description + .trim() + .split('\n') + .map((line) => `

${line.trim()}

`) + .join('') + + // replace

with
+ description = description.replace(/

<\/p>/g, '


') + + return description +} + class CampaignEditor extends React.Component { state = { ...this.props.campaign, @@ -53,20 +86,54 @@ class CampaignEditor extends React.Component { const { id, name, - cover, + nameEn, + nameZhHans, description, + descriptionEn, + descriptionZhHans, writingPeriod, applicationPeriod, coverId, link, } = this.state - const result = await putCampaign({ + + await putCampaign({ variables: { input: { id, - name, + name: [ + { + language: 'zh_hant', + text: name, + }, + { + language: 'en', + text: nameEn, + }, + { + language: 'zh_hans', + text: nameZhHans, + }, + ], cover: coverId, - description, + applicationPeriod, + writingPeriod, + description: [ + { + language: 'zh_hant', + text: serializeDescription(normalizeDescription(description)), + }, + { + language: 'en', + text: serializeDescription(normalizeDescription(descriptionEn)), + }, + { + language: 'zh_hans', + text: serializeDescription( + normalizeDescription(descriptionZhHans) + ), + }, + ], link, }, }, @@ -76,6 +143,10 @@ class CampaignEditor extends React.Component { (prev) => ({ ...prev, loading: false, error: null }), () => { message.success('儲存成功') + window.location.href = PATH.CAMPAIGN_DETAIL.replace( + ':id', + this.state.shortHash + ) } ) } catch (error) { @@ -92,9 +163,12 @@ class CampaignEditor extends React.Component { const { id, name, + nameEn, + nameZhHans, cover, - coverId, description, + descriptionEn, + descriptionZhHans, link, applicationPeriod, writingPeriod, @@ -117,17 +191,17 @@ class CampaignEditor extends React.Component { { - this.setState({ name: e.target.value }) + this.setState({ nameEn: e.target.value }) }} /> { - this.setState({ name: e.target.value }) + this.setState({ nameZhHans: e.target.value }) }} /> @@ -137,34 +211,37 @@ class CampaignEditor extends React.Component {
{ - // TODO: replace /n to
- this.setState({ description: e.target.value }) + this.setState({ + description: e.target.value, + }) }} />
{ - // TODO: replace /n to
- this.setState({ description: e.target.value }) + this.setState({ + descriptionEn: e.target.value, + }) }} />
{ - // TODO: replace /n to
- this.setState({ description: e.target.value }) + this.setState({ + descriptionZhHans: e.target.value, + }) }} />
@@ -199,7 +276,9 @@ class CampaignEditor extends React.Component {
{ this.setState({ applicationPeriod: { @@ -212,7 +291,9 @@ class CampaignEditor extends React.Component { { this.setState({ applicationPeriod: { @@ -229,7 +310,9 @@ class CampaignEditor extends React.Component {
{ this.setState({ writingPeriod: { @@ -243,7 +326,9 @@ class CampaignEditor extends React.Component { { this.setState({ writingPeriod: { diff --git a/src/components/DateTime/index.tsx b/src/components/DateTime/index.tsx index 8b5892c5..a8aa47c0 100644 --- a/src/components/DateTime/index.tsx +++ b/src/components/DateTime/index.tsx @@ -30,6 +30,10 @@ class DateTime extends React.Component { render() { const { date, ...props } = this.props + if (!date) { + return + } + return ( {timeFormat.en.render(new Date(date))} ( diff --git a/src/pages/CampaignDetail/index.tsx b/src/pages/CampaignDetail/index.tsx index f5668b3b..78e9e408 100644 --- a/src/pages/CampaignDetail/index.tsx +++ b/src/pages/CampaignDetail/index.tsx @@ -31,11 +31,7 @@ const sharedStyles = { } const Cover = ({ cover }: { cover?: string }) => { - return cover ? ( - - ) : ( -
未上傳
- ) + return cover ? :
} class CampaignDetail extends React.Component { @@ -66,12 +62,12 @@ class CampaignDetail extends React.Component { - ~{' '} - + ~{' '} + - ~{' '} - + ~{' '} +