|
|
@@ -23,6 +23,7 @@ import {
|
|
|
Building2,
|
|
|
Eye,
|
|
|
Info,
|
|
|
+ Loader2,
|
|
|
Pencil,
|
|
|
Plus,
|
|
|
Power,
|
|
|
@@ -248,6 +249,7 @@ export function Tenants() {
|
|
|
const [inviteOpen, setInviteOpen] = useState(false)
|
|
|
const [invitePlatformId, setInvitePlatformId] = useState('')
|
|
|
const [currentTenant, setCurrentTenant] = useState<Tenant | null>(null)
|
|
|
+ const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
const [submitAction, setSubmitAction] = useState<'save' | 'saveInvite'>(
|
|
|
'save'
|
|
|
)
|
|
|
@@ -457,16 +459,56 @@ export function Tenants() {
|
|
|
}
|
|
|
|
|
|
const handleSubmit = async (data: TenantFormData) => {
|
|
|
- if (currentTenant) {
|
|
|
- const uniqueId = currentTenant.code || String(currentTenant.id)
|
|
|
+ if (isSubmitting) return
|
|
|
+ setIsSubmitting(true)
|
|
|
+
|
|
|
+ try {
|
|
|
+ if (currentTenant) {
|
|
|
+ const uniqueId = currentTenant.code || String(currentTenant.id)
|
|
|
+
|
|
|
+ try {
|
|
|
+ const result = await updateDirectChildTenant(uniqueId, {
|
|
|
+ tenant_name: data.name,
|
|
|
+ contact_person: data.contact_name || undefined,
|
|
|
+ contact_phone: data.contact_phone || undefined,
|
|
|
+ contact_email: data.contact_email || undefined,
|
|
|
+ group_name: data.group,
|
|
|
+ quota_limit: data.quota_limit === '' ? undefined : data.quota_limit,
|
|
|
+ start_time: toApiDateTime(data.starts_at, '00:00:00'),
|
|
|
+ end_time: toApiDateTime(data.ends_at, '23:59:59'),
|
|
|
+ module_codes: data.permission_keys,
|
|
|
+ })
|
|
|
+
|
|
|
+ if (!result.success) {
|
|
|
+ toast.error(result.message || t('Failed to update tenant'))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ toast.success(t('Tenant updated successfully'))
|
|
|
+ const savedTenant = result.data ?? currentTenant
|
|
|
+ setEditOpen(false)
|
|
|
+ await Promise.all([
|
|
|
+ queryClient.invalidateQueries({
|
|
|
+ queryKey: ['tenants', 'direct-children'],
|
|
|
+ }),
|
|
|
+ queryClient.invalidateQueries({
|
|
|
+ queryKey: ['tenants', 'direct-children', uniqueId],
|
|
|
+ }),
|
|
|
+ ])
|
|
|
+ if (submitAction === 'saveInvite') openInvitationDialog(savedTenant)
|
|
|
+ } catch {
|
|
|
+ toast.error(t('Failed to update tenant'))
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
try {
|
|
|
- const result = await updateDirectChildTenant(uniqueId, {
|
|
|
+ const result = await createDirectChildTenant({
|
|
|
tenant_name: data.name,
|
|
|
+ group_name: data.group,
|
|
|
contact_person: data.contact_name || undefined,
|
|
|
contact_phone: data.contact_phone || undefined,
|
|
|
contact_email: data.contact_email || undefined,
|
|
|
- group_name: data.group,
|
|
|
quota_limit: data.quota_limit === '' ? undefined : data.quota_limit,
|
|
|
start_time: toApiDateTime(data.starts_at, '00:00:00'),
|
|
|
end_time: toApiDateTime(data.ends_at, '23:59:59'),
|
|
|
@@ -474,56 +516,24 @@ export function Tenants() {
|
|
|
})
|
|
|
|
|
|
if (!result.success) {
|
|
|
- toast.error(result.message || t('Failed to update tenant'))
|
|
|
+ toast.error(result.message || t('Failed to create tenant'))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- toast.success(t('Tenant updated successfully'))
|
|
|
- const savedTenant = result.data ?? currentTenant
|
|
|
- setEditOpen(false)
|
|
|
- await Promise.all([
|
|
|
- queryClient.invalidateQueries({
|
|
|
- queryKey: ['tenants', 'direct-children'],
|
|
|
- }),
|
|
|
- queryClient.invalidateQueries({
|
|
|
- queryKey: ['tenants', 'direct-children', uniqueId],
|
|
|
- }),
|
|
|
- ])
|
|
|
- if (submitAction === 'saveInvite') openInvitationDialog(savedTenant)
|
|
|
+ toast.success(t('Tenant created successfully'))
|
|
|
+ const savedTenant = result.data
|
|
|
+ setCreateOpen(false)
|
|
|
+ await queryClient.invalidateQueries({
|
|
|
+ queryKey: ['tenants', 'direct-children'],
|
|
|
+ })
|
|
|
+ if (submitAction === 'saveInvite' && savedTenant) {
|
|
|
+ openInvitationDialog(savedTenant)
|
|
|
+ }
|
|
|
} catch {
|
|
|
- toast.error(t('Failed to update tenant'))
|
|
|
+ toast.error(t('Failed to create tenant'))
|
|
|
}
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- const result = await createDirectChildTenant({
|
|
|
- tenant_name: data.name,
|
|
|
- group_name: data.group,
|
|
|
- contact_person: data.contact_name || undefined,
|
|
|
- contact_phone: data.contact_phone || undefined,
|
|
|
- contact_email: data.contact_email || undefined,
|
|
|
- quota_limit: data.quota_limit === '' ? undefined : data.quota_limit,
|
|
|
- start_time: toApiDateTime(data.starts_at, '00:00:00'),
|
|
|
- end_time: toApiDateTime(data.ends_at, '23:59:59'),
|
|
|
- module_codes: data.permission_keys,
|
|
|
- })
|
|
|
-
|
|
|
- if (!result.success) {
|
|
|
- toast.error(result.message || t('Failed to create tenant'))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- toast.success(t('Tenant created successfully'))
|
|
|
- const savedTenant = result.data
|
|
|
- setCreateOpen(false)
|
|
|
- await queryClient.invalidateQueries({
|
|
|
- queryKey: ['tenants', 'direct-children'],
|
|
|
- })
|
|
|
- if (submitAction === 'saveInvite' && savedTenant)
|
|
|
- openInvitationDialog(savedTenant)
|
|
|
- } catch {
|
|
|
- toast.error(t('Failed to create tenant'))
|
|
|
+ } finally {
|
|
|
+ setIsSubmitting(false)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -1155,9 +1165,13 @@ export function Tenants() {
|
|
|
<Button
|
|
|
form='tenant-edit-form'
|
|
|
type='submit'
|
|
|
+ disabled={isSubmitting}
|
|
|
onClick={() => setSubmitAction('save')}
|
|
|
>
|
|
|
- {t('Save changes')}
|
|
|
+ {isSubmitting && (
|
|
|
+ <Loader2 className='mr-2 h-4 w-4 animate-spin' />
|
|
|
+ )}
|
|
|
+ {isSubmitting ? t('Saving...') : t('Save changes')}
|
|
|
</Button>
|
|
|
</SheetFooter>
|
|
|
</SheetContent>
|
|
|
@@ -1290,16 +1304,28 @@ export function Tenants() {
|
|
|
<Button
|
|
|
form='tenant-create-form'
|
|
|
type='submit'
|
|
|
+ disabled={isSubmitting}
|
|
|
onClick={() => setSubmitAction('save')}
|
|
|
>
|
|
|
- {t('Create')}
|
|
|
+ {isSubmitting && submitAction === 'save' && (
|
|
|
+ <Loader2 className='mr-2 h-4 w-4 animate-spin' />
|
|
|
+ )}
|
|
|
+ {isSubmitting && submitAction === 'save'
|
|
|
+ ? t('Creating...')
|
|
|
+ : t('Create')}
|
|
|
</Button>
|
|
|
<Button
|
|
|
form='tenant-create-form'
|
|
|
type='submit'
|
|
|
+ disabled={isSubmitting}
|
|
|
onClick={() => setSubmitAction('saveInvite')}
|
|
|
>
|
|
|
- {t('Save and invite administrator')}
|
|
|
+ {isSubmitting && submitAction === 'saveInvite' && (
|
|
|
+ <Loader2 className='mr-2 h-4 w-4 animate-spin' />
|
|
|
+ )}
|
|
|
+ {isSubmitting && submitAction === 'saveInvite'
|
|
|
+ ? t('Saving...')
|
|
|
+ : t('Save and invite administrator')}
|
|
|
</Button>
|
|
|
</SheetFooter>
|
|
|
</SheetContent>
|