🐛 Fix anonymous features hook (#750)
This commit is contained in:
+33
-4
@@ -1,18 +1,47 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
import { useEffect } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
|
import Api from '@features/global/framework/api-service';
|
||||||
import FeatureTogglesService, {
|
import FeatureTogglesService, {
|
||||||
FeatureNames,
|
FeatureNames,
|
||||||
} from '@features/global/services/feature-toggles-service';
|
} from '@features/global/services/feature-toggles-service';
|
||||||
import { FeatureToggles, Feature, withFeatures } from '@paralleldrive/react-feature-toggles';
|
import { FeatureToggles, Feature, withFeatures } from '@paralleldrive/react-feature-toggles';
|
||||||
|
import RouterService from '@features/router/services/router-service';
|
||||||
import { useCurrentCompany } from '@features/companies/hooks/use-companies';
|
import { useCurrentCompany } from '@features/companies/hooks/use-companies';
|
||||||
|
import { CompanyType } from 'app/features/companies/types/company';
|
||||||
|
import Logger from '@features/global/framework/logger-service';
|
||||||
|
|
||||||
export const useFeatureToggles = () => {
|
export const useFeatureToggles = (anonymous = false) => {
|
||||||
|
const routeState = RouterService.getStateFromRoute();
|
||||||
const { activeFeatureNames } = FeatureTogglesService;
|
const { activeFeatureNames } = FeatureTogglesService;
|
||||||
const { company } = useCurrentCompany();
|
const [fetchedCompany, setFetchedCompany] = useState<CompanyType>();
|
||||||
|
const company = anonymous ? fetchedCompany : useCurrentCompany()?.company;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (anonymous) {
|
||||||
|
const fetchCompany = async () => {
|
||||||
|
try {
|
||||||
|
if (!routeState?.companyId) return;
|
||||||
|
|
||||||
|
const res = (await Api.get(
|
||||||
|
`/internal/services/users/v1/companies/${routeState.companyId}`,
|
||||||
|
)) as any;
|
||||||
|
if (res?.resource) {
|
||||||
|
setFetchedCompany(res.resource); // Assuming `res.resource` is structured correctly.
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error('Error fetching company', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchCompany();
|
||||||
|
}
|
||||||
|
}, [anonymous, routeState?.companyId]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const companyPlan = company?.plan;
|
const companyPlan = company?.plan;
|
||||||
companyPlan && FeatureTogglesService.setFeaturesFromCompanyPlan(companyPlan as any);
|
if (companyPlan) {
|
||||||
|
FeatureTogglesService.setFeaturesFromCompanyPlan(companyPlan as any);
|
||||||
|
}
|
||||||
}, [JSON.stringify(company)]);
|
}, [JSON.stringify(company)]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ import LocalStorage from 'app/features/global/framework/local-storage-service';
|
|||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
const companyId = useRouterCompany();
|
const companyId = useRouterCompany();
|
||||||
const { FeatureToggles, activeFeatureNames } = useFeatureToggles();
|
const { FeatureToggles, activeFeatureNames } = useFeatureToggles(true); // use toggle for anonymous user on public view
|
||||||
|
|
||||||
//Create a different local storage for shared view
|
//Create a different local storage for shared view
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user