Skip to content

Commit

Permalink
fix: update user state from server (freeCodeCamp#37374)
Browse files Browse the repository at this point in the history
* fix: return updates from server
* fix: make store consistant and adjust form validation
  • Loading branch information
ahmaxed authored and raisedadead committed Oct 21, 2019
1 parent e4a26c9 commit 3823ed1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion api-server/server/boot/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,5 @@ const updatePrivacyTerms = (req, res, next) => {

function updateUserFlag(req, res, next) {
const { user, body: update } = req;
user.updateAttributes(update, createStandardHandler(req, res, next));
return user.updateAttributes(update, createStandardHandler(req, res, next));
}
56 changes: 48 additions & 8 deletions client/src/components/settings/Internet.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const propTypes = {
class InternetSettings extends Component {
constructor(props) {
super(props);

const {
githubProfile = '',
linkedin = '',
Expand All @@ -39,6 +38,30 @@ class InternetSettings extends Component {
};
}

componentDidUpdate() {
const {
githubProfile = '',
linkedin = '',
twitter = '',
website = ''
} = this.props;

const { originalValues } = this.state;

if (
githubProfile !== originalValues.githubProfile ||
linkedin !== originalValues.linkedin ||
twitter !== originalValues.twitter ||
website !== originalValues.website
) {
/* eslint-disable-next-line react/no-did-update-set-state */
return this.setState({
originalValues: { githubProfile, linkedin, twitter, website }
});
}
return null;
}

getValidationStateFor(maybeURl = '') {
if (!maybeURl || !maybeUrlRE.test(maybeURl)) {
return {
Expand Down Expand Up @@ -78,7 +101,20 @@ class InternetSettings extends Component {
};

isFormValid = () => {
const { formValues } = this.state;
const { formValues, originalValues } = this.state;
const valueReducer = obj => {
return Object.values(obj).reduce(
(acc, cur) => (acc ? acc : cur !== ''),
false
);
};

let formHasValues = valueReducer(formValues);
let OriginalHasValues = valueReducer(originalValues);

// check if user had values but wants to delete them all
if (OriginalHasValues && !formHasValues) return true;

return Object.keys(formValues).reduce((bool, key) => {
const maybeUrl = formValues[key];
return maybeUrl ? isURL(maybeUrl) : bool;
Expand All @@ -88,10 +124,17 @@ class InternetSettings extends Component {
handleSubmit = e => {
e.preventDefault();
if (!this.isFormPristine() && this.isFormValid()) {
// Only submit the form if is has changed, and if it is valid
// // Only submit the form if is has changed, and if it is valid
const { formValues } = this.state;
const isSocial = {
isGithub: !!formValues.githubProfile,
isLinkedIn: !!formValues.linkedin,
isTwitter: !!formValues.twitter,
isWebsite: !!formValues.website
};

const { updateInternetSettings } = this.props;
return updateInternetSettings(formValues);
return updateInternetSettings({ ...isSocial, ...formValues });
}
return null;
};
Expand Down Expand Up @@ -183,10 +226,7 @@ class InternetSettings extends Component {
) : null}
</FormGroup>
<BlockSaveButton
disabled={
this.isFormPristine() ||
(!this.isFormPristine() && !this.isFormValid())
}
disabled={this.isFormPristine() || !this.isFormValid()}
/>
</form>
</FullWidthRow>
Expand Down

0 comments on commit 3823ed1

Please sign in to comment.