diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index a03488429405..f1be266894ad 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1540,32 +1540,9 @@ function navigateToConciergeChat(ignoreConciergeReportID = false) { /** * Add a policy report (workspace room) optimistically and navigate to it. * - * @param {String} policyID - * @param {String} reportName - * @param {String} visibility - * @param {String} writeCapability - * @param {String} welcomeMessage - */ -function addPolicyReport(policyID, reportName, visibility, writeCapability = CONST.REPORT.WRITE_CAPABILITIES.ALL, welcomeMessage = '') { - const participants = [currentUserAccountID]; - const parsedWelcomeMessage = ReportUtils.getParsedComment(welcomeMessage); - const policyReport = ReportUtils.buildOptimisticChatReport( - participants, - reportName, - CONST.REPORT.CHAT_TYPE.POLICY_ROOM, - policyID, - CONST.REPORT.OWNER_ACCOUNT_ID_FAKE, - false, - '', - visibility, - writeCapability, - - // The room might contain all policy members so notifying always should be opt-in only. - CONST.REPORT.NOTIFICATION_PREFERENCE.DAILY, - '', - '', - parsedWelcomeMessage, - ); + * @param {Object} policyReport + */ +function addPolicyReport(policyReport) { const createdReportAction = ReportUtils.buildOptimisticCreatedReportAction(CONST.POLICY.OWNER_EMAIL_FAKE); // Onyx.set is used on the optimistic data so that it is present before navigating to the workspace room. With Onyx.merge the workspace room reportID is not present when @@ -1587,6 +1564,11 @@ function addPolicyReport(policyID, reportName, visibility, writeCapability = CON key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${policyReport.reportID}`, value: {[createdReportAction.reportActionID]: createdReportAction}, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.FORMS.NEW_ROOM_FORM, + value: {isLoading: true}, + }, ]; const successData = [ { @@ -1607,6 +1589,11 @@ function addPolicyReport(policyID, reportName, visibility, writeCapability = CON }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.FORMS.NEW_ROOM_FORM, + value: {isLoading: false}, + }, ]; const failureData = [ { @@ -1618,22 +1605,26 @@ function addPolicyReport(policyID, reportName, visibility, writeCapability = CON }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.FORMS.NEW_ROOM_FORM, + value: {isLoading: false}, + }, ]; API.write( 'AddWorkspaceRoom', { policyID: policyReport.policyID, - reportName, - visibility, + reportName: policyReport.reportName, + visibility: policyReport.visibility, reportID: policyReport.reportID, createdReportActionID: createdReportAction.reportActionID, - writeCapability, - welcomeMessage: parsedWelcomeMessage, + writeCapability: policyReport.writeCapability, + welcomeMessage: policyReport.welcomeMessage, }, {optimisticData, successData, failureData}, ); - Navigation.dismissModal(policyReport.reportID); } /** @@ -2502,6 +2493,13 @@ function searchInServer(searchInput) { debouncedSearchInServer(searchInput); } +function clearNewRoomFormError() { + Onyx.set(ONYXKEYS.FORMS.NEW_ROOM_FORM, { + isLoading: false, + errorFields: {}, + }); +} + export { searchInServer, addComment, @@ -2563,4 +2561,5 @@ export { openRoomMembersPage, savePrivateNotesDraft, getDraftPrivateNote, + clearNewRoomFormError, }; diff --git a/src/pages/workspace/WorkspaceNewRoomPage.js b/src/pages/workspace/WorkspaceNewRoomPage.js index 2abc6c3d13fa..a7d32a8e54af 100644 --- a/src/pages/workspace/WorkspaceNewRoomPage.js +++ b/src/pages/workspace/WorkspaceNewRoomPage.js @@ -17,9 +17,11 @@ import withNavigationFocus from '@components/withNavigationFocus'; import useAutoFocusInput from '@hooks/useAutoFocusInput'; import useLocalize from '@hooks/useLocalize'; import useNetwork from '@hooks/useNetwork'; +import usePrevious from '@hooks/usePrevious'; import useWindowDimensions from '@hooks/useWindowDimensions'; import compose from '@libs/compose'; import * as ErrorUtils from '@libs/ErrorUtils'; +import Navigation from '@libs/Navigation/Navigation'; import Permissions from '@libs/Permissions'; import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReportUtils from '@libs/ReportUtils'; @@ -63,11 +65,33 @@ const propTypes = { /** Whether navigation is focused */ isFocused: PropTypes.bool.isRequired, + + /** Form state for NEW_ROOM_FORM */ + formState: PropTypes.shape({ + /** Loading state for the form */ + isLoading: PropTypes.bool, + + /** Field errors in the form */ + errorFields: PropTypes.objectOf(PropTypes.objectOf(PropTypes.string)), + }), + + /** Session details for the user */ + session: PropTypes.shape({ + /** accountID of current user */ + accountID: PropTypes.number, + }), }; const defaultProps = { betas: [], reports: {}, policies: {}, + formState: { + isLoading: false, + errorFields: {}, + }, + session: { + accountID: 0, + }, }; function WorkspaceNewRoomPage(props) { @@ -78,6 +102,7 @@ function WorkspaceNewRoomPage(props) { const [visibility, setVisibility] = useState(CONST.REPORT.VISIBILITY.RESTRICTED); const [policyID, setPolicyID] = useState(null); const [writeCapability, setWriteCapability] = useState(CONST.REPORT.WRITE_CAPABILITIES.ALL); + const wasLoading = usePrevious(props.formState.isLoading); const visibilityDescription = useMemo(() => translate(`newRoomPage.${visibility}Description`), [translate, visibility]); const isPolicyAdmin = useMemo(() => { if (!policyID) { @@ -86,14 +111,47 @@ function WorkspaceNewRoomPage(props) { return ReportUtils.isPolicyAdmin(policyID, props.policies); }, [policyID, props.policies]); + const [newRoomReportID, setNewRoomReportID] = useState(undefined); /** * @param {Object} values - form input values passed by the Form component */ const submit = (values) => { - Report.addPolicyReport(policyID, values.roomName, visibility, writeCapability, values.welcomeMessage); + const participants = [props.session.accountID]; + const parsedWelcomeMessage = ReportUtils.getParsedComment(values.welcomeMessage); + const policyReport = ReportUtils.buildOptimisticChatReport( + participants, + values.roomName, + CONST.REPORT.CHAT_TYPE.POLICY_ROOM, + policyID, + CONST.REPORT.OWNER_ACCOUNT_ID_FAKE, + false, + '', + visibility, + writeCapability || CONST.REPORT.WRITE_CAPABILITIES.ALL, + + // The room might contain all policy members so notifying always should be opt-in only. + CONST.REPORT.NOTIFICATION_PREFERENCE.DAILY, + '', + '', + parsedWelcomeMessage, + ); + setNewRoomReportID(policyReport.reportID); + Report.addPolicyReport(policyReport); }; + useEffect(() => { + Report.clearNewRoomFormError(); + }, []); + + useEffect(() => { + if (!(((wasLoading && !props.formState.isLoading) || (isOffline && props.formState.isLoading)) && _.isEmpty(props.formState.errorFields))) { + return; + } + Navigation.dismissModal(newRoomReportID); + // eslint-disable-next-line react-hooks/exhaustive-deps -- we just want this to update on changing the form State + }, [props.formState]); + useEffect(() => { if (isPolicyAdmin) { return; @@ -270,5 +328,11 @@ export default compose( reports: { key: ONYXKEYS.COLLECTION.REPORT, }, + formState: { + key: ONYXKEYS.FORMS.NEW_ROOM_FORM, + }, + session: { + key: ONYXKEYS.SESSION, + }, }), )(WorkspaceNewRoomPage);