-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #690 from thematters/feat/campaign
Campaign List & Detail
- Loading branch information
Showing
32 changed files
with
11,405 additions
and
14,516 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import * as React from 'react' | ||
import { Button, message } from 'antd' | ||
import gql from 'graphql-tag' | ||
import { Mutation } from 'react-apollo' | ||
import { withRouter, RouteComponentProps } from 'react-router-dom' | ||
|
||
import ErrorMessage from '../../../components/ErrorMessage' | ||
import { PATH } from '../../../constants' | ||
|
||
const PUT_CAMPAIGN = gql` | ||
mutation AddWritingChallenge($input: PutWritingChallengeInput!) { | ||
putWritingChallenge(input: $input) { | ||
id | ||
shortHash | ||
} | ||
} | ||
` | ||
|
||
type Props = RouteComponentProps & { onSuccess: any } | ||
|
||
type State = { | ||
loading: boolean | ||
warning: string | null | ||
error: any | ||
} | ||
|
||
class AddButton extends React.Component<Props, State> { | ||
state = { | ||
loading: false, | ||
warning: null, | ||
error: null, | ||
} | ||
|
||
private action = async (putWritingChallenge: any): Promise<any> => { | ||
const { history } = this.props | ||
|
||
if (!putWritingChallenge) { | ||
return | ||
} | ||
|
||
this.setState((prev) => ({ | ||
...prev, | ||
loading: true, | ||
warning: null, | ||
error: null, | ||
})) | ||
|
||
try { | ||
const result = await putWritingChallenge({ | ||
variables: { | ||
input: {}, | ||
}, | ||
}) | ||
|
||
const shortHash = result?.data?.putWritingChallenge.shortHash | ||
if (!shortHash) { | ||
throw new Error('invalid shortHash') | ||
} | ||
|
||
this.setState( | ||
(prev) => ({ ...prev, loading: false, error: null }), | ||
async () => { | ||
if (this.props.onSuccess) { | ||
await this.props.onSuccess() | ||
} | ||
history.push(PATH.CAMPAIGN_EDIT.replace(':id', shortHash)) | ||
} | ||
) | ||
} catch (error) { | ||
this.setState( | ||
(prev) => ({ ...prev, loading: false, error }), | ||
() => message.error('新增失敗') | ||
) | ||
} | ||
} | ||
|
||
public render() { | ||
const { loading, warning, error } = this.state | ||
|
||
if (error) { | ||
return <ErrorMessage error={error} /> | ||
} | ||
|
||
return ( | ||
<Mutation mutation={PUT_CAMPAIGN}> | ||
{(putWritingChallenge: any) => ( | ||
<Button | ||
onClick={() => this.action(putWritingChallenge)} | ||
disabled={loading} | ||
> | ||
新增 | ||
</Button> | ||
)} | ||
</Mutation> | ||
) | ||
} | ||
} | ||
|
||
export default withRouter(AddButton) |
Oops, something went wrong.