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

chg: Workspace Edit Page #5271

Merged
merged 8 commits into from
Oct 25, 2021
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
37 changes: 16 additions & 21 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,23 @@ function uploadAvatar(file) {
return API.User_UploadAvatar({file})
.then((response) => {
if (response.jsonCode !== 200) {
// Show the user feedback
const errorMessage = translateLocal('workspace.editor.avatarUploadFailureMessage');
Growl.error(errorMessage, 5000);
return;
// Let the component handle the issue.
throw new Error();
}

return response.s3url;
});
}

/**
* Sets local values for the policy
* @param {String} policyID
* @param {Object} values
*/
function updateLocalPolicyValues(policyID, values) {
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, values);
}

/**
* Sets the name of the policy
*
Expand All @@ -307,34 +314,22 @@ function update(policyID, values, shouldGrowl = false) {
API.UpdatePolicy({policyID, value: JSON.stringify(values), lastModified: null})
.then((policyResponse) => {
if (policyResponse.jsonCode !== 200) {
// Show the user feedback
const errorMessage = translateLocal('workspace.editor.genericFailureMessage');
Growl.error(errorMessage, 5000);
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {isPolicyUpdating: false});
return;
throw new Error();
}

const updatedValues = {...values, ...{isPolicyUpdating: false}};
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, updatedValues);
updateLocalPolicyValues(policyID, {...values, isPolicyUpdating: false});
if (shouldGrowl) {
Growl.show(translateLocal('workspace.common.growlMessageOnSave'), CONST.GROWL.SUCCESS, 3000);
}
}).catch(() => {
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {isPolicyUpdating: false});
updateLocalPolicyValues(policyID, {isPolicyUpdating: false});

// Show the user feedback
const errorMessage = translateLocal('workspace.editor.genericFailureMessage');
Growl.error(errorMessage, 5000);
});
}

/**
* Sets local values for the policy
* @param {String} policyID
* @param {Object} values
*/
function updateLocalPolicyValues(policyID, values) {
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, values);
}

/**
* @param {String} policyID
* @param {Object} errors
Expand Down
17 changes: 17 additions & 0 deletions src/pages/workspace/WorkspaceSettingsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ class WorkspaceSettingsPage extends React.Component {
this.uploadAvatar = this.uploadAvatar.bind(this);
this.removeAvatar = this.removeAvatar.bind(this);
this.getCurrencyItems = this.getCurrencyItems.bind(this);
this.validate = this.validate.bind(this);
this.uploadAvatarPromise = Promise.resolve();
}

componentDidMount() {
getCurrencyList();
}

componentWillUnmount() {
Policy.updateLocalPolicyValues(this.props.policy.id, {isAvatarUploading: false});
}

/**
* @returns {Object[]}
*/
Expand Down Expand Up @@ -101,11 +106,15 @@ class WorkspaceSettingsPage extends React.Component {
this.uploadAvatarPromise = Policy.uploadAvatar(image).then(url => new Promise((resolve) => {
this.setState({avatarURL: url}, resolve);
})).catch(() => {
this.setState({previewAvatarURL: ''});
Growl.error(this.props.translate('workspace.editor.avatarUploadFailureMessage'));
}).finally(() => Policy.updateLocalPolicyValues(this.props.policy.id, {isAvatarUploading: false}));
}

submit() {
if (this.props.policy.isAvatarUploading || !this.validate()) {
return;
}
const name = this.state.name.trim();
const avatarURL = this.state.avatarURL;
const outputCurrency = this.state.currency;
Expand All @@ -118,6 +127,14 @@ class WorkspaceSettingsPage extends React.Component {
Growl.success(this.props.translate('workspace.common.growlMessageOnSave'));
}

validate() {
const errors = {};
if (!this.state.name.trim().length) {
errors.nameError = true;
johnmlee101 marked this conversation as resolved.
Show resolved Hide resolved
}
return _.size(errors) === 0;
}

render() {
const {policy} = this.props;

Expand Down