Skip to content
This repository was archived by the owner on May 28, 2024. It is now read-only.

Commit fd5c6fa

Browse files
committed
refactor: update methods to new method of storing preferences
1 parent dd3185a commit fd5c6fa

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"moment": "^2.17.1",
3131
"phpurlencode": "^1.0.0",
3232
"qs": "^6.3.0",
33+
"ramda": "^0.23.0",
3334
"rndm": "^1.2.0",
3435
"url": "^0.11.0"
3536
},
Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,71 @@
11
import DeskbookersError from '../../DeskbookersError'
22
import Resource from '../Resource'
3+
import { pickAll } from 'ramda'
4+
5+
// Construct and return Map
6+
function constructMap (obj) {
7+
const map = new Map()
8+
9+
for (const key in obj) {
10+
map.set(key, obj[key])
11+
}
12+
13+
return map
14+
}
315

416
export default class Preferences extends Resource {
517
constructor (api) {
618
super(api)
719
this.endpoint = 'user/preferences'
820
}
921

10-
async retrieve (key) {
11-
const value = await this.request({
22+
async getCurrentPreferences () {
23+
const res = await this.request({
1224
method: 'GET',
13-
path: this.endpoint,
14-
params: {
15-
keys: [key]
16-
}
25+
path: this.endpoint
1726
})
1827

19-
return value[0]
28+
return JSON.parse(res)
29+
}
30+
31+
/**
32+
* Retrieve a single preference
33+
*/
34+
async retrieve (key) {
35+
const prefs = await this.list([key])
36+
return prefs.get(key) || null
2037
}
2138

39+
/**
40+
* List all preferences, or a subset
41+
*/
2242
async list (...keys) {
23-
const preferences = await this.request({
24-
method: 'GET',
25-
path: this.endpoint,
26-
params: {
27-
keys
28-
}
29-
})
43+
const current = await this.getCurrentPreferences()
44+
45+
// Filter preferences based on desired keys
46+
const prefs = keys.length ? pickAll(keys, current): current
3047

31-
// Construct and return Map
32-
const map = new Map()
33-
keys.forEach((key, i) => map.set(key, preferences[i]))
34-
return map
48+
return constructMap(prefs)
3549
}
3650

51+
/**
52+
* Update preferences by key
53+
*/
3754
async update (preferences = {}) {
38-
const prefs = await this.request({
55+
const prefs = await this.getCurrentPreferences()
56+
57+
for (const key in preferences) {
58+
prefs[key] = preferences[key]
59+
}
60+
61+
const res = await this.request({
3962
method: 'POST',
4063
path: this.endpoint,
4164
params: {
42-
preferences
65+
preferences: prefs
4366
}
4467
})
4568

46-
// Construct and return Map
47-
const map = new Map()
48-
Object.keys(prefs).forEach(key => map.set(key, prefs[key]))
49-
return map
69+
return constructMap(JSON.parse(res))
5070
}
5171
}

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,6 +2687,10 @@ [email protected]:
26872687
version "0.2.0"
26882688
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620"
26892689

2690+
ramda@^0.23.0:
2691+
version "0.23.0"
2692+
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.23.0.tgz#ccd13fff73497a93974e3e86327bfd87bd6e8e2b"
2693+
26902694
randomatic@^1.1.3:
26912695
version "1.1.6"
26922696
resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-1.1.6.tgz#110dcabff397e9dcff7c0789ccc0a49adf1ec5bb"

0 commit comments

Comments
 (0)