useSubscriptionsData.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import { useState, useEffect } from 'react';
  16. import { useTranslation } from 'react-i18next';
  17. import { API, showError, showSuccess } from '../../helpers';
  18. import { useTableCompactMode } from '../common/useTableCompactMode';
  19. export const useSubscriptionsData = () => {
  20. const { t } = useTranslation();
  21. const [compactMode, setCompactMode] = useTableCompactMode('subscriptions');
  22. // State management
  23. const [allPlans, setAllPlans] = useState([]);
  24. const [loading, setLoading] = useState(true);
  25. // Pagination (client-side for now)
  26. const [activePage, setActivePage] = useState(1);
  27. const [pageSize, setPageSize] = useState(10);
  28. // Drawer states
  29. const [showEdit, setShowEdit] = useState(false);
  30. const [editingPlan, setEditingPlan] = useState(null);
  31. const [sheetPlacement, setSheetPlacement] = useState('left'); // 'left' | 'right'
  32. // Load subscription plans
  33. const loadPlans = async () => {
  34. setLoading(true);
  35. try {
  36. const res = await API.get('/api/subscription/admin/plans');
  37. if (res.data?.success) {
  38. const next = res.data.data || [];
  39. setAllPlans(next);
  40. // Keep page in range after data changes
  41. const totalPages = Math.max(1, Math.ceil(next.length / pageSize));
  42. setActivePage((p) => Math.min(p || 1, totalPages));
  43. } else {
  44. showError(res.data?.message || t('加载失败'));
  45. }
  46. } catch (e) {
  47. showError(t('请求失败'));
  48. } finally {
  49. setLoading(false);
  50. }
  51. };
  52. // Refresh data
  53. const refresh = async () => {
  54. await loadPlans();
  55. };
  56. const handlePageChange = (page) => {
  57. setActivePage(page);
  58. };
  59. const handlePageSizeChange = (size) => {
  60. setPageSize(size);
  61. setActivePage(1);
  62. };
  63. // Update plan enabled status (single endpoint)
  64. const setPlanEnabled = async (planRecordOrId, enabled) => {
  65. const planId =
  66. typeof planRecordOrId === 'number'
  67. ? planRecordOrId
  68. : planRecordOrId?.plan?.id;
  69. if (!planId) return;
  70. setLoading(true);
  71. try {
  72. const res = await API.patch(`/api/subscription/admin/plans/${planId}`, {
  73. enabled: !!enabled,
  74. });
  75. if (res.data?.success) {
  76. showSuccess(enabled ? t('已启用') : t('已禁用'));
  77. await loadPlans();
  78. } else {
  79. showError(res.data?.message || t('操作失败'));
  80. }
  81. } catch (e) {
  82. showError(t('请求失败'));
  83. } finally {
  84. setLoading(false);
  85. }
  86. };
  87. // Modal control functions
  88. const closeEdit = () => {
  89. setShowEdit(false);
  90. setEditingPlan(null);
  91. };
  92. const openCreate = () => {
  93. setSheetPlacement('left');
  94. setEditingPlan(null);
  95. setShowEdit(true);
  96. };
  97. const openEdit = (planRecord) => {
  98. setSheetPlacement('right');
  99. setEditingPlan(planRecord);
  100. setShowEdit(true);
  101. };
  102. // Initialize data on component mount
  103. useEffect(() => {
  104. loadPlans();
  105. }, []);
  106. const planCount = allPlans.length;
  107. const plans = allPlans.slice(
  108. Math.max(0, (activePage - 1) * pageSize),
  109. Math.max(0, (activePage - 1) * pageSize) + pageSize,
  110. );
  111. return {
  112. // Data state
  113. plans,
  114. planCount,
  115. loading,
  116. // Modal state
  117. showEdit,
  118. editingPlan,
  119. sheetPlacement,
  120. setShowEdit,
  121. setEditingPlan,
  122. // UI state
  123. compactMode,
  124. setCompactMode,
  125. // Pagination
  126. activePage,
  127. pageSize,
  128. handlePageChange,
  129. handlePageSizeChange,
  130. // Actions
  131. loadPlans,
  132. setPlanEnabled,
  133. refresh,
  134. closeEdit,
  135. openCreate,
  136. openEdit,
  137. // Translation
  138. t,
  139. };
  140. };