From e4d41b82e6d0e681d13e73b9e2e8a2d4be14f2da Mon Sep 17 00:00:00 2001 From: Chen-I Lim Date: Wed, 13 Jan 2021 11:17:47 -0800 Subject: [PATCH] Share board call API --- webapp/src/blocks/sharing.ts | 4 +- webapp/src/components/shareBoardComponent.tsx | 43 ++++++++++++++++--- webapp/src/components/viewHeader.tsx | 1 + 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/webapp/src/blocks/sharing.ts b/webapp/src/blocks/sharing.ts index 1a238e49e..091552148 100644 --- a/webapp/src/blocks/sharing.ts +++ b/webapp/src/blocks/sharing.ts @@ -5,8 +5,8 @@ interface ISharing { id: string, enabled: boolean, token: string, - modifiedBy: string, - updateAt: number, + modifiedBy?: string, + updateAt?: number, } export {ISharing} diff --git a/webapp/src/components/shareBoardComponent.tsx b/webapp/src/components/shareBoardComponent.tsx index fa8d8d317..5f54c2ca4 100644 --- a/webapp/src/components/shareBoardComponent.tsx +++ b/webapp/src/components/shareBoardComponent.tsx @@ -3,6 +3,10 @@ import React from 'react' import {injectIntl, IntlShape} from 'react-intl' +import {ISharing} from '../blocks/sharing' + +import client from '../octoClient' + import {Utils} from '../utils' import Button from '../widgets/buttons/button' @@ -12,22 +16,34 @@ import Modal from './modal' import './shareBoardComponent.scss' type Props = { + boardId: string onClose: () => void intl: IntlShape } type State = { - isShared?: boolean + sharing?: ISharing wasCopied?: boolean } class ShareBoardComponent extends React.PureComponent { state: State = {} + componentDidMount() { + this.loadData() + } + + private async loadData() { + const sharing = await client.getSharing(this.props.boardId) + this.setState({sharing}) + } + render(): JSX.Element { const {intl} = this.props + const {sharing} = this.state - const readToken = '123' + const isSharing = sharing && sharing.id === this.props.boardId && sharing.enabled + const readToken = (sharing && isSharing) ? sharing.token : '' const shareUrl = new URL(window.location.toString()) shareUrl.searchParams.set('r', readToken) @@ -40,11 +56,11 @@ class ShareBoardComponent extends React.PureComponent {
{intl.formatMessage({id: 'ShareBoard.share', defaultMessage: 'Publish to web and share this board to anyone'})}
- {this.state.isShared && + {isSharing &&
{ ) } - private onShareChanged = (isOn: boolean) => { - // TODO - this.setState({isShared: isOn}) + private createSharingInfo() { + const sharing: ISharing = { + id: this.props.boardId, + enabled: true, + token: Utils.createGuid(), + } + return sharing + } + + private onShareChanged = async (isOn: boolean) => { + const sharing: ISharing = this.state.sharing || this.createSharingInfo() + sharing.id = this.props.boardId + sharing.enabled = isOn + sharing.token = Utils.createGuid() + await client.setSharing(sharing) + await this.loadData() } } diff --git a/webapp/src/components/viewHeader.tsx b/webapp/src/components/viewHeader.tsx index 2fcf5f6f9..44a963d11 100644 --- a/webapp/src/components/viewHeader.tsx +++ b/webapp/src/components/viewHeader.tsx @@ -340,6 +340,7 @@ class ViewHeader extends React.Component { {this.state.showShareDialog && }