Skip to content

Commit

Permalink
Make changing child reactions on paginated reactions work
Browse files Browse the repository at this point in the history
  • Loading branch information
JelteF committed Nov 30, 2018
1 parent 14315a4 commit d5b1e23
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Expand Up @@ -17,7 +17,7 @@
"no-useless-concat": 2,
"default-case": 2,
"no-self-compare": 2,
"prefer-const": 2,
"prefer-const": 1,
"no-underscore-dangle": [2, { "allowAfterThis": true }],
"object-shorthand": 1,
"babel/no-invalid-this": 2,
Expand Down
54 changes: 51 additions & 3 deletions src/Context/Feed.js
Expand Up @@ -8,6 +8,7 @@ import type {
FeedRequestOptions,
FeedResponse,
ReactionRequestOptions,
ReactionFilterResponse,
} from 'getstream';
import type {
BaseActivityResponse,
Expand All @@ -30,6 +31,7 @@ export const FeedContext = React.createContext({});

// type FR = FeedResponse<Object, Object>;
type FR = FeedResponse<{}, {}>;
type RR = ReactionFilterResponse<{}, {}>;
export type FeedCtx = {|
feedGroup: string,
userId?: string,
Expand Down Expand Up @@ -560,7 +562,7 @@ export class FeedManager {
return map;
};

responseToReactionIdToPaths = (response: FR, previous: {} = {}) => {
feedResponseToReactionIdToPaths = (response: FR, previous: {} = {}) => {
const map = previous;
const currentPath = [];
function addFoundReactions(obj) {
Expand Down Expand Up @@ -593,6 +595,45 @@ export class FeedManager {
return map;
};

reactionResponseToReactionIdToPaths = (
response: RR,
previous: {},
basePath: $ReadOnlyArray<mixed>,
oldLength: number,
) => {
const map = previous;
const currentPath = [...basePath];
function addFoundReactions(obj) {
if (Array.isArray(obj)) {
obj.forEach((v, i) => {
currentPath.push(i);
addFoundReactions(v);
currentPath.pop();
});
} else if (isPlainObject(obj)) {
if (obj.id && obj.kind && obj.data) {
if (!map[obj.id]) {
map[obj.id] = [];
}
map[obj.id].push([...currentPath]);
}
for (const k in obj) {
currentPath.push(k);
addFoundReactions(obj[k]);
currentPath.pop();
}
}
}

for (const a of response.results) {
currentPath.push(oldLength);
addFoundReactions((a: any));
currentPath.pop();
oldLength++;
}
return map;
};

responseToReactionActivities = (response: FR) => {
if (response.results.length === 0) {
return {};
Expand Down Expand Up @@ -651,7 +692,7 @@ export class FeedManager {
activities: this.responseToActivityMap(response),
activityIdToPath: this.responseToActivityIdToPath(response),
activityIdToPaths: this.responseToActivityIdToPaths(response),
reactionIdToPaths: this.responseToReactionIdToPaths(response),
reactionIdToPaths: this.feedResponseToReactionIdToPaths(response),
reactionActivities: this.responseToReactionActivities(response),
refreshing: false,
lastResponse: response,
Expand Down Expand Up @@ -777,7 +818,7 @@ export class FeedManager {
response,
prevState.activityIdToPaths,
),
reactionIdToPaths: this.responseToReactionIdToPaths(
reactionIdToPaths: this.feedResponseToReactionIdToPaths(
response,
prevState.reactionIdToPaths,
),
Expand Down Expand Up @@ -846,6 +887,13 @@ export class FeedManager {
.updateIn(latestReactionsPath, (v = immutable.List()) =>
v.concat(immutable.fromJS(response.results)),
),
reactionIdToPaths: this.reactionResponseToReactionIdToPaths(
response,
prevState.reactionIdToPaths,
latestReactionsPath,
prevState.activities.getIn(latestReactionsPath, immutable).toJS()
.length,
),
}));
};

Expand Down

0 comments on commit d5b1e23

Please sign in to comment.