-
Notifications
You must be signed in to change notification settings - Fork 27
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
Use patch for list updates #1511
Changes from all commits
d67f369
3f74622
4bac74a
88cd019
d73d9dd
b3dd5a3
de9f000
4e5af99
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import _ from 'lodash'; | ||
import { LIST_LOAD_PENDING, LIST_LOAD_SUCCESS, LIST_LOAD_FAIL } from './mutationTypes'; | ||
import { getList as getListFromAPI, saveList } from '../core-data/api'; | ||
import { getList as getListFromAPI, saveList, patchList as patchListAPI } from '../core-data/api'; | ||
|
||
/** | ||
* @module lists | ||
|
@@ -16,15 +16,52 @@ export function getList(store, listName) { | |
.catch(error => store.commit(LIST_LOAD_FAIL, { listName, error })); | ||
} | ||
|
||
export function updateList(store, { listName, fn }) { | ||
/** | ||
* if a list doesn't exist, creates the empty list | ||
* @param {Object} store | ||
* @param {string} listName | ||
* @returns {Array<Object>} | ||
*/ | ||
function getOrStartList(store, listName) { | ||
const prefix = _.get(store, 'state.site.prefix'); | ||
|
||
store.commit(LIST_LOAD_PENDING, { listName }); | ||
|
||
return getListFromAPI(prefix, listName) | ||
.catch(() => []) // allow adding to a new list | ||
.catch(error => { | ||
// if it's a 404, that means the list doesn't exist and we need to create it | ||
if (error && error.response && error.response.status === 404) { | ||
return saveList(prefix, listName, []); | ||
} else { | ||
throw error; | ||
} | ||
}); | ||
} | ||
|
||
export function updateList(store, { listName, fn }) { | ||
const prefix = _.get(store, 'state.site.prefix'); | ||
|
||
return getOrStartList(store, listName) | ||
.then(listItems => fn(_.cloneDeep(listItems))) | ||
.then(listItems => saveList(prefix, listName, listItems)) | ||
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. so it looks like the remaining lists using 'updateList' are new-pages and bookmarks, neither of which will ever reach a large number of items. However if we keep updateList in the codebase, then
so I could see value in forcing patchList to be used moving forward (i.e. remove updateList). But no strong opinions there 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. yea i was hesitant to remove it from the codebase in case there were already existing plugins that used it. and because of this, felt like it'd be a waste of time re-writing the bookmarks/new-pages logic. i'll definitely rework those two functions to use patchList though, if we think it'd be beneficial 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. ah yeah, the trouble of moving people off updateList when it is likely fine for 99% of lists - is probably not worth it. so yeah i'm good with leaving this as is |
||
.then(listItems => store.commit(LIST_LOAD_SUCCESS, { listName, listItems })) | ||
.catch(error => store.commit(LIST_LOAD_FAIL, { listName, error })); | ||
} | ||
|
||
export function patchList(store, { listName, fn }) { | ||
const prefix = _.get(store, 'state.site.prefix'); | ||
|
||
// get the list | ||
return getOrStartList(store, listName) | ||
// pass list to fn | ||
.then(list => { | ||
const patch = fn(list); | ||
|
||
// fn needs to return a patch `{ remove: [], add: [] }` | ||
return patch && (patch.add || patch.remove) ? patchListAPI(prefix, listName, patch) : list; | ||
}) | ||
// patch api returns full list | ||
.then(listItems => store.commit(LIST_LOAD_SUCCESS, { listName, listItems })) | ||
// catch and log errors | ||
.catch(error => store.commit(LIST_LOAD_FAIL, { listName, error })); | ||
} |
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.
removeLIstItem and addListItem are no longer used (and shouldn't be used) so they can be removed