1
- import { Classes , Colors , Menu } from '@blueprintjs/core' ;
1
+ import { Classes , Colors , Divider , FormGroup , Menu , Text } from '@blueprintjs/core' ;
2
2
import { IconNames } from '@blueprintjs/icons' ;
3
3
import { Popover2 , Tooltip2 } from '@blueprintjs/popover2' ;
4
4
import React from 'react' ;
5
5
import * as CopyToClipboard from 'react-copy-to-clipboard' ;
6
6
7
- import { checkSessionIdExists , createNewSession } from '../collabEditing/CollabEditingHelper' ;
7
+ import { createNewSession , getDocInfoFromSessionId } from '../collabEditing/CollabEditingHelper' ;
8
8
import ControlButton from '../ControlButton' ;
9
9
import { showWarningMessage } from '../utils/notifications/NotificationsHelper' ;
10
10
11
11
type ControlBarSessionButtonsProps = DispatchProps & StateProps ;
12
12
13
13
type DispatchProps = {
14
14
handleSetEditorSessionId ?: ( editorSessionId : string ) => void ;
15
+ handleSetSessionDetails ?: ( sessionDetails : { docId : string ; readOnly : boolean } | null ) => void ;
15
16
} ;
16
17
17
18
type StateProps = {
@@ -24,6 +25,8 @@ type StateProps = {
24
25
25
26
type State = {
26
27
joinElemValue : string ;
28
+ sessionEditingId : string ;
29
+ sessionViewingId : string ;
27
30
} ;
28
31
29
32
function handleError ( error : any ) {
@@ -34,34 +37,77 @@ export class ControlBarSessionButtons extends React.PureComponent<
34
37
ControlBarSessionButtonsProps ,
35
38
State
36
39
> {
37
- private inviteInputElem : React . RefObject < HTMLInputElement > ;
40
+ private sessionEditingIdInputElem : React . RefObject < HTMLInputElement > ;
41
+ private sessionViewingIdInputElem : React . RefObject < HTMLInputElement > ;
38
42
39
43
constructor ( props : ControlBarSessionButtonsProps ) {
40
44
super ( props ) ;
41
- this . state = { joinElemValue : '' } ;
45
+ this . state = { joinElemValue : '' , sessionEditingId : '' , sessionViewingId : '' } ;
42
46
43
47
this . handleChange = this . handleChange . bind ( this ) ;
44
- this . inviteInputElem = React . createRef ( ) ;
45
- this . selectInviteInputText = this . selectInviteInputText . bind ( this ) ;
48
+ this . sessionEditingIdInputElem = React . createRef ( ) ;
49
+ this . sessionViewingIdInputElem = React . createRef ( ) ;
50
+ this . selectSessionEditingId = this . selectSessionEditingId . bind ( this ) ;
51
+ this . selectSessionViewingId = this . selectSessionViewingId . bind ( this ) ;
46
52
}
47
53
48
54
public render ( ) {
49
55
const handleStartInvite = ( ) => {
50
56
// FIXME this handler should be a Saga action or at least in a controller
51
57
if ( this . props . editorSessionId === '' ) {
52
- createNewSession ( this . props . getEditorValue ( ) ) . then ( sessionId => {
53
- this . props . handleSetEditorSessionId ! ( sessionId ) ;
58
+ createNewSession ( this . props . getEditorValue ( ) ) . then ( resp => {
59
+ this . setState ( {
60
+ sessionEditingId : resp . sessionEditingId ,
61
+ sessionViewingId : resp . sessionViewingId
62
+ } ) ;
63
+ this . props . handleSetEditorSessionId ! ( resp . sessionEditingId ) ;
64
+ this . props . handleSetSessionDetails ! ( { docId : resp . docId , readOnly : false } ) ;
54
65
} , handleError ) ;
55
66
}
56
67
} ;
57
68
58
69
const inviteButtonPopoverContent = (
59
- < >
60
- < input value = { this . props . editorSessionId } readOnly = { true } ref = { this . inviteInputElem } />
61
- < CopyToClipboard text = { '' + this . props . editorSessionId } >
62
- < ControlButton icon = { IconNames . DUPLICATE } onClick = { this . selectInviteInputText } />
63
- </ CopyToClipboard >
64
- </ >
70
+ < div style = { { display : 'flex' , flexDirection : 'column' } } >
71
+ { ! this . props . editorSessionId ? (
72
+ < >
73
+ < Text > You are not currently in any session.</ Text >
74
+ < Divider />
75
+ < ControlButton label = { 'Create' } icon = { IconNames . ADD } onClick = { handleStartInvite } />
76
+ </ >
77
+ ) : (
78
+ < >
79
+ < Text >
80
+ You have joined the session as{ ' ' }
81
+ { this . state . sessionEditingId ? 'an editor' : 'a viewer' } .
82
+ </ Text >
83
+ < Divider />
84
+ { this . state . sessionEditingId && (
85
+ < FormGroup subLabel = "Invite as editor" >
86
+ < input
87
+ value = { this . state . sessionEditingId }
88
+ readOnly = { true }
89
+ ref = { this . sessionEditingIdInputElem }
90
+ />
91
+ < CopyToClipboard text = { '' + this . state . sessionEditingId } >
92
+ < ControlButton icon = { IconNames . DUPLICATE } onClick = { this . selectSessionEditingId } />
93
+ </ CopyToClipboard >
94
+ </ FormGroup >
95
+ ) }
96
+ { this . state . sessionViewingId && (
97
+ < FormGroup subLabel = "Invite as viewer" >
98
+ < input
99
+ value = { this . state . sessionViewingId }
100
+ readOnly = { true }
101
+ ref = { this . sessionViewingIdInputElem }
102
+ />
103
+ < CopyToClipboard text = { '' + this . state . sessionViewingId } >
104
+ < ControlButton icon = { IconNames . DUPLICATE } onClick = { this . selectSessionViewingId } />
105
+ </ CopyToClipboard >
106
+ </ FormGroup >
107
+ ) }
108
+ </ >
109
+ ) }
110
+ </ div >
65
111
) ;
66
112
67
113
const inviteButton = (
@@ -70,19 +116,32 @@ export class ControlBarSessionButtons extends React.PureComponent<
70
116
inheritDarkTheme = { false }
71
117
content = { inviteButtonPopoverContent }
72
118
>
73
- < ControlButton label = "Invite" icon = { IconNames . GRAPH } onClick = { handleStartInvite } />
119
+ < ControlButton label = "Invite" icon = { IconNames . GRAPH } />
74
120
</ Popover2 >
75
121
) ;
76
122
77
123
const handleStartJoining = ( event : React . FormEvent < HTMLFormElement > ) => {
78
124
event . preventDefault ( ) ;
79
125
// FIXME this handler should be a Saga action or at least in a controller
80
- checkSessionIdExists ( this . state . joinElemValue ) . then (
81
- exists => {
82
- if ( exists ) {
126
+ getDocInfoFromSessionId ( this . state . joinElemValue ) . then (
127
+ docInfo => {
128
+ if ( docInfo !== null ) {
83
129
this . props . handleSetEditorSessionId ! ( this . state ! . joinElemValue ) ;
130
+ this . props . handleSetSessionDetails ! ( docInfo ) ;
131
+ if ( docInfo . readOnly ) {
132
+ this . setState ( {
133
+ sessionEditingId : '' ,
134
+ sessionViewingId : this . state . joinElemValue
135
+ } ) ;
136
+ } else {
137
+ this . setState ( {
138
+ sessionEditingId : this . state . joinElemValue ,
139
+ sessionViewingId : ''
140
+ } ) ;
141
+ }
84
142
} else {
85
143
this . props . handleSetEditorSessionId ! ( '' ) ;
144
+ this . props . handleSetSessionDetails ! ( null ) ;
86
145
showWarningMessage ( 'Could not find a session with that ID.' ) ;
87
146
}
88
147
} ,
@@ -120,7 +179,7 @@ export class ControlBarSessionButtons extends React.PureComponent<
120
179
onClick = { ( ) => {
121
180
// FIXME: this handler should be a Saga action or at least in a controller
122
181
this . props . handleSetEditorSessionId ! ( '' ) ;
123
- this . setState ( { joinElemValue : '' } ) ;
182
+ this . setState ( { joinElemValue : '' , sessionEditingId : '' , sessionViewingId : '' } ) ;
124
183
} }
125
184
/>
126
185
) ;
@@ -158,10 +217,17 @@ export class ControlBarSessionButtons extends React.PureComponent<
158
217
) ;
159
218
}
160
219
161
- private selectInviteInputText ( ) {
162
- if ( this . inviteInputElem . current !== null ) {
163
- this . inviteInputElem . current . focus ( ) ;
164
- this . inviteInputElem . current . select ( ) ;
220
+ private selectSessionEditingId ( ) {
221
+ if ( this . sessionEditingIdInputElem . current !== null ) {
222
+ this . sessionEditingIdInputElem . current . focus ( ) ;
223
+ this . sessionEditingIdInputElem . current . select ( ) ;
224
+ }
225
+ }
226
+
227
+ private selectSessionViewingId ( ) {
228
+ if ( this . sessionViewingIdInputElem . current !== null ) {
229
+ this . sessionViewingIdInputElem . current . focus ( ) ;
230
+ this . sessionViewingIdInputElem . current . select ( ) ;
165
231
}
166
232
}
167
233
0 commit comments