diff --git a/src/lib/components/order/ordertest/config/ordertest-form-config.js b/src/lib/components/order/ordertest/config/ordertest-form-config.js index 82d3000..9a386b8 100644 --- a/src/lib/components/order/ordertest/config/ordertest-form-config.js +++ b/src/lib/components/order/ordertest/config/ordertest-form-config.js @@ -148,7 +148,7 @@ export const orderTestFormFields = [ type: "tests2", optionsEndpoint: `${API.BASE_URL}${API.TEST}`, otherEndpoint: `${API.BASE_URL}${API.DISCIPLINE}`, - valueKey: 'TestSiteCode', + valueKey: 'TestSiteID', labelKey: (item) => `${item.TestSiteCode} - ${item.TestSiteName}`, validateOn: ['input'] }, diff --git a/src/lib/components/reusable/form/order-form-renderer.svelte b/src/lib/components/reusable/form/order-form-renderer.svelte index 2fae549..ba69d45 100644 --- a/src/lib/components/reusable/form/order-form-renderer.svelte +++ b/src/lib/components/reusable/form/order-form-renderer.svelte @@ -46,7 +46,28 @@ ); let discipline = $state([]); let selectedDiscipline = $state(null); - $inspect(discipline) + let searchText = $state(''); + + let searchedTests = $derived.by(() => { + if (!searchText.trim()) return filteredTests; + + const query = searchText.toLowerCase().trim(); + const result = {}; + + for (const [disciplineId, { disciplineName, tests }] of Object.entries(filteredTests)) { + const matched = tests.filter(test => + test.rawItem.TestSiteName.toLowerCase().includes(query) || + test.rawItem.TestSiteCode.toLowerCase().includes(query) + ); + + if (matched.length > 0) { + result[disciplineId] = { disciplineName, tests: matched }; + } + } + + return result; + }); + function getFilteredOptions(key) { const query = searchQuery[key] || ''; if (!query) return formState.selectOptions?.[key] ?? []; @@ -127,11 +148,34 @@ if (col.type === 'tests2' && col.otherEndpoint) { fetchDiscipline(col.otherEndpoint); } + if (col.type === "tests2" && col.optionsEndpoint) { + formState.fetchOptions(col, formState.form); + } } } } }); }); + + let testsByDiscipline = $derived.by(() => { + return (formState.selectOptions?.Tests ?? []).reduce((acc, test) => { + const id = test.rawItem.DisciplineID; + if (!acc[id]) { + acc[id] = { disciplineName: test.rawItem.DisciplineName, tests: [] }; + } + acc[id].tests.push(test); + return acc; + }, {}); + }); + + let filteredTests = $derived.by(() => { + if (!selectedDiscipline) return testsByDiscipline; + + return Object.fromEntries( + Object.entries(testsByDiscipline).filter(([disciplineId]) => disciplineId === selectedDiscipline) + ); + }); + $inspect(searchedTests) {#snippet Fieldset({ @@ -355,19 +399,7 @@ {:else if type === "tests2"} - {@const allTests = formState.selectOptions?.[key] ?? []} - {@const _ = console.log('allTests', allTests)} - {@const disciplineList = [ - ...new Map( - allTests - .filter(t => t.DisciplineID !== null) - .map(t => [t.DisciplineID, { id: t.DisciplineID, name: t.DisciplineName }]) - ).values() - ]} {@const filteredOptions = getFilteredOptions(key)} - {@const filteredTests = selectedDiscipline - ? allTests.filter(t => t.DisciplineID === selectedDiscipline) - : allTests}