diff --git a/app/javascript/packs/sidebarWidget.jsx b/app/javascript/packs/sidebarWidget.jsx index 54ccc0ca9..8ba27d3a7 100644 --- a/app/javascript/packs/sidebarWidget.jsx +++ b/app/javascript/packs/sidebarWidget.jsx @@ -1,8 +1,10 @@ import { h, render } from 'preact'; import SidebarWidget from '../sidebar-widget/SidebarWidget'; -HTMLDocument.prototype.ready = new Promise((resolve) => { - if (document.readyState !== 'loading') { return resolve(); } +HTMLDocument.prototype.ready = new Promise(resolve => { + if (document.readyState !== 'loading') { + return resolve(); + } document.addEventListener('DOMContentLoaded', () => resolve()); }); diff --git a/app/javascript/sidebar-widget/SidebarWidget.jsx b/app/javascript/sidebar-widget/SidebarWidget.jsx index f5bc31682..35dec20de 100644 --- a/app/javascript/sidebar-widget/SidebarWidget.jsx +++ b/app/javascript/sidebar-widget/SidebarWidget.jsx @@ -1,5 +1,4 @@ import { h, Component } from 'preact'; -import PropTypes from 'prop-types'; import sendFollowUser from '../src/utils/sendFollowUser'; import SidebarUser from './sidebarUser'; @@ -14,14 +13,18 @@ class SidebarWidget extends Component { suggestedUsers: [], }; } - + componentDidMount() { this.getTagInfo(); this.getSuggestedUsers(); } getTagInfo() { - this.setState({ tagInfo: JSON.parse(document.getElementById('sidebarWidget__pack').dataset.tagInfo) }); + this.setState({ + tagInfo: JSON.parse( + document.getElementById('sidebarWidget__pack').dataset.tagInfo, + ), + }); } getSuggestedUsers() { @@ -33,16 +36,16 @@ class SidebarWidget extends Component { credentials: 'same-origin', }) .then(response => response.json()) - .then((json) => { + .then(json => { this.setState({ suggestedUsers: json }); }) - .catch((error) => { + .catch(error => { console.log(error); }); } followUser(user) { - const toggleFollowState = (newFollowState) => { + const toggleFollowState = newFollowState => { const updatedUser = user; const updatedSuggestedUsers = this.state.suggestedUsers; const userIndex = this.state.suggestedUsers.indexOf(user); @@ -54,19 +57,13 @@ class SidebarWidget extends Component { } render() { - const users = this.state.suggestedUsers.map((user) => { - return( - - ); - }); + const users = this.state.suggestedUsers.map(user => ( + + )); return (
-
- {""} -
-
- {users} -
+
{''}
+
{users}
); } diff --git a/app/javascript/sidebar-widget/sidebarUser.jsx b/app/javascript/sidebar-widget/sidebarUser.jsx index f46d94dfe..eba89c544 100644 --- a/app/javascript/sidebar-widget/sidebarUser.jsx +++ b/app/javascript/sidebar-widget/sidebarUser.jsx @@ -12,20 +12,28 @@ class SidebarUser extends Component { } render() { - return( + return (
- {this.props.user.name} + {this.props.user.name} {this.props.user.name} -

- ) + ); } } @@ -34,4 +42,4 @@ SidebarUser.propTypes = { user: PropTypes.object.isRequired, }; -export default SidebarUser; \ No newline at end of file +export default SidebarUser; diff --git a/app/javascript/src/Onboarding.jsx b/app/javascript/src/Onboarding.jsx index 9f21dab3d..501c43804 100644 --- a/app/javascript/src/Onboarding.jsx +++ b/app/javascript/src/Onboarding.jsx @@ -44,14 +44,18 @@ class Onboarding extends Component { getUserTags() { fetch('/api/tags/onboarding') .then(response => response.json()) - .then((json) => { - const followedTagNames = JSON.parse(document.body.getAttribute('data-user')).followed_tag_names; + .then(json => { + const followedTagNames = JSON.parse( + document.body.getAttribute('data-user'), + ).followed_tag_names; function checkFollowingStatus(followedTags, jsonTags) { const newJSON = jsonTags; jsonTags.map((tag, index) => { if (followedTags.includes(tag.name)) { newJSON[index].following = true; - } else { newJSON[index].following = false; } + } else { + newJSON[index].following = false; + } return newJSON; }); return newJSON; @@ -59,7 +63,7 @@ class Onboarding extends Component { const updatedJSON = checkFollowingStatus(followedTagNames, json); this.setState({ allTags: updatedJSON }); }) - .catch((error) => { + .catch(error => { console.log(error); }); } @@ -73,12 +77,12 @@ class Onboarding extends Component { credentials: 'same-origin', }) .then(response => response.json()) - .then((json) => { + .then(json => { if (this.state.users.length === 0) { this.setState({ users: json, checkedUsers: json }); } }) - .catch((error) => { + .catch(error => { console.log(error); }); } @@ -99,14 +103,15 @@ class Onboarding extends Component { credentials: 'same-origin', }) .then(response => response.json()) - .then((json) => { + .then(json => { this.setState({ articles: json, savedArticles: json }); }); } handleBulkFollowUsers(users) { if (this.state.checkedUsers.length > 0 && !this.state.followRequestSent) { - const csrfToken = document.querySelector("meta[name='csrf-token']").content; + const csrfToken = document.querySelector("meta[name='csrf-token']") + .content; const formData = new FormData(); formData.append('users', JSON.stringify(users)); @@ -118,7 +123,7 @@ class Onboarding extends Component { }, body: formData, credentials: 'same-origin', - }).then((response) => { + }).then(response => { if (response.ok) { this.setState({ followRequestSent: true }); } @@ -128,7 +133,8 @@ class Onboarding extends Component { handleBulkSaveArticles(articles) { if (this.state.savedArticles.length > 0 && !this.state.saveRequestSent) { - const csrfToken = document.querySelector("meta[name='csrf-token']").content; + const csrfToken = document.querySelector("meta[name='csrf-token']") + .content; const formData = new FormData(); formData.append('articles', JSON.stringify(articles)); @@ -140,7 +146,7 @@ class Onboarding extends Component { }, body: formData, credentials: 'same-origin', - }).then((response) => { + }).then(response => { if (response.ok) { this.setState({ saveRequestSent: true }); } @@ -149,7 +155,9 @@ class Onboarding extends Component { } updateUserData() { - this.setState({ userData: JSON.parse(document.body.getAttribute('data-user')) }); + this.setState({ + userData: JSON.parse(document.body.getAttribute('data-user')), + }); if (this.state.userData.saw_onboarding === true) { this.setState({ showOnboarding: false }); } else { @@ -163,7 +171,7 @@ class Onboarding extends Component { const formData = new FormData(); formData.append('followable_type', 'Tag'); formData.append('followable_id', tag.id); - formData.append('verb', (tag.following ? 'unfollow' : 'follow')); + formData.append('verb', tag.following ? 'unfollow' : 'follow'); fetch('/follows', { method: 'POST', @@ -173,10 +181,9 @@ class Onboarding extends Component { body: formData, credentials: 'same-origin', }) - .then((response) => { - return response.json().then((json) => { + .then(response => response.json().then(json => { this.setState({ - allTags: this.state.allTags.map((currentTag) => { + allTags: this.state.allTags.map(currentTag => { const newTag = currentTag; if (currentTag.name === tag.name) { newTag.following = json.outcome === 'followed'; @@ -185,14 +192,12 @@ class Onboarding extends Component { // add in optimistic rendering }), }); - }); - }) - .catch((error) => { + })) + .catch(error => { console.log(error); }); } - handleCheckAllUsers() { if (this.state.checkedUsers.length < this.state.users.length) { this.setState({ checkedUsers: this.state.users.slice() }); @@ -239,9 +244,11 @@ class Onboarding extends Component { } handleNextButton() { - if (this.state.pageNumber === 2 && - this.state.users.length === 0 && - this.state.articles.length === 0) { + if ( + this.state.pageNumber === 2 && + this.state.users.length === 0 && + this.state.articles.length === 0 + ) { this.getUsersToFollow(); this.getSuggestedArticles(); } @@ -249,7 +256,10 @@ class Onboarding extends Component { this.setState({ pageNumber: this.state.pageNumber + 1 }); if (this.state.pageNumber === 4 && this.state.checkedUsers.length > 0) { this.handleBulkFollowUsers(this.state.checkedUsers); - } else if (this.state.pageNumber === 5 && this.state.savedArticles.length > 0) { + } else if ( + this.state.pageNumber === 5 && + this.state.savedArticles.length > 0 + ) { this.handleBulkSaveArticles(this.state.savedArticles); } } else if (this.state.pageNumber === 5) { @@ -278,12 +288,12 @@ class Onboarding extends Component { credentials: 'same-origin', }) .then(response => response.json()) - .then((json) => { + .then(json => { this.setState({ showOnboarding: json.outcome === 'onboarding opened' }); // console.log('this is special') // console.log(this.state) }) - .catch((error) => { + .catch(error => { console.log(error); }); } @@ -319,16 +329,17 @@ class Onboarding extends Component { /> ); } else if (this.state.pageNumber === 5) { - return ( - - ); + return ; } } renderBackButton() { if (this.state.pageNumber > 1) { return ( - + ); } } @@ -374,16 +385,15 @@ class Onboarding extends Component {
- -
-
- {this.toggleOnboardingSlide()} +
+
{this.toggleOnboardingSlide()}
-
- {this.renderBackButton()} -
+
{this.renderBackButton()}
{this.renderNextButton()} diff --git a/app/javascript/src/utils/sendFollowUser.js b/app/javascript/src/utils/sendFollowUser.js index 1cc2cb84d..5350dd0b1 100644 --- a/app/javascript/src/utils/sendFollowUser.js +++ b/app/javascript/src/utils/sendFollowUser.js @@ -1,25 +1,26 @@ export default function sendFollowUser(user, successCb, failureCb) { - const csrfToken = document.querySelector("meta[name='csrf-token']").content; + const csrfToken = document.querySelector("meta[name='csrf-token']").content; - const formData = new FormData(); - formData.append('followable_type', 'User'); - formData.append('followable_id', user.id); - formData.append('verb', (user.following ? 'unfollow' : 'follow')); + const formData = new FormData(); + formData.append('followable_type', 'User'); + formData.append('followable_id', user.id); + formData.append('verb', user.following ? 'unfollow' : 'follow'); - fetch('/follows', { - method: 'POST', - headers: { - 'X-CSRF-Token': csrfToken, - }, - body: formData, - credentials: 'same-origin', - }).then((response) => { - return response.json(); - }).then((json) => { - console.log('in fetch request: ' + json.outcome) + fetch('/follows', { + method: 'POST', + headers: { + 'X-CSRF-Token': csrfToken, + }, + body: formData, + credentials: 'same-origin', + }) + .then(response => response.json()) + .then(json => { + console.log(`in fetch request: ${ json.outcome}`); successCb(json.outcome); // json is followed or unfollowed - }).catch((error) => { + }) + .catch(error => { console.log(error); }); }