Skip to content

Commit

Permalink
Merge pull request #1568 from NDLANO/fetch-ai-config-backend
Browse files Browse the repository at this point in the history
Fetches config from backend in stead of config.
  • Loading branch information
gunnarvelle authored Nov 13, 2023
2 parents 5e14f16 + 902f956 commit 244f8bd
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 30 deletions.
27 changes: 0 additions & 27 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,6 @@ const logglyApiKey = (): string | undefined => {
return getEnvironmentVariabel('LOGGLY_API_KEY');
};

export const allowedAIOrgs = () => {
const defaultList = [
'Agder fylkeskommune',
'Nordland fylkeskommune',
'Rogaland fylkeskommune',
'Troms og Finnmark fylkeskommune',
'Trøndelag fylkeskommune',
'Vestland fylkeskommune',
];
if (['local', 'dev'].includes(ndlaEnvironment)) {
return [...defaultList, 'Universitetet i Rogn']; // frank_foreleser
}
if (['test'].includes(ndlaEnvironment)) {
return [
...defaultList,
'Innlandet fylkeskommune',
'Møre og Romsdal fylkeskommune',
'Vestfold og Telemark fylkeskommune',
'Viken fylkeskommune',
'Universitetet i Rogn',
];
}
return defaultList;
};

export const getDefaultLocale = () =>
getEnvironmentVariabel('NDLA_DEFAULT_LOCALE', 'nb');

Expand Down Expand Up @@ -139,7 +114,6 @@ export type ConfigType = {
matomoTagmanagerId: string;
isVercel: boolean;
monsidoToken: string;
allowedAIOrgs: string[];
};

const config: ConfigType = {
Expand Down Expand Up @@ -175,7 +149,6 @@ const config: ConfigType = {
matomoTagmanagerId: getEnvironmentVariabel('MATOMO_TAGMANAGER_ID', ''),
isVercel: getEnvironmentVariabel('IS_VERCEL', false),
monsidoToken: getEnvironmentVariabel('MONSIDO_TOKEN', ''),
allowedAIOrgs: allowedAIOrgs(),
};

export function getUniversalConfig() {
Expand Down
12 changes: 9 additions & 3 deletions src/containers/MyNdla/MyNdlaPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import { useContext, useEffect } from 'react';
import { useContext, useEffect, useMemo } from 'react';
import { useLocation } from 'react-router-dom';
import { Trans, useTranslation } from 'react-i18next';
import keyBy from 'lodash/keyBy';
Expand All @@ -28,7 +28,6 @@ import {
ModalTrigger,
ModalContent,
} from '@ndla/modal';
import config from '../../config';
import InfoPart, { InfoPartIcon, InfoPartText } from './InfoSection';
import { AuthContext } from '../../components/AuthenticationContext';
import {
Expand All @@ -44,6 +43,7 @@ import { useBaseName } from '../../components/BaseNameContext';
import { useDeletePersonalData } from './userMutations';
import { getAllDimensions } from '../../util/trackingUtil';
import { UserInfo } from './components/UserInfo';
import { useAiOrgs } from './configQueries';

const ShareIcon = InfoPartIcon.withComponent(Share);
const HeartOutlineIcon = InfoPartIcon.withComponent(HeartOutline);
Expand Down Expand Up @@ -116,6 +116,7 @@ const MyNdlaPage = () => {
const { trackPageView } = useTracker();
const { deletePersonalData } = useDeletePersonalData();
const { allFolderResources } = useRecentlyUsedResources();
const { data: aiData } = useAiOrgs();
const { data: metaData, loading } = useFolderResourceMetaSearch(
allFolderResources?.map((r) => ({
id: r.resourceId,
Expand Down Expand Up @@ -147,6 +148,11 @@ const MyNdlaPage = () => {

const aiLang = i18n.language === 'nn' ? 'nn' : '';

const allowedAiOrgs = useMemo(() => {
if (!aiData?.aiEnabledOrgs?.value) return [];
return aiData?.aiEnabledOrgs.value;
}, [aiData]);

return (
<StyledPageContentContainer>
<HelmetWithTracker title={t('htmlTitles.myNdlaPage')} />
Expand All @@ -155,7 +161,7 @@ const MyNdlaPage = () => {
<MyNdlaTitle title={t('myNdla.myPage.myPage')} />
</TitleWrapper>
<StyledDescription>{t('myNdla.myPage.welcome')}</StyledDescription>
{config.allowedAIOrgs.includes(user?.baseOrg?.displayName ?? '') && (
{allowedAiOrgs.includes(user?.baseOrg?.displayName ?? '') && (
<StyledBannerCard
link={`https://ai.ndla.no/${aiLang}`}
title={{
Expand Down
25 changes: 25 additions & 0 deletions src/containers/MyNdla/configQueries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2023-present, NDLA.
*
* This source code is licensed under the GPLv3 license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import { gql } from '@apollo/client';
import { GQLAiOrganizationsQuery } from '../../graphqlTypes';
import { useGraphQuery } from '../../util/runQueries';

export const configQuery = gql`
query AiOrganizations {
aiEnabledOrgs {
key
value
}
}
`;

export const useAiOrgs = () => {
const { data } = useGraphQuery<GQLAiOrganizationsQuery>(configQuery);
return { data };
};
12 changes: 12 additions & 0 deletions src/graphqlTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,7 @@ export type GQLProgrammePage = {

export type GQLQuery = {
__typename?: 'Query';
aiEnabledOrgs?: Maybe<GQLConfigMetaStringList>;
alerts?: Maybe<Array<Maybe<GQLUptimeAlert>>>;
allFolderResources: Array<GQLFolderResource>;
arenaCategories: Array<GQLArenaCategory>;
Expand Down Expand Up @@ -2665,6 +2666,17 @@ export type GQLArenaUserQuery = {
arenaUser?: { __typename?: 'ArenaUser' } & GQLArenaUserQueryFragmentFragment;
};

export type GQLAiOrganizationsQueryVariables = Exact<{ [key: string]: never }>;

export type GQLAiOrganizationsQuery = {
__typename?: 'Query';
aiEnabledOrgs?: {
__typename?: 'ConfigMetaStringList';
key: string;
value: Array<string>;
};
};

export type GQLFolderResourceFragmentFragment = {
__typename: 'FolderResource';
resourceId: string;
Expand Down
1 change: 1 addition & 0 deletions src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ type ProgrammePage {
}

type Query {
aiEnabledOrgs: ConfigMetaStringList
alerts: [UptimeAlert]
allFolderResources(size: Int): [FolderResource!]!
arenaCategories: [ArenaCategory!]!
Expand Down
6 changes: 6 additions & 0 deletions src/util/apiHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ const typePolicies: TypePolicies = {
MyNdlaPersonalData: {
keyFields: (obj) => obj.__typename,
},
ConfigMetaBoolean: {
keyFields: ['key'],
},
ConfigMetaStringList: {
keyFields: ['key'],
},
};

function getCache() {
Expand Down

0 comments on commit 244f8bd

Please sign in to comment.