Skip to content

Commit

Permalink
fix(web): get default locale data from correct place (#6351)
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-kralj-novu authored Aug 19, 2024
1 parent d44d747 commit 88b6f41
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .source
15 changes: 10 additions & 5 deletions apps/web/src/ee/clerk/providers/EnterpriseAuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useNavigate } from 'react-router-dom';
import { useQueryClient } from '@tanstack/react-query';
import { useSegment } from '../../../components/providers/SegmentProvider';
import { ROUTES } from '../../../constants/routes';
import { useGetDefaultLocale } from '../../translations/hooks/useGetDefaultLocale';

const asyncNoop = async () => {};

Expand All @@ -18,6 +19,7 @@ EnterpriseAuthContext.displayName = 'EnterpriseAuthProvider';

export const EnterpriseAuthProvider = ({ children }: { children: React.ReactNode }) => {
const { signOut, orgId } = useAuth();
const { defaultLocale } = useGetDefaultLocale();
const { user: clerkUser, isLoaded: isUserLoaded } = useUser();
const { organization: clerkOrganization, isLoaded: isOrganizationLoaded } = useOrganization();
// TODO @ChmaraX: Can we use setActive from useSession, useSignIn, or useSignUp to avoid loading the list?
Expand Down Expand Up @@ -106,12 +108,12 @@ export const EnterpriseAuthProvider = ({ children }: { children: React.ReactNode
redirectTo({ url: ROUTES.AUTH_SIGNUP_ORGANIZATION_LIST });
}
}
}, [navigate, setActive, isOrgListLoaded, clerkUser, orgId, redirectTo]);
}, [setActive, isOrgListLoaded, clerkUser, orgId, redirectTo]);

const currentUser = useMemo(() => (clerkUser ? toUserEntity(clerkUser) : undefined), [clerkUser]);
const currentOrganization = useMemo(
() => (clerkOrganization ? toOrganizationEntity(clerkOrganization) : undefined),
[clerkOrganization]
() => (clerkOrganization ? toOrganizationEntity(clerkOrganization, defaultLocale) : undefined),
[clerkOrganization, defaultLocale]
);

// refetch queries on organization switch
Expand Down Expand Up @@ -177,7 +179,10 @@ const toUserEntity = (clerkUser: UserResource): IUserEntity => {
};
};

const toOrganizationEntity = (clerkOrganization: OrganizationResource): IOrganizationEntity => {
const toOrganizationEntity = (
clerkOrganization: OrganizationResource,
defaultLocale: string | undefined
): IOrganizationEntity => {
/*
* When mapping to IOrganizationEntity, we have 2 cases:
* - user exists and has signed in
Expand All @@ -197,7 +202,7 @@ const toOrganizationEntity = (clerkOrganization: OrganizationResource): IOrganiz
name: clerkOrganization.name,
createdAt: clerkOrganization.createdAt.toISOString(),
updatedAt: clerkOrganization.updatedAt.toISOString(),
defaultLocale: clerkOrganization.publicMetadata.defaultLocale,
defaultLocale: defaultLocale,
domain: clerkOrganization.publicMetadata.domain,
productUseCases: clerkOrganization.publicMetadata.productUseCases,
language: clerkOrganization.publicMetadata.language,
Expand Down
12 changes: 12 additions & 0 deletions apps/web/src/ee/translations/hooks/useGetDefaultLocale.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useQuery } from '@tanstack/react-query';
import { api } from '../../../api';

export const useGetDefaultLocale = () => {
const { data } = useQuery<{ defaultLocale: string }>([`translations/defaultLocale`], () =>
api.get(`/v1/translations/defaultLocale`)
);

return {
defaultLocale: data?.defaultLocale,
};
};

0 comments on commit 88b6f41

Please sign in to comment.