11import React , { useState , useEffect } from 'react' ;
22import { useApi } from '../contexts/ApiContext' ;
3+ import { useAuth } from '../contexts/AuthContext' ;
34import toast from 'react-hot-toast' ;
45import { Globe , Key , Settings , Zap , AlertCircle , CheckCircle , X } from 'lucide-react' ;
56
@@ -24,15 +25,22 @@ const PublishingForm = ({ modelName, onComplete, onCancel }) => {
2425 const [ isPublished , setIsPublished ] = useState ( false ) ;
2526 const [ modelDetails , setModelDetails ] = useState ( null ) ;
2627 const [ tenantInfo , setTenantInfo ] = useState ( null ) ;
28+ const [ tenants , setTenants ] = useState ( [ ] ) ;
2729 const api = useApi ( ) ;
30+ const { user } = useAuth ( ) ;
2831
2932 useEffect ( ( ) => {
3033 if ( modelName ) {
3134 fetchModelDetails ( ) ;
3235 fetchTenantInfo ( ) ;
3336 checkIfPublished ( ) ;
37+
38+ // If user is admin, fetch available tenants
39+ if ( user ?. isAdmin ) {
40+ fetchTenants ( ) ;
41+ }
3442 }
35- } , [ modelName ] ) ;
43+ } , [ modelName , user ] ) ;
3644
3745 const fetchModelDetails = async ( ) => {
3846 try {
@@ -48,12 +56,31 @@ const PublishingForm = ({ modelName, onComplete, onCancel }) => {
4856 try {
4957 const response = await api . getTenantInfo ( ) ;
5058 setTenantInfo ( response . data ) ;
51- setFormData ( prev => ( { ...prev , tenantId : response . data . tenant } ) ) ;
59+ // Only set the default tenant if admin hasn't selected one
60+ if ( ! user ?. isAdmin ) {
61+ setFormData ( prev => ( { ...prev , tenantId : response . data . tenant } ) ) ;
62+ }
5263 } catch ( error ) {
5364 console . error ( 'Error fetching tenant info:' , error ) ;
5465 }
5566 } ;
5667
68+ const fetchTenants = async ( ) => {
69+ try {
70+ const response = await api . getTenants ( ) ;
71+ setTenants ( response . data . tenants || [ ] ) ;
72+
73+ // Set default tenant for admin (first tenant or current user tenant)
74+ if ( response . data . tenants && response . data . tenants . length > 0 ) {
75+ const defaultTenant = tenantInfo ?. tenant || response . data . tenants [ 0 ] . id ;
76+ setFormData ( prev => ( { ...prev , tenantId : defaultTenant } ) ) ;
77+ }
78+ } catch ( error ) {
79+ console . error ( 'Error fetching tenants:' , error ) ;
80+ toast . error ( 'Failed to fetch tenant list' ) ;
81+ }
82+ } ;
83+
5784 const checkIfPublished = async ( ) => {
5885 try {
5986 const response = await api . getPublishedModel ( modelName ) ;
@@ -97,9 +124,17 @@ const PublishingForm = ({ modelName, onComplete, onCancel }) => {
97124 setLoading ( true ) ;
98125
99126 try {
100- const response = await api . publishModel ( modelName , {
127+ // Add tenant parameter for admin users
128+ const requestBody = {
101129 config : formData
102- } ) ;
130+ } ;
131+
132+ // If admin is publishing to a different tenant, include tenant in request
133+ if ( user ?. isAdmin && formData . tenantId !== tenantInfo ?. tenant ) {
134+ requestBody . config . tenantID = formData . tenantId ;
135+ }
136+
137+ const response = await api . publishModel ( modelName , requestBody ) ;
103138
104139 toast . success ( 'Model published successfully!' ) ;
105140 onComplete ( response . data ) ;
@@ -118,7 +153,11 @@ const PublishingForm = ({ modelName, onComplete, onCancel }) => {
118153
119154 try {
120155 setLoading ( true ) ;
121- await api . unpublishModel ( modelName ) ;
156+
157+ // Include namespace parameter for admin users
158+ const namespace = user ?. isAdmin ? formData . tenantId : null ;
159+ await api . unpublishModel ( modelName , namespace ) ;
160+
122161 toast . success ( 'Model unpublished successfully!' ) ;
123162 onComplete ( null ) ;
124163 } catch ( error ) {
@@ -184,13 +223,35 @@ const PublishingForm = ({ modelName, onComplete, onCancel }) => {
184223
185224 < div className = "form-group" >
186225 < label > Tenant</ label >
187- < input
188- type = "text"
189- value = { formData . tenantId }
190- className = "form-control"
191- disabled
192- style = { { backgroundColor : '#f8fafc' , color : '#6b7280' } }
193- />
226+ { user ?. isAdmin ? (
227+ < select
228+ name = "tenantId"
229+ value = { formData . tenantId }
230+ onChange = { handleInputChange }
231+ className = "form-control"
232+ required
233+ >
234+ < option value = "" > Select a tenant</ option >
235+ { tenants . map ( tenant => (
236+ < option key = { tenant . id } value = { tenant . id } >
237+ { tenant . name || tenant . id }
238+ </ option >
239+ ) ) }
240+ </ select >
241+ ) : (
242+ < input
243+ type = "text"
244+ value = { formData . tenantId }
245+ className = "form-control"
246+ disabled
247+ style = { { backgroundColor : '#f8fafc' , color : '#6b7280' } }
248+ />
249+ ) }
250+ { user ?. isAdmin && (
251+ < div style = { { fontSize : '0.875rem' , color : '#6b7280' , marginTop : '0.25rem' } } >
252+ Select the tenant namespace where the model should be published
253+ </ div >
254+ ) }
194255 </ div >
195256 </ div >
196257
0 commit comments