Skip to content

Commit

Permalink
Merge pull request #166 from craigybaeb/dev
Browse files Browse the repository at this point in the history
#161 Syncing BT editor with cockpit
  • Loading branch information
craigybaeb authored Oct 27, 2023
2 parents 7e2170d + 0180444 commit ad72712
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 10 deletions.
75 changes: 68 additions & 7 deletions src/components/iSee/persona/PersonaIntents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,23 +50,25 @@ export type PersonaType = {
updatePersona: any;
ontoExplainers: any;
ontoValues?: API.OntoParams
personas: Persona[];

};

const PersonaIntents: React.FC<PersonaType> = (props) => {
const { persona, updatePersona, usecaseId } = props;
const { persona, updatePersona, usecaseId, personas } = props;
const [personaState, setPersonaState] = useState(persona);
const [chosenStrategy, setChosenStrategy] = useState<any>();

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 (<div>
{!intent_status && <Tag color="red">Incomplete Intent</Tag>}
{intent_status && <Tag color="success">Completed Intent</Tag>}

<Popconfirm
title={'Are you sure to delete?'}
title={'Are you sure that you want to delete?'}
onConfirm={async () => {
const temp = personaState.intents?.filter((i) => i.id !== intent.id);

Expand Down Expand Up @@ -170,6 +172,7 @@ const PersonaIntents: React.FC<PersonaType> = (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) {
Expand Down Expand Up @@ -222,6 +225,63 @@ const PersonaIntents: React.FC<PersonaType> = (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({
Expand Down Expand Up @@ -269,8 +329,9 @@ const PersonaIntents: React.FC<PersonaType> = (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) => {
Expand Down Expand Up @@ -423,7 +484,7 @@ const PersonaIntents: React.FC<PersonaType> = (props) => {
</Button>
</p>
<p style={{ marginTop: -10 }}>
<Button block onClick={() => openEditor(strategy.tree, usecaseId)} target="_blank" type="dashed" shape="round" icon={<EditOutlined />} >
<Button block onClick={() => openEditor(strategy, usecaseId)} target="_blank" type="dashed" shape="round" icon={<EditOutlined />} >
Edit
</Button>
</p></>
Expand Down
1 change: 1 addition & 0 deletions src/components/iSee/persona/PersonaTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const PersonaTabs: React.FC<PersonaType> = (props) => {
key={'pi-' + persona._id}
usecaseId={usecaseId}
persona={persona}
personas={personas}
updatePersona={updatePersona}
/>
</Panel>
Expand Down
19 changes: 18 additions & 1 deletion src/services/isee/editor.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 [];
}
};
4 changes: 2 additions & 2 deletions src/services/isee/usecases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,15 @@ 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',
'x-access-token': getToken(),
},
body: JSON.stringify(intent),
});
return usecaseId;
return UPDATED_PERSONA.json();
} catch (error) {
return usecaseId;
}
Expand Down

0 comments on commit ad72712

Please sign in to comment.