@@ -12,7 +12,7 @@ import PCModalContent from './PCModalContent';
1212import { ExampleType , File , QuestionSolution , WorkflowType } from './types' ;
1313import FileSelectorButton from './FileSelectorButton' ;
1414
15- import { fetchFileContent , getExampleType , useGetExamplesByUseCase } from './hooks' ;
15+ import { fetchExamplesByUseCase , fetchFileContent , getExampleType , useGetExamplesByUseCase } from './hooks' ;
1616import { useState } from 'react' ;
1717import FreeFormExampleTable from './FreeFormExampleTable' ;
1818import { L } from 'vitest/dist/chunks/reporters.DTtkbAtP.js' ;
@@ -54,13 +54,21 @@ const Examples: FunctionComponent = () => {
5454 const form = Form . useFormInstance ( ) ;
5555 const [ records , setRecords ] = useState < Record < string , string > [ ] > ( [ ] ) ;
5656 const workflowType = form . getFieldValue ( 'workflow_type' ) ;
57- const { examples, isLoading : examplesLoading , refetch } =
58- useGetExamplesByUseCase ( form . getFieldValue ( 'use_case' ) ) ;
5957
6058 const mutation = useMutation ( {
6159 mutationFn : fetchFileContent
6260 } ) ;
6361
62+ const restore_mutation = useMutation ( {
63+ mutationFn : fetchExamplesByUseCase
64+ } ) ;
65+
66+ useEffect ( ( ) => {
67+ const useCase = form . getFieldValue ( 'use_case' ) ;
68+ restore_mutation . mutate ( useCase ) ;
69+ } , [ form . getFieldValue ( 'use_case' ) ] ) ;
70+
71+
6472 useEffect ( ( ) => {
6573 const example_path = form . getFieldValue ( 'example_path' ) ;
6674 if ( ! isEmpty ( example_path ) ) {
@@ -70,21 +78,28 @@ const Examples: FunctionComponent = () => {
7078 }
7179 } , [ form . getFieldValue ( 'example_path' ) , form . getFieldValue ( 'workflow_type' ) ] ) ;
7280
73- useEffect ( ( ) => {
74- console . log ( '------------------> useEffect' )
81+ useEffect ( ( ) => {
7582 if ( ! isEmpty ( mutation . data ) ) {
7683 form . setFieldValue ( 'examples' , mutation . data ) ;
7784 if ( ! isEqual ( mutation . data , records ) ) {
7885 setRecords ( mutation . data ) ;
7986 }
8087
81- } else if ( Array . isArray ( examples ) && ! isEqual ( examples , records ) ) {
88+ }
89+ } , [ mutation . data ] ) ;
90+
91+ useEffect ( ( ) => {
92+ if ( ! isEmpty ( restore_mutation . data ) ) {
93+ const examples = get ( restore_mutation . data , 'examples' , [ ] ) ;
8294 form . setFieldValue ( 'examples' , examples ) ;
8395 setRecords ( examples ) ;
8496 }
85- } , [ mutation . data , examples ] ) ;
86-
97+ } , [ restore_mutation . data ] ) ;
8798
99+ const onRestoreDefaults = async ( ) => {
100+ const useCase = form . getFieldValue ( 'use_case' ) ;
101+ restore_mutation . mutate ( useCase ) ;
102+ }
88103
89104 const onAddFiles = ( files : File [ ] ) => {
90105 if ( ! isEmpty ( files ) ) {
@@ -104,16 +119,16 @@ const Examples: FunctionComponent = () => {
104119 span : 10
105120 } ;
106121
107- const showEmptyState = workflowType === WorkflowType . FREE_FORM_DATA_GENERATION &&
122+ const showEmptyState = ( workflowType === WorkflowType . FREE_FORM_DATA_GENERATION &&
108123 isEmpty ( mutation . data ) &&
109- records . length === 0 ;
124+ records . length === 0 ) ||
125+ ( form . getFieldValue ( 'use_case' ) === 'custom' &&
126+ isEmpty ( form . getFieldValue ( 'examples' ) ) ) ;
110127
111- console . log ( 'examples' , form . getFieldValue ( 'examples' ) ) ;
112- console . log ( 'records' , records ) ;
113128
114129 return (
115130 < Container >
116- { examplesLoading && < Loading /> }
131+ { mutation ?. isPending || restore_mutation . isPending && < Loading /> }
117132 < Header align = 'center' justify = 'space-between' >
118133 < StyledTitle level = { 3 } >
119134 < Space >
@@ -149,8 +164,8 @@ const Examples: FunctionComponent = () => {
149164 < ModalButtonGroup gap = { 8 } justify = 'end' >
150165 < Button onClick = { ( ) => Modal . destroyAll ( ) } > { 'Cancel' } </ Button >
151166 < Button
152- onClick = { ( ) => {
153- refetch ( ) ;
167+ onClick = { async ( ) => {
168+ await onRestoreDefaults ( ) ;
154169 Modal . destroyAll ( ) ;
155170 } }
156171 type = 'primary'
0 commit comments