Skip to content

Commit

Permalink
Import services methods in object rather than separately (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarsh-scottlogic authored Feb 7, 2024
1 parent 8c4e2c9 commit 8f84648
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 46 deletions.
10 changes: 4 additions & 6 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import MissionInformation from './components/Overlay/MissionInformation';
import OverlayWelcome from './components/Overlay/OverlayWelcome';
import ResetProgressOverlay from './components/Overlay/ResetProgress';
import { LEVEL_NAMES, LevelSystemRole } from './models/level';
import { getValidModels } from './service/chatService';
import { resetAllLevelProgress } from './service/levelService';
import { getSystemRoles } from './service/systemRoleService';
import { chatService, levelService, systemRoleService } from './service';

import './App.css';
import './Theme.css';
Expand Down Expand Up @@ -79,8 +77,8 @@ function App() {
try {
console.log("Initializing app's backend data");
const [models, roles] = await Promise.all([
getValidModels(),
getSystemRoles(),
chatService.getValidModels(),
systemRoleService.getSystemRoles(),
]);
setChatModels(models);
setSystemRoles(roles);
Expand Down Expand Up @@ -239,7 +237,7 @@ function App() {
console.log('resetting progress for all levels');

// reset on the backend
await resetAllLevelProgress();
await levelService.resetAllLevelProgress();

localStorage.setItem('numCompletedLevels', '0');
setNumCompletedLevels(0);
Expand Down
13 changes: 5 additions & 8 deletions frontend/src/components/ChatBox/ChatBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import useUnitStepper from '@src/hooks/useUnitStepper';
import { ChatMessage, ChatResponse } from '@src/models/chat';
import { EmailInfo } from '@src/models/email';
import { LEVEL_NAMES } from '@src/models/level';
import {
addInfoMessageToChatHistory,
sendMessage,
} from '@src/service/chatService';
import { chatService } from '@src/service';

import ChatBoxFeed from './ChatBoxFeed';
import ChatBoxInput from './ChatBoxInput';
Expand Down Expand Up @@ -127,7 +124,7 @@ function ChatBox({
message: alertMsg,
});
// asynchronously add the message to the chat history
void addInfoMessageToChatHistory(
void chatService.addInfoMessageToChatHistory(
alertMsg,
'DEFENCE_ALERTED',
currentLevel
Expand All @@ -147,7 +144,7 @@ function ChatBox({
message: triggerMsg,
});
// asynchronously add the message to the chat history
void addInfoMessageToChatHistory(
void chatService.addInfoMessageToChatHistory(
triggerMsg,
'DEFENCE_TRIGGERED',
currentLevel
Expand All @@ -166,7 +163,7 @@ function ChatBox({
message: successMessage,
});
// asynchronously add the message to the chat history
void addInfoMessageToChatHistory(
void chatService.addInfoMessageToChatHistory(
successMessage,
'LEVEL_INFO',
currentLevel
Expand All @@ -188,7 +185,7 @@ function ChatBox({
});

try {
const response: ChatResponse = await sendMessage(
const response: ChatResponse = await chatService.sendMessage(
chatInput,
currentLevel
);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/DefenceBox/DefenceConfiguration.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ThemedButton from '@src/components/ThemedButtons/ThemedButton';
import { Defence, DefenceConfigItem } from '@src/models/defence';
import { validateDefence } from '@src/service/defenceService';
import { defenceService } from '@src/service';

import DefenceConfigurationInput from './DefenceConfigurationInput';

Expand All @@ -27,7 +27,7 @@ function DefenceConfiguration({
const supportText = `reset ${config.name} to default`;

function validateNewInput(value: string) {
return validateDefence(defence.id, config.id, value);
return defenceService.validateDefence(defence.id, config.id, value);
}

return (
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/components/DocumentViewer/DocumentViewBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import DocViewer, { DocViewerRenderers } from '@cyntler/react-doc-viewer';
import { useEffect, useState } from 'react';

import { DocumentMeta } from '@src/models/document';
import { getDocumentMetas } from '@src/service/documentService';
import { documentService } from '@src/service';

import DocumentViewBoxHeader from './DocumentViewBoxHeader';

Expand All @@ -17,7 +17,8 @@ function DocumentViewBox({ closeOverlay }: { closeOverlay: () => void }) {
// on mount get document uris
useEffect(() => {
const abortController = new AbortController();
void getDocumentMetas(abortController.signal)
void documentService
.getDocumentMetas(abortController.signal)
.then((uris) => {
setDocumentMetas(uris);
})
Expand Down
55 changes: 33 additions & 22 deletions frontend/src/components/MainComponent/MainComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,11 @@ import { DEFENCE_ID, DefenceConfigItem, Defence } from '@src/models/defence';
import { EmailInfo } from '@src/models/email';
import { LEVEL_NAMES } from '@src/models/level';
import {
addInfoMessageToChatHistory,
clearChat,
getChatHistory,
} from '@src/service/chatService';
import {
toggleDefence,
configureDefence,
getDefences,
resetDefenceConfig,
} from '@src/service/defenceService';
import { clearEmails, getSentEmails } from '@src/service/emailService';
import { healthCheck } from '@src/service/healthService';
chatService,
defenceService,
emailService,
healthService,
} from '@src/service';

import MainBody from './MainBody';
import MainFooter from './MainFooter';
Expand Down Expand Up @@ -64,7 +57,7 @@ function MainComponent({
// called on mount
useEffect(() => {
// perform backend health check
healthCheck().catch(() => {
healthService.healthCheck().catch(() => {
// addErrorMessage('Failed to reach the server. Please try again later.');
setMessages([
{
Expand Down Expand Up @@ -112,7 +105,10 @@ function MainComponent({
// for clearing single level progress
async function resetLevel() {
// reset on the backend
await Promise.all([clearChat(currentLevel), clearEmails(currentLevel)]);
await Promise.all([
chatService.clearChat(currentLevel),
emailService.clearEmails(currentLevel),
]);

resetFrontendState();
addResetMessage();
Expand All @@ -121,10 +117,10 @@ function MainComponent({
// for going switching level without clearing progress
async function setNewLevel(newLevel: LEVEL_NAMES) {
// get emails for new level from the backend
setEmails(await getSentEmails(newLevel));
setEmails(await emailService.getSentEmails(newLevel));

// get chat history for new level from the backend
const retrievedMessages = await getChatHistory(newLevel);
const retrievedMessages = await chatService.getChatHistory(newLevel);

// add welcome message for levels only
newLevel !== LEVEL_NAMES.SANDBOX
Expand All @@ -134,7 +130,7 @@ function MainComponent({
const defences =
newLevel === LEVEL_NAMES.LEVEL_3 ? DEFENCES_SHOWN_LEVEL3 : ALL_DEFENCES;
// fetch defences from backend
const remoteDefences = await getDefences(newLevel);
const remoteDefences = await defenceService.getDefences(newLevel);
defences.map((localDefence) => {
const matchingRemoteDefence = remoteDefences.find((remoteDefence) => {
return localDefence.id === remoteDefence.id;
Expand Down Expand Up @@ -164,7 +160,11 @@ function MainComponent({
type: 'GENERIC_INFO',
});
// asynchronously add message to chat history
void addInfoMessageToChatHistory(message, 'GENERIC_INFO', currentLevel);
void chatService.addInfoMessageToChatHistory(
message,
'GENERIC_INFO',
currentLevel
);
}

function addConfigUpdateToChat(defenceId: DEFENCE_ID, update: string) {
Expand All @@ -176,7 +176,10 @@ function MainComponent({
defenceId: DEFENCE_ID,
configId: string
) {
const resetDefence = await resetDefenceConfig(defenceId, configId);
const resetDefence = await defenceService.resetDefenceConfig(
defenceId,
configId
);
addConfigUpdateToChat(defenceId, 'reset');
// update state
const newDefences = defencesToShow.map((defence) => {
Expand All @@ -193,7 +196,11 @@ function MainComponent({
}

async function setDefenceToggle(defence: Defence) {
await toggleDefence(defence.id, defence.isActive, currentLevel);
await defenceService.toggleDefence(
defence.id,
defence.isActive,
currentLevel
);

const newDefenceDetails = defencesToShow.map((defenceDetail) => {
if (defenceDetail.id === defence.id) {
Expand All @@ -212,7 +219,11 @@ function MainComponent({
defenceId: DEFENCE_ID,
config: DefenceConfigItem[]
) {
const success = await configureDefence(defenceId, config, currentLevel);
const success = await defenceService.configureDefence(
defenceId,
config,
currentLevel
);
if (success) {
addConfigUpdateToChat(defenceId, 'updated');
// update state
Expand Down Expand Up @@ -248,7 +259,7 @@ function MainComponent({
message: `Level progress reset`,
type: 'RESET_LEVEL',
};
void addInfoMessageToChatHistory(
void chatService.addInfoMessageToChatHistory(
resetMessage.message,
resetMessage.type,
currentLevel
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/components/ModelBox/ModelConfiguration.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react';

import { CustomChatModelConfiguration, MODEL_CONFIG } from '@src/models/chat';
import { configureGptModel, getGptModel } from '@src/service/chatService';
import { chatService } from '@src/service';

import ModelConfigurationSlider from './ModelConfigurationSlider';

Expand Down Expand Up @@ -70,7 +70,7 @@ function ModelConfiguration({

setCustomChatModelByID(id, newValue);

void configureGptModel(id, newValue).then((success) => {
void chatService.configureGptModel(id, newValue).then((success) => {
if (success) {
addInfoMessage(`changed ${id} to ${newValue}`);
} else {
Expand All @@ -81,7 +81,8 @@ function ModelConfiguration({

// get model configs on mount
useEffect(() => {
getGptModel()
chatService
.getGptModel()
.then((model) => {
// apply the currently set values
const newCustomChatModelConfigs = customChatModelConfigs.map(
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/components/ModelBox/ModelSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { useEffect, useState } from 'react';

import LoadingButton from '@src/components/ThemedButtons/LoadingButton';
import { setGptModel, getGptModel } from '@src/service/chatService';
import { chatService } from '@src/service';

import './ModelSelection.css';

Expand All @@ -29,7 +29,7 @@ function ModelSelection({
const currentSelectedModel = selectedModel;
console.log(`selected model: ${currentSelectedModel}`);
setIsSettingModel(true);
const modelUpdated = await setGptModel(currentSelectedModel);
const modelUpdated = await chatService.setGptModel(currentSelectedModel);
setIsSettingModel(false);
if (modelUpdated) {
setModelInUse(currentSelectedModel);
Expand All @@ -43,7 +43,8 @@ function ModelSelection({

// get the model
useEffect(() => {
getGptModel()
chatService
.getGptModel()
.then((model) => {
setModelInUse(model.id);
// default the dropdown selection to the model in use
Expand Down
17 changes: 17 additions & 0 deletions frontend/src/service/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as chatService from './chatService';
import * as defenceService from './defenceService';
import * as documentService from './documentService';
import * as emailService from './emailService';
import * as healthService from './healthService';
import * as levelService from './levelService';
import * as systemRoleService from './systemRoleService';

export {
chatService,
defenceService,
documentService,
emailService,
healthService,
levelService,
systemRoleService,
};

0 comments on commit 8f84648

Please sign in to comment.