1- import { IconButton , Menu , MenuItem , Switch , Typography , Box , Divider } from '@mui/material' ;
1+ import { IconButton , Menu , MenuItem , Switch , Typography , Box , Divider , TextField , Button } from '@mui/material' ;
22import SettingsIcon from '@mui/icons-material/Settings' ;
33import { useState , useEffect } from 'react' ;
44import axios from 'axios' ;
55
6+ interface PharmacyConfig {
7+ useIntermediary : boolean ;
8+ intermediaryUrl : string ;
9+ remsAdminUrl : string ;
10+ ehrUrl : string ;
11+ }
12+
613export default function ConfigToggle ( ) {
714 const [ anchorEl , setAnchorEl ] = useState < null | HTMLElement > ( null ) ;
8- const [ useIntermediary , setUseIntermediary ] = useState ( false ) ;
15+ const [ config , setConfig ] = useState < PharmacyConfig > ( {
16+ useIntermediary : false ,
17+ intermediaryUrl : '' ,
18+ remsAdminUrl : '' ,
19+ ehrUrl : '' ,
20+ } ) ;
921 const open = Boolean ( anchorEl ) ;
1022
11- // Load config on mount
23+ // Load config from backend on mount
1224 useEffect ( ( ) => {
13- const saved = localStorage . getItem ( 'useIntermediary' ) ;
14- if ( saved !== null ) {
15- setUseIntermediary ( saved === 'true' ) ;
16- }
25+ axios . get < PharmacyConfig > ( '/doctorOrders/api/config' )
26+ . then ( ( { data } ) => setConfig ( data ) )
27+ . catch ( ( ) => console . error ( 'Failed to load config' ) ) ;
1728 } , [ ] ) ;
1829
1930 const handleClick = ( event : React . MouseEvent < HTMLElement > ) => {
@@ -24,15 +35,15 @@ export default function ConfigToggle() {
2435 setAnchorEl ( null ) ;
2536 } ;
2637
27- const handleToggle = async ( ) => {
28- const newValue = ! useIntermediary ;
29- setUseIntermediary ( newValue ) ;
30- localStorage . setItem ( 'useIntermediary' , String ( newValue ) ) ;
38+ const handleToggle = ( ) => {
39+ setConfig ( prev => ( { ...prev , useIntermediary : ! prev . useIntermediary } ) ) ;
40+ } ;
3141
32- // Update backend
42+ const handleSave = async ( ) => {
3343 try {
34- await axios . post ( '/doctorOrders/api/config' , { useIntermediary : newValue } ) ;
35- console . log ( 'Configuration updated:' , newValue ? 'Using Intermediary' : 'Direct Connection' ) ;
44+ await axios . post ( '/doctorOrders/api/config' , config ) ;
45+ console . log ( 'Configuration updated:' , config ) ;
46+ handleClose ( ) ;
3647 } catch ( error ) {
3748 console . error ( 'Failed to update backend config:' , error ) ;
3849 }
@@ -55,7 +66,7 @@ export default function ConfigToggle() {
5566 open = { open }
5667 onClose = { handleClose }
5768 PaperProps = { {
58- sx : { minWidth : 280 , p : 1 }
69+ sx : { minWidth : 300 , p : 1 }
5970 } }
6071 >
6172 < Box sx = { { px : 2 , py : 1 } } >
@@ -66,19 +77,44 @@ export default function ConfigToggle() {
6677 < Divider />
6778 < MenuItem onClick = { handleToggle } sx = { { py : 1.5 } } >
6879 < Box sx = { { display : 'flex' , alignItems : 'center' , gap : 2 , width : '100%' } } >
69- < Switch checked = { useIntermediary } size = "small" />
80+ < Switch checked = { config . useIntermediary } size = "small" />
7081 < Box >
7182 < Typography variant = "body2" fontWeight = "medium" >
7283 Use Intermediary
7384 </ Typography >
7485 < Typography variant = "caption" color = "text.secondary" >
75- { useIntermediary
76- ? 'Routing via intermediary'
77- : 'Direct to REMS Admin' }
86+ { config . useIntermediary ? 'Routing via intermediary' : 'Direct to REMS Admin' }
7887 </ Typography >
7988 </ Box >
8089 </ Box >
8190 </ MenuItem >
91+ < Divider />
92+ < Box sx = { { px : 2 , py : 1.5 , display : 'flex' , flexDirection : 'column' , gap : 1.5 } } >
93+ < TextField
94+ label = "Intermediary URL"
95+ size = "small"
96+ fullWidth
97+ value = { config . intermediaryUrl }
98+ onChange = { e => setConfig ( prev => ( { ...prev , intermediaryUrl : e . target . value } ) ) }
99+ />
100+ < TextField
101+ label = "REMS Admin URL"
102+ size = "small"
103+ fullWidth
104+ value = { config . remsAdminUrl }
105+ onChange = { e => setConfig ( prev => ( { ...prev , remsAdminUrl : e . target . value } ) ) }
106+ />
107+ < TextField
108+ label = "EHR URL"
109+ size = "small"
110+ fullWidth
111+ value = { config . ehrUrl }
112+ onChange = { e => setConfig ( prev => ( { ...prev , ehrUrl : e . target . value } ) ) }
113+ />
114+ < Button variant = "contained" size = "small" onClick = { handleSave } sx = { { alignSelf : 'flex-end' } } >
115+ Save
116+ </ Button >
117+ </ Box >
82118 </ Menu >
83119 </ >
84120 ) ;
0 commit comments