-
Notifications
You must be signed in to change notification settings - Fork 186
Refactor mod loading #1859
Refactor mod loading #1859
Changes from 7 commits
c0714ce
d2a4cc2
53a3348
76935bd
81a1ecc
b67acea
1b89467
4a9b265
9fbba4e
033e131
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ export default class extends baseVw { | |
* @param {boolean} options.useCache - Use cached data for faster speed. | ||
* @param {array} options.moderatorIDs - list of moderators to retrieve. If none get all. | ||
* @param {array} options.excludeIDs - list of moderators to not use. | ||
* @param {boolean} options.includeSelf - allow fetching of the user as a moderator. | ||
* @param {string} options.method - POST or GET | ||
* @param {string} options.include - If apiPath is moderator, set to 'profile' or only | ||
* the peerIDs of each moderator are returned. | ||
|
@@ -48,6 +49,7 @@ export default class extends baseVw { | |
useCache: true, | ||
moderatorIDs: [], | ||
excludeIDs: [], | ||
includeSelf: false, | ||
method: 'POST', | ||
include: '', | ||
purchase: false, | ||
|
@@ -93,9 +95,10 @@ export default class extends baseVw { | |
|
||
super(opts); | ||
this.options = opts; | ||
this.excludeIDs = opts.excludeIDs; | ||
this.excludeIDs = opts.includeSelf ? | ||
[...opts.excludeIDs] : [...opts.excludeIDs, app.profile.id]; | ||
this.modsToFetch = [...opts.moderatorIDs]; | ||
this.unfetchedMods = []; | ||
this.fetchingMods = []; | ||
this.fetchingVerifiedMods = []; | ||
this.modFetches = []; | ||
this.moderatorsCol = new Moderators(); | ||
|
@@ -172,37 +175,36 @@ export default class extends baseVw { | |
throw new Error('Please provide the list of moderators as an array.'); | ||
} | ||
|
||
// don't get any that have already been added or excluded, or the user's own id. | ||
const excluded = [app.profile.id, ...this.allIDs, ...this.excludeIDs]; | ||
const IDs = _.without(op.moderatorIDs, excluded); | ||
const includeString = op.include ? `&include=${op.include}` : ''; | ||
const urlString = | ||
`ob/${op.apiPath}?async=${op.async}${includeString}&usecache=${op.useCache}`; | ||
const url = app.getServerUrl(urlString); | ||
|
||
this.unfetchedMods = IDs; | ||
this.fetchingMods = IDs; | ||
this.fetchingVerifiedMods = app.verifiedMods.matched(IDs); | ||
|
||
this.setState({ | ||
loading: true, | ||
noValidModerators: false, | ||
noValidVerifiedModerators: !this.fetchingVerifiedMods.length, | ||
}); | ||
// don't get any that have already been added or excluded. | ||
const excluded = [...this.allIDs, ...this.excludeIDs]; | ||
this.modsToFetch = _.without(op.moderatorIDs, excluded); | ||
this.unfetchedMods = [...this.modsToFetch]; | ||
this.fetchingVerifiedMods = app.verifiedMods.matched(this.modsToFetch); | ||
|
||
// Either a list of IDs can be posted, or any available moderators can be retrieved with GET | ||
if (IDs.length || op.method === 'GET') { | ||
if (this.modsToFetch.length || op.method === 'GET') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was moved so it wouldn't set the state to loading if there were no mods to fetch. |
||
this.setState({ | ||
loading: true, | ||
noValidModerators: false, | ||
noValidVerifiedModerators: !this.fetchingVerifiedMods.length, | ||
}); | ||
|
||
this.moderatorsStatus.setState({ | ||
hidden: false, | ||
loaded: 0, | ||
toLoad: IDs.length, | ||
toLoad: this.modsToFetch.length, | ||
total: this.modCount, | ||
loading: true, | ||
}); | ||
|
||
const fetch = $.ajax({ | ||
url, | ||
data: JSON.stringify(IDs), | ||
data: JSON.stringify(this.modsToFetch), | ||
method: op.method, | ||
}) | ||
.done((data) => { | ||
|
@@ -213,13 +215,15 @@ export default class extends baseVw { | |
const eventData = event.jsonData; | ||
if (eventData.error) { | ||
// errors don't have a message id, check to see if the peerID matches | ||
if (IDs.includes(eventData.peerId)) { | ||
// provide the expected capitalization of peerID | ||
eventData.peerID = eventData.peerId; | ||
delete eventData.peerId; | ||
if (this.modsToFetch.includes(eventData.peerID)) { | ||
this.processMod(eventData); | ||
} | ||
} else if (eventData.id === socketID && !excluded.includes(eventData.peerId)) { | ||
} else if ( | ||
eventData.id && | ||
eventData.peerId && | ||
eventData.id === socketID && | ||
!excluded.includes(eventData.peerId) | ||
) { | ||
this.processMod(eventData.profile); | ||
} | ||
}); | ||
|
@@ -230,9 +234,7 @@ export default class extends baseVw { | |
data.forEach(mod => { | ||
if (!excluded.includes(mod.peerId)) this.processMod(mod.profile); | ||
}); | ||
this.unfetchedMods = []; | ||
this.checkNotFetched(); | ||
if (!data.length) this.trigger('noModsFound', { guids: IDs }); | ||
if (!data.length) this.trigger('noModsFound', { guids: this.modsToFetch }); | ||
} | ||
}) | ||
.fail((xhr) => { | ||
|
@@ -259,7 +261,7 @@ export default class extends baseVw { | |
} | ||
|
||
checkNotFetched() { | ||
if (this.unfetchedMods.length === 0 && this.fetchingMods.length) { | ||
if (this.unfetchedMods.length === 0) { | ||
// All ids have been fetched and ids existed to fetch. | ||
this.moderatorsStatus.setState({ | ||
loading: false, | ||
|
@@ -272,7 +274,7 @@ export default class extends baseVw { | |
// Either ids are still fetching, or this is an open fetch with no set ids. | ||
this.moderatorsStatus.setState({ | ||
loaded: this.moderatorsCol.length, // not shown if open fetch | ||
toLoad: this.fetchingMods.length, // not shown if open fetch | ||
toLoad: this.modsToFetch.length, // not shown if open fetch | ||
total: this.modCount, | ||
}); | ||
// re-render to show the unverified moderators button if needed. | ||
|
@@ -405,17 +407,14 @@ export default class extends baseVw { | |
const showMods = this.modCards.filter(mod => this.modShouldRender(mod.model)); | ||
const unVerCount = this.modCards.filter(mod => | ||
mod.model.hasModCurrency(state.showOnlyCur) && !mod.model.isVerified).length; | ||
const totalIDs = this.allIDs.length; | ||
clearTimeout(this.renderTimer); | ||
this.renderTimer = null; | ||
|
||
loadTemplate('components/moderators/moderators.html', t => { | ||
this.$el.html(t({ | ||
wrapperClasses: this.options.wrapperClasses, | ||
placeholder: !showMods.length && (this.unfetchedMods.length || !totalIDs), | ||
purchase: this.options.purchase, | ||
totalShown: showMods.length, | ||
totalIDs, | ||
unVerCount, | ||
...state, | ||
})); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ export default class extends BaseVw { | |
let mode = this.getState().mode; | ||
if (mode === 'loadingXofY') mode = 'loadingXofYTimedOut'; | ||
this.setState({ showSpinner: false, mode }); | ||
}, 10000); | ||
}, 60000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made this longer since I was seeing the message for no mods appear then mods would load, which was pretty clunky. |
||
} | ||
super.setState(state, options); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,7 +162,7 @@ export default class extends BaseModal { | |
singleSelect: true, | ||
radioStyle: true, | ||
initialState: { | ||
showOnlyCur: currencies[0], | ||
showOnlyCur: '', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was only showing moderators that accept BTC, I assume it was there because originally we automatically selected the first currency. Since we changed it to require the user to select a currency (none are selected by default) this should also have no default. |
||
showVerifiedOnly: true, | ||
}, | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The placeholder got refactored so the messages below take care of everything with less duplication. This also makes sure there's always a message.