Skip to content
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

Refactor Usage frontend data fetching #4639

Merged
merged 16 commits into from Sep 22, 2023
Merged

Conversation

LMNTL
Copy link
Contributor

@LMNTL LMNTL commented Sep 15, 2023

Checklist

  1. If you've added code that should be tested, add tests
  2. If you've changed APIs, update (or create!) the documentation
  3. Ensure the tests pass
  4. Make sure that your code lints and that you've followed our coding style
  5. Write a title and, if necessary, a description of your work suitable for publishing in our release notes
  6. Mention any related issues in this repository (as #ISSUE) and in other repositories (as kobotoolbox/other#ISSUE)
  7. Open an issue in the docs if there are UI/UX changes

Description

Prevent some extra API calls that were slowing down the Project list/Usage page.

Notes

  • It now fetches only once per page load from api/v2/stripe/subscriptions/.
  • Ripped out a lot of code that was duplicated between the Usage component and getAllExceedingLimits and put it into a hook. Usage data is now passed down via context instead of getting fetched twice on the Usage page.
  • I renamed getAllExceedingLimits to useExceedingLimits to make it more obvious that it's a hook.
  • With the code-splitting and removed lines, it should (hopefully) read a bit easier.

@LMNTL LMNTL marked this pull request as ready for review September 15, 2023 20:13
Copy link
Member

@magicznyleszek magicznyleszek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I no longer see duplicated calls 👍 From the little testing in the UI I did, it seems everything works fine. I was overriding kpi/serializers/v2/service_usage.py to force the API to return exceeding/almost exceeding amounts - so I'm not 100% sure how the code behaves when the limits change while the app is being used, but I think this will be tested thoroughly during QA.

@@ -1,5 +1,6 @@
import {fetchGet, fetchPost} from 'jsapp/js/api';
import {getOrganization} from 'js/account/stripe.api';
import {createContext} from 'react';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing not needed :)

submissions: number;
transcriptionMinutes: number;
translationChars: number;
interface LimitState {
storageByteLimit: number | 'unlimited';
nlpCharacterLimit: number | 'unlimited';
nlpMinuteLimit: number | 'unlimited';
submissionLimit: number | 'unlimited';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to create some type UsageLimit = number | 'unlimited';?

let interval;
if (state.subscribedProduct?.length > 0) {
interval = state.subscribedProduct?.[0].items[0].price.recurring.interval;
}
isOverLimit(subscribedStorageLimit, usage.storage, 'storage');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given it's not a boolean-returning function, I find this name confusing. Could this be something like checkIfOverLimit or verifyOverLimit, something that sounds like an action that will cause the data to be rebuilt ;-)

limits={limits.warningList}
usagePage={usagePage}
usagePage={Boolean(usagePage)}
/>
)}
{useModal && (
<LimitModal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nitpick (and more of a problem with the modal styles/decisions) - the buttons overlap in an ugly way in a small viewport:
Screenshot 2023-09-20 at 01 39 18

@@ -23,8 +21,8 @@ const LimitNotifications = ({
const [dismissed, setDismissed] = useState(!useModal);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could go with export default const LimitNotifications and remove that closing line :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please add some short /** comment description of this ❤️

import {ACTIVE_STRIPE_STATUSES} from 'js/constants';
import envStore from 'js/envStore';
import subscriptionStore from 'js/account/subscriptionStore';

// check if the currently logged-in user has a paid subscription in an active status
// promise returns a boolean, or `null` if Stripe is not active - we check for the existence of `stripe_public_key`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please turn this into /** comment, as it will be showing up in code editors as a helpful hint :)

@@ -1,5 +1,6 @@
import {fetchGet, fetchPost} from 'jsapp/js/api';
import {getOrganization} from 'js/account/stripe.api';
import {createContext} from 'react';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly to stripe.api.ts - some short /** comments for funfctions would be great.

isUsageLoaded: boolean;
isLimitsLoaded: boolean;
isSubscriptionLoaded: boolean;
isLoaded: boolean;
}

export default function Usage() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a short /** comment ❤️ Having one would make it easier to understand why we render this in MyProjectsRoute etc.

isLoaded: boolean;
}

export function useUsage() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a short /** comment ❤️

@@ -30,17 +21,9 @@ function subscriptionReducer(state: SubscribedState, action: {prodData: any}) {
return {...state, subscribedProduct: action.prodData};
}

export const getAllExceedingLimits = () => {
export const useExceedingLimits = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a short /** comment ❤️

@jnm jnm merged commit 2a826c9 into release/2.023.37 Sep 22, 2023
4 checks passed
@LMNTL LMNTL deleted the usage-frontend-refactor branch November 27, 2023 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants