Skip to content

Commit 4918251

Browse files
authored
Merge pull request #5271 from parasharrajat/workspace-fix
chg: Workspace Edit Page
2 parents adaf1c0 + e88a920 commit 4918251

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/libs/actions/Policy.js

+16-21
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,23 @@ function uploadAvatar(file) {
286286
return API.User_UploadAvatar({file})
287287
.then((response) => {
288288
if (response.jsonCode !== 200) {
289-
// Show the user feedback
290-
const errorMessage = translateLocal('workspace.editor.avatarUploadFailureMessage');
291-
Growl.error(errorMessage, 5000);
292-
return;
289+
// Let the component handle the issue.
290+
throw new Error();
293291
}
294292

295293
return response.s3url;
296294
});
297295
}
298296

297+
/**
298+
* Sets local values for the policy
299+
* @param {String} policyID
300+
* @param {Object} values
301+
*/
302+
function updateLocalPolicyValues(policyID, values) {
303+
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, values);
304+
}
305+
299306
/**
300307
* Sets the name of the policy
301308
*
@@ -307,34 +314,22 @@ function update(policyID, values, shouldGrowl = false) {
307314
API.UpdatePolicy({policyID, value: JSON.stringify(values), lastModified: null})
308315
.then((policyResponse) => {
309316
if (policyResponse.jsonCode !== 200) {
310-
// Show the user feedback
311-
const errorMessage = translateLocal('workspace.editor.genericFailureMessage');
312-
Growl.error(errorMessage, 5000);
313-
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {isPolicyUpdating: false});
314-
return;
317+
throw new Error();
315318
}
316319

317-
const updatedValues = {...values, ...{isPolicyUpdating: false}};
318-
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, updatedValues);
320+
updateLocalPolicyValues(policyID, {...values, isPolicyUpdating: false});
319321
if (shouldGrowl) {
320322
Growl.show(translateLocal('workspace.common.growlMessageOnSave'), CONST.GROWL.SUCCESS, 3000);
321323
}
322324
}).catch(() => {
323-
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {isPolicyUpdating: false});
325+
updateLocalPolicyValues(policyID, {isPolicyUpdating: false});
326+
327+
// Show the user feedback
324328
const errorMessage = translateLocal('workspace.editor.genericFailureMessage');
325329
Growl.error(errorMessage, 5000);
326330
});
327331
}
328332

329-
/**
330-
* Sets local values for the policy
331-
* @param {String} policyID
332-
* @param {Object} values
333-
*/
334-
function updateLocalPolicyValues(policyID, values) {
335-
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, values);
336-
}
337-
338333
/**
339334
* @param {String} policyID
340335
* @param {Object} errors

src/pages/workspace/WorkspaceSettingsPage.js

+17
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,18 @@ class WorkspaceSettingsPage extends React.Component {
6767
this.uploadAvatar = this.uploadAvatar.bind(this);
6868
this.removeAvatar = this.removeAvatar.bind(this);
6969
this.getCurrencyItems = this.getCurrencyItems.bind(this);
70+
this.validate = this.validate.bind(this);
7071
this.uploadAvatarPromise = Promise.resolve();
7172
}
7273

7374
componentDidMount() {
7475
getCurrencyList();
7576
}
7677

78+
componentWillUnmount() {
79+
Policy.updateLocalPolicyValues(this.props.policy.id, {isAvatarUploading: false});
80+
}
81+
7782
/**
7883
* @returns {Object[]}
7984
*/
@@ -101,11 +106,15 @@ class WorkspaceSettingsPage extends React.Component {
101106
this.uploadAvatarPromise = Policy.uploadAvatar(image).then(url => new Promise((resolve) => {
102107
this.setState({avatarURL: url}, resolve);
103108
})).catch(() => {
109+
this.setState({previewAvatarURL: ''});
104110
Growl.error(this.props.translate('workspace.editor.avatarUploadFailureMessage'));
105111
}).finally(() => Policy.updateLocalPolicyValues(this.props.policy.id, {isAvatarUploading: false}));
106112
}
107113

108114
submit() {
115+
if (this.props.policy.isAvatarUploading || !this.validate()) {
116+
return;
117+
}
109118
const name = this.state.name.trim();
110119
const avatarURL = this.state.avatarURL;
111120
const outputCurrency = this.state.currency;
@@ -118,6 +127,14 @@ class WorkspaceSettingsPage extends React.Component {
118127
Growl.success(this.props.translate('workspace.common.growlMessageOnSave'));
119128
}
120129

130+
validate() {
131+
const errors = {};
132+
if (!this.state.name.trim().length) {
133+
errors.nameError = true;
134+
}
135+
return _.size(errors) === 0;
136+
}
137+
121138
render() {
122139
const {policy} = this.props;
123140

0 commit comments

Comments
 (0)