commit 6e8f8c14554c36d057cbac5179711b6c9d64b9b6
parent dfd51c5f8495ad6c231aee1528f086f6ef71d503
Author: Johan Lindskogen <johan.lindskogen@gmail.com>
Date: Sun, 1 Apr 2018 20:31:24 +0200
Add hotlist fetcher
Diffstat:
1 file changed, 28 insertions(+), 4 deletions(-)
diff --git a/src/lib/weechat/action_transformer.ts b/src/lib/weechat/action_transformer.ts
@@ -3,8 +3,15 @@ export type WeechatReduxAction = {
payload: any;
};
-const reduceToObjectByKey = <T>(array: T[], keyFn: (t: T) => string): object =>
- array.reduce((acc, elem) => ({ ...acc, [keyFn(elem)]: elem }), {});
+type KeyFn<T> = (t: T) => string;
+type MapFn<A, B> = (a: A) => A | B;
+
+const reduceToObjectByKey = <T, U>(
+ array: T[],
+ keyFn: KeyFn<T>,
+ mapFn: MapFn<T, U> = a => a
+): object =>
+ array.reduce((acc, elem) => ({ ...acc, [keyFn(elem)]: mapFn(elem) }), {});
export const transformToReduxAction = (data: WeechatResponse<any>) => {
switch (data.id) {
@@ -73,14 +80,31 @@ export const transformToReduxAction = (data: WeechatResponse<any>) => {
bufferId: buffer.id
};
}
+ case "hotlist": {
+ const object = data.objects[0] as WeechatObject<WeechatHotlist[]>;
+
+ return {
+ type: "FETCH_HOTLISTS",
+ payload: reduceToObjectByKey(
+ object.content,
+ hotlist => hotlist.buffer,
+ h => {
+ const [unknown, message, privmsg, highlight] = h.count;
+ const sum = message + privmsg + highlight;
+ return { ...h, message, privmsg, highlight, sum };
+ }
+ )
+ };
+ }
case "buffers": {
const object = data.objects[0] as WeechatObject<WeechatBuffer[]>;
return {
type: "FETCH_BUFFERS",
payload: reduceToObjectByKey(
- object.content.map(o => ({ ...o, id: o.pointers[0] })),
- buffer => buffer.id
+ object.content,
+ buffer => buffer.pointers[0],
+ buf => ({ ...buf, id: buf.pointers[0] })
)
};
}