Skip to content

Commit 394dbf0

Browse files
fix(access-type): Perf fix, access action dispatch ⭐ (#101)
* feat(access-reducer): Perf fix, access dispatch * chore(release): 2.10.0-access-reducer.0 * fix(access-type): Remove HOC implementation * chore(package.json): Revert package version update * fix(promise): Implement try catch for promises :bug * fix(promise): Implement try catch for promises :bug
1 parent 181e845 commit 394dbf0

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

src/components/access-type.js

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, {PureComponent} from 'react';
2-
import {connect} from 'react-redux';
1+
import React, {Component} from 'react';
2+
import {connect, batch} from 'react-redux';
33
import get from "lodash/get";
44
import {
55
ACCESS_BEING_LOADED,
@@ -11,7 +11,8 @@ import {
1111
import PropTypes from "prop-types";
1212
import {awaitHelper} from "../utils";
1313

14-
class AccessTypeBase extends PureComponent {
14+
15+
class AccessTypeBase extends Component {
1516

1617
componentDidMount() {
1718
this.initAccessType();
@@ -70,9 +71,7 @@ class AccessTypeBase extends PureComponent {
7071
error: 'subscriptions fetch failed'
7172
};
7273
}
73-
const {'subscription_groups': subscriptionGroups = []} = subscriptions;
74-
this.props.subscriptionGroupLoaded(subscriptionGroups);
75-
return subscriptionGroups;
74+
return subscriptions["subscription_groups"] || [];
7675
};
7776

7877
getPaymentOptions= async () => {
@@ -85,15 +84,23 @@ class AccessTypeBase extends PureComponent {
8584
error: 'payment options fetch failed'
8685
};
8786
}
88-
this.props.paymentOptionsLoaded(paymentOptions);
8987
return paymentOptions;
9088
};
9189

9290
runSequentialCalls = async () => {
9391
const user = await this.setUser(this.props.email, this.props.phone);
9492
if(user) {
95-
this.getSubscription();
96-
this.getPaymentOptions();
93+
try{
94+
Promise.all([this.getSubscription(), this.getPaymentOptions()]).then(([subscriptionGroups, paymentOptions]) => {
95+
batch(() => {
96+
this.props.subscriptionGroupLoaded(subscriptionGroups);
97+
this.props.paymentOptionsLoaded(paymentOptions);
98+
})
99+
})
100+
}catch (e) {
101+
console.log(`Subscription / payments failed`, e);
102+
}
103+
97104
}
98105
};
99106

@@ -146,8 +153,6 @@ class AccessTypeBase extends PureComponent {
146153

147154
const meteredBody = {
148155
method: "POST",
149-
cache: "no-cache",
150-
"Cache-Control": "private,no-cache,no-store",
151156
headers: {
152157
"Content-Type": "text/plain"
153158
},
@@ -165,13 +170,6 @@ class AccessTypeBase extends PureComponent {
165170

166171
this.props.accessIsLoading(true);
167172

168-
169-
const accessObject = {
170-
id: assetId,
171-
type: 'story',
172-
attributes: {}
173-
};
174-
175173
const meteringParam = this.props.disableMetering === true ? '?disable-meter=true' : '';
176174
const { error, data: accessData } = await awaitHelper((await global.fetch(`/api/access/v1/stories/${assetId}/access${meteringParam}`)).json());
177175

@@ -193,16 +191,17 @@ class AccessTypeBase extends PureComponent {
193191
};
194192

195193
render() {
196-
const {children} = this.props;
197-
console.log(`RENDERING AT`);
198-
return children({
199-
initAccessType: this.initAccessType,
200-
initRazorPayPayment: this.initRazorPayPayment,
201-
checkAccess: this.checkAccess,
202-
getSubscriptionForUser: this.getSubscriptionForUser,
203-
accessUpdated: this.props.accessUpdated,
204-
accessIsLoading: this.props.accessIsLoading
205-
});
194+
const {children} = this.props;
195+
196+
return children({
197+
initAccessType: this.initAccessType,
198+
initRazorPayPayment: this.initRazorPayPayment,
199+
checkAccess: this.checkAccess,
200+
getSubscriptionForUser: this.getSubscriptionForUser,
201+
accessUpdated: this.props.accessUpdated,
202+
accessIsLoading: this.props.accessIsLoading
203+
});
204+
206205
}
207206
}
208207

src/store/reducers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import get from "lodash/get";
12
import {
23
BREAKING_NEWS_UPDATED,
34
CLIENT_SIDE_RENDERED,
@@ -11,6 +12,7 @@ import {
1112
SUBSCRIPTION_GROUP_UPDATED,
1213
PAYMENT_OPTIONS_UPDATED, ACCESS_BEING_LOADED, ACCESS_UPDATED, METER_UPDATED
1314
} from './actions';
15+
import {computeAccess} from "../utils";
1416

1517
function setToTrueOnEvent() {
1618
const events = Array.from(arguments);
@@ -86,7 +88,7 @@ function accessLoadingReducer(state = true, action) {
8688

8789
function accessReducer(state = {}, action) {
8890
switch (action.type) {
89-
case ACCESS_UPDATED: return Object.assign({}, state, action.access);
91+
case ACCESS_UPDATED: return computeAccess(state, action);
9092
default: return state;
9193
}
9294
}

src/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// FIXME: TEST THIS
2+
import get from "lodash/get";
3+
24
export function removeDuplicateStories(existingStories, newStories, keyFn = story => story.id) {
35
const existingStoryIds = existingStories.map(keyFn);
46
return newStories.filter(story => !existingStoryIds.includes(keyFn(story)));
@@ -45,4 +47,18 @@ export const getQliticsSchema = (story = {}, card = {}, element = {}) => {
4547
'story-element-id': element.id,
4648
'story-element-type': element.subtype || element.type
4749
});
50+
};
51+
52+
53+
export const computeAccess = (previousState, currentState) => {
54+
const currentAccess = get(currentState, ["access"], {});
55+
const currentStoryId = get(Object.keys(currentAccess), [0], "");
56+
if(currentStoryId in previousState){
57+
const storyAccess = previousState[currentStoryId];
58+
if(storyAccess.granted !== currentAccess[currentStoryId].granted || storyAccess.grantReason !== currentAccess[currentStoryId].grantReason){
59+
return {...previousState, ...currentAccess};
60+
}
61+
return previousState;
62+
}
63+
return {...previousState, ...currentAccess};
4864
};

0 commit comments

Comments
 (0)