@@ -17,9 +17,11 @@ import withNavigationFocus from '@components/withNavigationFocus';
17
17
import useAutoFocusInput from '@hooks/useAutoFocusInput' ;
18
18
import useLocalize from '@hooks/useLocalize' ;
19
19
import useNetwork from '@hooks/useNetwork' ;
20
+ import usePrevious from '@hooks/usePrevious' ;
20
21
import useWindowDimensions from '@hooks/useWindowDimensions' ;
21
22
import compose from '@libs/compose' ;
22
23
import * as ErrorUtils from '@libs/ErrorUtils' ;
24
+ import Navigation from '@libs/Navigation/Navigation' ;
23
25
import Permissions from '@libs/Permissions' ;
24
26
import * as PolicyUtils from '@libs/PolicyUtils' ;
25
27
import * as ReportUtils from '@libs/ReportUtils' ;
@@ -63,11 +65,33 @@ const propTypes = {
63
65
64
66
/** Whether navigation is focused */
65
67
isFocused : PropTypes . bool . isRequired ,
68
+
69
+ /** Form state for NEW_ROOM_FORM */
70
+ formState : PropTypes . shape ( {
71
+ /** Loading state for the form */
72
+ isLoading : PropTypes . bool ,
73
+
74
+ /** Field errors in the form */
75
+ errorFields : PropTypes . objectOf ( PropTypes . objectOf ( PropTypes . string ) ) ,
76
+ } ) ,
77
+
78
+ /** Session details for the user */
79
+ session : PropTypes . shape ( {
80
+ /** accountID of current user */
81
+ accountID : PropTypes . number ,
82
+ } ) ,
66
83
} ;
67
84
const defaultProps = {
68
85
betas : [ ] ,
69
86
reports : { } ,
70
87
policies : { } ,
88
+ formState : {
89
+ isLoading : false ,
90
+ errorFields : { } ,
91
+ } ,
92
+ session : {
93
+ accountID : 0 ,
94
+ } ,
71
95
} ;
72
96
73
97
function WorkspaceNewRoomPage ( props ) {
@@ -78,6 +102,7 @@ function WorkspaceNewRoomPage(props) {
78
102
const [ visibility , setVisibility ] = useState ( CONST . REPORT . VISIBILITY . RESTRICTED ) ;
79
103
const [ policyID , setPolicyID ] = useState ( null ) ;
80
104
const [ writeCapability , setWriteCapability ] = useState ( CONST . REPORT . WRITE_CAPABILITIES . ALL ) ;
105
+ const wasLoading = usePrevious ( props . formState . isLoading ) ;
81
106
const visibilityDescription = useMemo ( ( ) => translate ( `newRoomPage.${ visibility } Description` ) , [ translate , visibility ] ) ;
82
107
const isPolicyAdmin = useMemo ( ( ) => {
83
108
if ( ! policyID ) {
@@ -86,14 +111,47 @@ function WorkspaceNewRoomPage(props) {
86
111
87
112
return ReportUtils . isPolicyAdmin ( policyID , props . policies ) ;
88
113
} , [ policyID , props . policies ] ) ;
114
+ const [ newRoomReportID , setNewRoomReportID ] = useState ( undefined ) ;
89
115
90
116
/**
91
117
* @param {Object } values - form input values passed by the Form component
92
118
*/
93
119
const submit = ( values ) => {
94
- Report . addPolicyReport ( policyID , values . roomName , visibility , writeCapability , values . welcomeMessage ) ;
120
+ const participants = [ props . session . accountID ] ;
121
+ const parsedWelcomeMessage = ReportUtils . getParsedComment ( values . welcomeMessage ) ;
122
+ const policyReport = ReportUtils . buildOptimisticChatReport (
123
+ participants ,
124
+ values . roomName ,
125
+ CONST . REPORT . CHAT_TYPE . POLICY_ROOM ,
126
+ policyID ,
127
+ CONST . REPORT . OWNER_ACCOUNT_ID_FAKE ,
128
+ false ,
129
+ '' ,
130
+ visibility ,
131
+ writeCapability || CONST . REPORT . WRITE_CAPABILITIES . ALL ,
132
+
133
+ // The room might contain all policy members so notifying always should be opt-in only.
134
+ CONST . REPORT . NOTIFICATION_PREFERENCE . DAILY ,
135
+ '' ,
136
+ '' ,
137
+ parsedWelcomeMessage ,
138
+ ) ;
139
+ setNewRoomReportID ( policyReport . reportID ) ;
140
+ Report . addPolicyReport ( policyReport ) ;
95
141
} ;
96
142
143
+ useEffect ( ( ) => {
144
+ Report . clearNewRoomFormError ( ) ;
145
+ } , [ ] ) ;
146
+
147
+ useEffect ( ( ) => {
148
+ if ( ! ( ( ( wasLoading && ! props . formState . isLoading ) || ( isOffline && props . formState . isLoading ) ) && _ . isEmpty ( props . formState . errorFields ) ) ) {
149
+ return ;
150
+ }
151
+ Navigation . dismissModal ( newRoomReportID ) ;
152
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- we just want this to update on changing the form State
153
+ } , [ props . formState ] ) ;
154
+
97
155
useEffect ( ( ) => {
98
156
if ( isPolicyAdmin ) {
99
157
return ;
@@ -270,5 +328,11 @@ export default compose(
270
328
reports : {
271
329
key : ONYXKEYS . COLLECTION . REPORT ,
272
330
} ,
331
+ formState : {
332
+ key : ONYXKEYS . FORMS . NEW_ROOM_FORM ,
333
+ } ,
334
+ session : {
335
+ key : ONYXKEYS . SESSION ,
336
+ } ,
273
337
} ) ,
274
338
) ( WorkspaceNewRoomPage ) ;
0 commit comments