Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate room name error when room created #31655

Merged
merged 2 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 30 additions & 31 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -1544,32 +1544,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 @@ -1591,6 +1568,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 @@ -1611,6 +1593,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 @@ -1622,22 +1609,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 @@ -2506,6 +2497,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 @@ -2567,4 +2565,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 @@ -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';
Expand Down Expand Up @@ -62,11 +64,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 @@ -77,6 +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.formState.isLoading);
const visibilityDescription = useMemo(() => translate(`newRoomPage.${visibility}Description`), [translate, visibility]);
const isPolicyAdmin = useMemo(() => {
if (!policyID) {
Expand All @@ -85,14 +110,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 @@ -265,5 +323,11 @@ export default compose(
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
formState: {
key: ONYXKEYS.FORMS.NEW_ROOM_FORM,
},
session: {
key: ONYXKEYS.SESSION,
},
}),
)(WorkspaceNewRoomPage);