Skip to content

Commit

Permalink
Merge pull request Expensify#31655 from esh-g/duplicate-room-error
Browse files Browse the repository at this point in the history
Duplicate room name error when room created
  • Loading branch information
jasperhuangg authored Dec 2, 2023
2 parents 414c08c + b91ca76 commit 1682be4
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 32 deletions.
61 changes: 30 additions & 31 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = [
{
Expand All @@ -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 = [
{
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -2502,6 +2493,13 @@ function searchInServer(searchInput) {
debouncedSearchInServer(searchInput);
}

function clearNewRoomFormError() {
Onyx.set(ONYXKEYS.FORMS.NEW_ROOM_FORM, {
isLoading: false,
errorFields: {},
});
}

export {
searchInServer,
addComment,
Expand Down Expand Up @@ -2563,4 +2561,5 @@ export {
openRoomMembersPage,
savePrivateNotesDraft,
getDraftPrivateNote,
clearNewRoomFormError,
};
66 changes: 65 additions & 1 deletion src/pages/workspace/WorkspaceNewRoomPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -270,5 +328,11 @@ export default compose(
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
formState: {
key: ONYXKEYS.FORMS.NEW_ROOM_FORM,
},
session: {
key: ONYXKEYS.SESSION,
},
}),
)(WorkspaceNewRoomPage);

0 comments on commit 1682be4

Please sign in to comment.