Skip to content

Commit

Permalink
[TASK-1096] Implementation of queries for organization (#5197)
Browse files Browse the repository at this point in the history
* implemementation of useMeQuery and
useOrganizationQuery hooks

* general changes

* Implement organization mock in query

* Set enabled property from reactQuery

* review catches

* implement new api call logic
  • Loading branch information
pauloamorimbr authored Oct 29, 2024
1 parent 6d239d8 commit 8e7f10e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
51 changes: 43 additions & 8 deletions jsapp/js/account/stripe.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {endpoints} from 'js/api.endpoints';
import {ACTIVE_STRIPE_STATUSES} from 'js/constants';
import type {PaginatedResponse} from 'js/dataInterface';
import envStore from 'js/envStore';
import {fetchGet, fetchPost} from 'jsapp/js/api';
import {fetchGet, fetchGetUrl, fetchPost} from 'jsapp/js/api';
import type {
AccountLimit,
ChangePlan,
Expand All @@ -17,6 +17,8 @@ import {Limits} from 'js/account/stripe.types';
import {getAdjustedQuantityForPrice} from 'js/account/stripe.utils';
import {useQuery} from '@tanstack/react-query';
import {QueryKeys} from 'js/query/queryKeys';
import {FeatureFlag, useFeatureFlag} from '../featureFlags';
import sessionStore from 'js/stores/session';

const DEFAULT_LIMITS: AccountLimit = Object.freeze({
submission_limit: Limits.unlimited,
Expand Down Expand Up @@ -48,13 +50,46 @@ export async function changeSubscription(
});
}

export const useOrganizationQuery = () => useQuery({
queryFn: async () => {
const response = await fetchGet<PaginatedResponse<Organization>>(endpoints.ORGANIZATION_URL);
return response.results?.[0];
},
queryKey: [QueryKeys.organization],
});
export const useOrganizationQuery = () => {
const isMmosEnabled = useFeatureFlag(FeatureFlag.mmosEnabled);

const currentAccount = sessionStore.currentAccount;

const organizationUrl =
'organization' in currentAccount ? currentAccount.organization?.url : null;

// Using a separated function to fetch the organization data to prevent
// feature flag dependencies from being added to the hook
const fetchOrganization = async (): Promise<Organization> => {
// organizationUrl is a full url with protocol and domain name, so we're using fetchGetUrl
// We're asserting the organizationUrl is not null here because the query is disabled if it is
const organization = await fetchGetUrl<Organization>(organizationUrl!);

if (isMmosEnabled) {
return organization;
}

// While the project is in development we will force a false return for the is_mmo
// to make sure we don't have any implementations appearing for users
return {
...organization,
is_mmo: false,
};
};

// Setting the 'enabled' property so the query won't run until we have the session data
// loaded. Account data is needed to fetch the organization data.
const isQueryEnabled =
!sessionStore.isPending &&
sessionStore.isInitialLoadComplete &&
!!organizationUrl;

return useQuery({
queryFn: fetchOrganization,
queryKey: [QueryKeys.organization],
enabled: isQueryEnabled,
});
};

/**
* Start a checkout session for the given price and organization. Response contains the checkout URL.
Expand Down
10 changes: 9 additions & 1 deletion jsapp/js/account/stripe.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface SubscriptionPhase {
quantity: number;
tax_rates: [number];
billing_thresholds: Record<string, string>;
}
},
];
coupon: null;
currency: string;
Expand Down Expand Up @@ -147,6 +147,14 @@ export interface Organization {
modified: string;
slug: string;
is_owner: boolean;
is_mmo: boolean;
request_user_role: OrganizationUserRole;
}

export enum OrganizationUserRole {
member = 'member',
admin = 'admin',
owner = 'owner',
}

export enum PlanNames {
Expand Down
2 changes: 2 additions & 0 deletions jsapp/js/dataInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,8 @@ export interface AccountResponse {
tag: string | boolean;
};
social_accounts: SocialAccount[];
// Organization details
organization?: {url: string};
}

export interface AccountRequest {
Expand Down

0 comments on commit 8e7f10e

Please sign in to comment.