From 77fceacf7d38f4767626b8466fb8e41093d2405b Mon Sep 17 00:00:00 2001 From: someone-here Date: Wed, 22 Nov 2023 02:15:07 +0530 Subject: [PATCH 1/2] Duplicate room name error when room created --- src/libs/actions/Report.js | 45 +++++++++---------- src/pages/workspace/WorkspaceNewRoomPage.js | 49 ++++++++++++++++++++- 2 files changed, 68 insertions(+), 26 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index ac45a1e3f3be..a95c41bdab76 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1550,26 +1550,7 @@ function navigateToConciergeChat(ignoreConciergeReportID = false) { * @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, - ); +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 @@ -1591,6 +1572,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 = [ { @@ -1611,6 +1597,11 @@ function addPolicyReport(policyID, reportName, visibility, writeCapability = CON }, }, }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.FORMS.NEW_ROOM_FORM, + value: {isLoading: false}, + }, ]; const failureData = [ { @@ -1622,22 +1613,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); } /** diff --git a/src/pages/workspace/WorkspaceNewRoomPage.js b/src/pages/workspace/WorkspaceNewRoomPage.js index df78539bf665..6bac70202657 100644 --- a/src/pages/workspace/WorkspaceNewRoomPage.js +++ b/src/pages/workspace/WorkspaceNewRoomPage.js @@ -27,8 +27,10 @@ import useThemeStyles from '@styles/useThemeStyles'; import variables from '@styles/variables'; import * as App from '@userActions/App'; import * as Report from '@userActions/Report'; +import Navigation from '@libs/Navigation/Navigation'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; +import usePrevious from '@hooks/usePrevious'; const propTypes = { /** All reports shared with the user */ @@ -69,6 +71,13 @@ const defaultProps = { policies: {}, }; +function clearNewRoomFormError() { + Onyx.set(ONYXKEYS.FORMS.NEW_ROOM_FORM, { + isLoading: false, + errorFields: {}, + }); +} + function WorkspaceNewRoomPage(props) { const styles = useThemeStyles(); const {translate} = useLocalize(); @@ -77,6 +86,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.formData.isLoading); const visibilityDescription = useMemo(() => translate(`newRoomPage.${visibility}Description`), [translate, visibility]); const isPolicyAdmin = useMemo(() => { if (!policyID) { @@ -85,14 +95,45 @@ 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(() => { + return clearNewRoomFormError; + }, []); + + useEffect(() => { + if (((wasLoading && !props.formData.isLoading) || (isOffline && props.formData.isLoading)) && _.isEmpty(props.formData.errorFields)) { + Navigation.dismissModal(newRoomReportID); + } + }, [props.formData]); + useEffect(() => { if (isPolicyAdmin) { return; @@ -265,5 +306,11 @@ export default compose( reports: { key: ONYXKEYS.COLLECTION.REPORT, }, + formData: { + key: ONYXKEYS.FORMS.NEW_ROOM_FORM + }, + session: { + key: ONYXKEYS.SESSION, + } }), )(WorkspaceNewRoomPage); From b91ca76ea8c653855d572b9ddfef3e638d151b27 Mon Sep 17 00:00:00 2001 From: someone-here Date: Wed, 22 Nov 2023 02:56:01 +0530 Subject: [PATCH 2/2] Fix lint --- src/libs/actions/Report.js | 14 ++++-- src/pages/workspace/WorkspaceNewRoomPage.js | 51 ++++++++++++++------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index a95c41bdab76..8267f1518797 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1544,11 +1544,7 @@ 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 + * @param {Object} policyReport */ function addPolicyReport(policyReport) { const createdReportAction = ReportUtils.buildOptimisticCreatedReportAction(CONST.POLICY.OWNER_EMAIL_FAKE); @@ -2501,6 +2497,13 @@ function searchInServer(searchInput) { debouncedSearchInServer(searchInput); } +function clearNewRoomFormError() { + Onyx.set(ONYXKEYS.FORMS.NEW_ROOM_FORM, { + isLoading: false, + errorFields: {}, + }); +} + export { searchInServer, addComment, @@ -2562,4 +2565,5 @@ export { openRoomMembersPage, savePrivateNotesDraft, getDraftPrivateNote, + clearNewRoomFormError, }; diff --git a/src/pages/workspace/WorkspaceNewRoomPage.js b/src/pages/workspace/WorkspaceNewRoomPage.js index 6bac70202657..6f30942cf825 100644 --- a/src/pages/workspace/WorkspaceNewRoomPage.js +++ b/src/pages/workspace/WorkspaceNewRoomPage.js @@ -16,9 +16,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'; @@ -27,10 +29,8 @@ import useThemeStyles from '@styles/useThemeStyles'; import variables from '@styles/variables'; import * as App from '@userActions/App'; import * as Report from '@userActions/Report'; -import Navigation from '@libs/Navigation/Navigation'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import usePrevious from '@hooks/usePrevious'; const propTypes = { /** All reports shared with the user */ @@ -64,19 +64,34 @@ 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: {}, -}; - -function clearNewRoomFormError() { - Onyx.set(ONYXKEYS.FORMS.NEW_ROOM_FORM, { + formState: { isLoading: false, errorFields: {}, - }); -} + }, + session: { + accountID: 0, + }, +}; function WorkspaceNewRoomPage(props) { const styles = useThemeStyles(); @@ -86,7 +101,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.formData.isLoading); + const wasLoading = usePrevious(props.formState.isLoading); const visibilityDescription = useMemo(() => translate(`newRoomPage.${visibility}Description`), [translate, visibility]); const isPolicyAdmin = useMemo(() => { if (!policyID) { @@ -113,7 +128,7 @@ function WorkspaceNewRoomPage(props) { '', 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, '', @@ -125,14 +140,16 @@ function WorkspaceNewRoomPage(props) { }; useEffect(() => { - return clearNewRoomFormError; + Report.clearNewRoomFormError(); }, []); useEffect(() => { - if (((wasLoading && !props.formData.isLoading) || (isOffline && props.formData.isLoading)) && _.isEmpty(props.formData.errorFields)) { - Navigation.dismissModal(newRoomReportID); + if (!(((wasLoading && !props.formState.isLoading) || (isOffline && props.formState.isLoading)) && _.isEmpty(props.formState.errorFields))) { + return; } - }, [props.formData]); + 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) { @@ -306,11 +323,11 @@ export default compose( reports: { key: ONYXKEYS.COLLECTION.REPORT, }, - formData: { - key: ONYXKEYS.FORMS.NEW_ROOM_FORM + formState: { + key: ONYXKEYS.FORMS.NEW_ROOM_FORM, }, session: { key: ONYXKEYS.SESSION, - } + }, }), )(WorkspaceNewRoomPage);