From 66c9f93d4b9f8c03ca9ee91917ad408aa0e0eafc Mon Sep 17 00:00:00 2001 From: Craig Pirie Date: Thu, 26 Oct 2023 18:37:33 +0100 Subject: [PATCH] Synced BT editor with cockpit --- .../iSee/persona/PersonaIntents.tsx | 75 +++++++++++++++++-- src/components/iSee/persona/PersonaTabs.tsx | 1 + src/services/isee/editor.ts | 19 ++++- src/services/isee/usecases.ts | 4 +- 4 files changed, 89 insertions(+), 10 deletions(-) diff --git a/src/components/iSee/persona/PersonaIntents.tsx b/src/components/iSee/persona/PersonaIntents.tsx index b458d33..d47fda0 100644 --- a/src/components/iSee/persona/PersonaIntents.tsx +++ b/src/components/iSee/persona/PersonaIntents.tsx @@ -39,7 +39,7 @@ import QuestionnaireTab from '../question/QuestionnaireTab'; import DATA_FILEDS from '@/models/common'; import { IntentQuestion } from '@/models/questionnaire'; import TOOL_TIPS from '@/models/tooltips'; -import { open_editor_with_token } from '@/services/isee/editor'; +import { open_editor_with_token, refresh_reuse_cases } from '@/services/isee/editor'; const { Panel } = Collapse; const { Option, OptGroup } = Select; @@ -50,23 +50,25 @@ export type PersonaType = { updatePersona: any; ontoExplainers: any; ontoValues?: API.OntoParams + personas: Persona[]; }; const PersonaIntents: React.FC = (props) => { - const { persona, updatePersona, usecaseId } = props; + const { persona, updatePersona, usecaseId, personas } = props; const [personaState, setPersonaState] = useState(persona); + const [chosenStrategy, setChosenStrategy] = useState(); const genIntentStatus = (intent: PersonaIntent) => { let intent_status = intent.strategy_selected && (intent?.evaluation?.questions && intent?.evaluation?.questions?.length > 0) || false; - console.log("genIntentStatus", intent_status) + console.log("genIntentStatus", intent_status); return (
{!intent_status && Incomplete Intent} {intent_status && Completed Intent} { const temp = personaState.intents?.filter((i) => i.id !== intent.id); @@ -170,6 +172,7 @@ const PersonaIntents: React.FC = (props) => { const strategies = await api_persona_query_strategies(usecaseId, personaState._id, intent.id, loadMore); intent.strategies = strategies intent.strategy_selected = false + hide(); if (strategies.length > 0) { @@ -222,6 +225,63 @@ const PersonaIntents: React.FC = (props) => { })); }; + useEffect(() => { + const handleVisibilityChange = () => { + if (document.visibilityState === 'visible' && chosenStrategy?.tree !== "" && chosenStrategy?.tree !== undefined) { + + refresh_reuse_cases(chosenStrategy.tree).then(methods => { + + + let selectedIntent: PersonaIntent = { + id: "", + completed: false, + name: "", + evaluation: {} + }; + + let intent_idx = 0; + personaState?.intents?.forEach((intent_, i) => { + intent_.strategies?.forEach((strat) => { + if (strat.id == chosenStrategy?.id) { + selectedIntent = intent_; + intent_idx = i; + } + }) + }); + + const UPDATED_STRATEGIES = personaState.intents?.[intent_idx].strategies?.map((s) => { + if (s.tree === chosenStrategy.tree) { + s.methods = methods; + } + return s; + }); + + const UPDATED_INTENTS = personaState.intents; + + if (typeof UPDATED_INTENTS !== "undefined") { + UPDATED_INTENTS[intent_idx].strategies = UPDATED_STRATEGIES; + } + + selectedIntent.strategies = UPDATED_STRATEGIES; + + api_persona_update_intent(usecaseId, personaState._id, selectedIntent.id, selectedIntent).then(res => { + + const PERSONA_IDX = personas.findIndex(p => p._id === persona._id); + setPersonaState(res.personas[PERSONA_IDX]); + }); + + }); + + } + }; + + document.addEventListener('visibilitychange', handleVisibilityChange); + + return () => { + document.removeEventListener('visibilitychange', handleVisibilityChange); + }; + }, [chosenStrategy, personaState, updatePersona, usecaseId]); + const setSelectedStrategy = async (event: any, strategy: any) => { message.config({ @@ -269,8 +329,9 @@ const PersonaIntents: React.FC = (props) => { } }; - const openEditor = (strategy: string, useCaseId: string) => { - open_editor_with_token(strategy, useCaseId) + const openEditor = (strategy: any, useCaseId: string) => { + setChosenStrategy(strategy); + open_editor_with_token(strategy.tree, useCaseId) } const onFinishNewIntent = async (values: any) => { @@ -423,7 +484,7 @@ const PersonaIntents: React.FC = (props) => {

-

diff --git a/src/components/iSee/persona/PersonaTabs.tsx b/src/components/iSee/persona/PersonaTabs.tsx index c5498df..f07eb34 100644 --- a/src/components/iSee/persona/PersonaTabs.tsx +++ b/src/components/iSee/persona/PersonaTabs.tsx @@ -84,6 +84,7 @@ const PersonaTabs: React.FC = (props) => { key={'pi-' + persona._id} usecaseId={usecaseId} persona={persona} + personas={personas} updatePersona={updatePersona} /> diff --git a/src/services/isee/editor.ts b/src/services/isee/editor.ts index 792e700..9892f40 100644 --- a/src/services/isee/editor.ts +++ b/src/services/isee/editor.ts @@ -1,6 +1,6 @@ // @ts-ignore -import { EDITOR_URL } from './api.config'; +import { EDITOR_URL, BASE_URL } from './api.config'; import { getToken } from './user'; import Cookies from 'js-cookie'; @@ -18,4 +18,21 @@ export const open_editor_with_token = async (strategy: string, usecaseId: string console.log(error) return []; } +}; + +export const refresh_reuse_cases = async (strategy: string) => { + try { + const STRATEGY_URL = `${BASE_URL}/trees/Projects/${strategy}/methods`; + const data = await fetch(STRATEGY_URL, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'x-access-token': getToken() + }, + }); + const result = await data.json(); + return result || []; + } catch (error) { + return []; + } }; \ No newline at end of file diff --git a/src/services/isee/usecases.ts b/src/services/isee/usecases.ts index 5deb81a..dd74b88 100644 --- a/src/services/isee/usecases.ts +++ b/src/services/isee/usecases.ts @@ -383,7 +383,7 @@ export async function api_persona_update_intent( if (!usecaseId || !personaId || !intentId) return usecaseId; try { - await fetch(`${BASE_URL}/${KEY}/${usecaseId}/persona/${personaId}/intent/${intentId}`, { + const UPDATED_PERSONA = await fetch(`${BASE_URL}/${KEY}/${usecaseId}/persona/${personaId}/intent/${intentId}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', @@ -391,7 +391,7 @@ export async function api_persona_update_intent( }, body: JSON.stringify(intent), }); - return usecaseId; + return UPDATED_PERSONA.json(); } catch (error) { return usecaseId; }