From 0e06767f98f98abfc236e90a5e3d95ba3850e71b Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Fri, 12 Aug 2016 19:40:40 -0400 Subject: [PATCH] Fix up login and article content editing and display. --- client/actions.js | 16 +++++++++------- client/views/journal_markdown_viewer.js | 21 +++++++++++++++------ client/views/user_login_modal.js | 20 +++++++++++--------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/client/actions.js b/client/actions.js index c82410b..24b681c 100644 --- a/client/actions.js +++ b/client/actions.js @@ -220,14 +220,16 @@ export function loginUser(username, password) { }, body: JSON.stringify({username: username, password: password}) }).then((response) => { - if (response.status === 200) { - let userInfoJson = response.json(); - let authToken = Reflect.get(userInfoJson, "token"); - let user = Reflect.get(userInfoJson, "user"); - dispatch(updateUser(authToken, user)); - } else { - dispatch(logoutUser(`Unable to login "${username}": ${response.text}`)); + // TODO: Turn this into a better action: + if (response.status !== 200) { + // TODO: Deal with resolving the promise first. + return dispatch(logoutUser(`Unable to login "${username}": ${response.text()}`)); } + return response.json(); + }).then((json) => { + let authToken = Reflect.get(json, "token"); + let user = Reflect.get(json, "user"); + return dispatch(updateUser(authToken, user)); }); } } diff --git a/client/views/journal_markdown_viewer.js b/client/views/journal_markdown_viewer.js index dab2a5b..11aff6d 100644 --- a/client/views/journal_markdown_viewer.js +++ b/client/views/journal_markdown_viewer.js @@ -36,7 +36,8 @@ export class JournalMarkdownView extends React.Component { editorTheme: React.PropTypes.string, params: React.PropTypes.shape({ pageId: React.PropTypes.string - }) + }), + userCanEdit: React.PropTypes.bool } } @@ -47,7 +48,12 @@ export class JournalMarkdownView extends React.Component { originalContent: this.props.article || "Please wait while we load this journal entry.", editorPaneState: EditorPaneState.hidden }; + this.updateEditAllowance(this.props.userCanEdit); this.handleSaveContent = this.handleSaveContent.bind(this); + this.handleToggleEditorPane = this.handleToggleEditorPane.bind(this); + this.handleSaveContent = this.handleSaveContent.bind(this); + this.handleResetContent = this.handleResetContent.bind(this); + this.handleUpdateCode = this.handleUpdateCode.bind(this); } componentDidMount() { @@ -55,6 +61,11 @@ export class JournalMarkdownView extends React.Component { appStore.dispatch(fetchArticle(pageToLoad)); } + componentWillReceiveProps(nextProps) { + this.setState({displayContent: nextProps.article, originalContent: nextProps.article}); + this.updateEditAllowance(nextProps.userCanEdit) + } + handleUpdateCode(newCode) { this.setState({displayContent: newCode}); } @@ -89,11 +100,8 @@ export class JournalMarkdownView extends React.Component { appStore.dispatch(saveArticle(pageContent)); } - updateEditAllowance() { - // let allowEdits = JournalEntryStore.canEditPage(); - let allowEdits = false; + updateEditAllowance(allowEdits) { let editorState = this.state.editorPaneState; - if (!allowEdits) { editorState = EditorPaneState.hidden; } @@ -148,6 +156,7 @@ export const ConnectedArticle = connect( 'id': state.page.id, 'article': state.page.content, 'title': state.page.title, - 'editorTheme': state.editor.theme + 'editorTheme': state.editor.theme, + 'userCanEdit': state.user.token !== "" }; })(JournalMarkdownView); diff --git a/client/views/user_login_modal.js b/client/views/user_login_modal.js index 1cd30c3..538d2b5 100644 --- a/client/views/user_login_modal.js +++ b/client/views/user_login_modal.js @@ -42,6 +42,17 @@ export class UserLoginView extends React.Component { this.handlePasswordUpdate = this.handlePasswordUpdate.bind(this); } + componentWillReceiveProps(nextProps) { + // TODO: Fix the transitions up, maybe have the login state as part of the app itself. + if (nextProps.externalError !== "") { + this.updateErrorMessage(nextProps.externalError); + } + + if (nextProps.isUserLoggedIn === true) { + this.handleHideLoginModal(); + } + } + handleLoginAttempt() { if (this.state.username === "" || this.state.password === "") { this.updateErrorMessage("Enter a username and password to login in..."); @@ -80,20 +91,11 @@ export class UserLoginView extends React.Component { let loginIcon = loggedIn ? () : (); let loginOperation = loggedIn ? this.handleLogoutUser : this.handleShowLoginModal; - // TODO: Fix the transitions up, maybe have the login state as part of the app itself. - // if (this.props.externalError !== "") { - // this.updateErrorMessage(this.props.externalError); - // } - let errorDisplay = ""; if (this.state.errorMessage !== undefined && this.state.errorMessage !== "") { errorDisplay = ({ this.state.errorMessage }); } - // if (loggedIn === true) { - // this.handleHideLoginModal(); - // } - return (