fix(admin): reset user modal state consistently
This commit is contained in:
@@ -16,6 +16,17 @@ export function createUsersStore({ api, onToast, at, routePrefix = "" }) {
|
|||||||
usersSort: "",
|
usersSort: "",
|
||||||
usersLoading: false,
|
usersLoading: false,
|
||||||
|
|
||||||
|
..._closedUserModalState(),
|
||||||
|
userReferralsPageSize: USERS_PAGE_SIZE,
|
||||||
|
userLogsPageSize: USER_LOGS_PAGE_SIZE,
|
||||||
|
});
|
||||||
|
|
||||||
|
let _activeRef = "stats"; // fallback if active isn't tracked
|
||||||
|
let _pathContext = null;
|
||||||
|
let _openUserRequestId = 0;
|
||||||
|
|
||||||
|
function _closedUserModalState() {
|
||||||
|
return {
|
||||||
openedUser: null,
|
openedUser: null,
|
||||||
openedUserDetail: null,
|
openedUserDetail: null,
|
||||||
userDetailLoading: false,
|
userDetailLoading: false,
|
||||||
@@ -31,7 +42,6 @@ export function createUsersStore({ api, onToast, at, routePrefix = "" }) {
|
|||||||
userReferrals: [],
|
userReferrals: [],
|
||||||
userReferralsTotal: 0,
|
userReferralsTotal: 0,
|
||||||
userReferralsPage: 0,
|
userReferralsPage: 0,
|
||||||
userReferralsPageSize: USERS_PAGE_SIZE,
|
|
||||||
userReferralsInviter: null,
|
userReferralsInviter: null,
|
||||||
userDetailTab: "profile",
|
userDetailTab: "profile",
|
||||||
premiumUnlimitedDraft: false,
|
premiumUnlimitedDraft: false,
|
||||||
@@ -42,19 +52,32 @@ export function createUsersStore({ api, onToast, at, routePrefix = "" }) {
|
|||||||
hwidDeviceLimitDraft: "",
|
hwidDeviceLimitDraft: "",
|
||||||
grantTrafficGbDraft: "",
|
grantTrafficGbDraft: "",
|
||||||
grantTrafficKindDraft: "regular",
|
grantTrafficKindDraft: "regular",
|
||||||
|
|
||||||
userLogs: [],
|
userLogs: [],
|
||||||
userLogsTotal: 0,
|
userLogsTotal: 0,
|
||||||
userLogsPage: 0,
|
userLogsPage: 0,
|
||||||
userLogsLoading: false,
|
userLogsLoading: false,
|
||||||
userLogsLoaded: false,
|
userLogsLoaded: false,
|
||||||
userLogsUserId: null,
|
userLogsUserId: null,
|
||||||
userLogsPageSize: USER_LOGS_PAGE_SIZE,
|
};
|
||||||
});
|
}
|
||||||
|
|
||||||
let _activeRef = "stats"; // fallback if active isn't tracked
|
function _openingUserModalState(user, userId) {
|
||||||
let _pathContext = null;
|
return {
|
||||||
let _openUserRequestId = 0;
|
..._closedUserModalState(),
|
||||||
|
openedUser: user,
|
||||||
|
userDetailLoading: true,
|
||||||
|
userDetailTab: "subscription",
|
||||||
|
userLogsUserId: userId,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function _isCurrentUserRequest(s, requestId, userId) {
|
||||||
|
return (
|
||||||
|
requestId === _openUserRequestId &&
|
||||||
|
Boolean(s.openedUser) &&
|
||||||
|
s.openedUser.user_id === userId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function setActive(active) {
|
function setActive(active) {
|
||||||
_activeRef = active;
|
_activeRef = active;
|
||||||
@@ -127,30 +150,13 @@ export function createUsersStore({ api, onToast, at, routePrefix = "" }) {
|
|||||||
if (!userId) return;
|
if (!userId) return;
|
||||||
const requestId = ++_openUserRequestId;
|
const requestId = ++_openUserRequestId;
|
||||||
_setPathContext(opts.pathContext);
|
_setPathContext(opts.pathContext);
|
||||||
|
const openedUser =
|
||||||
|
typeof userOrId === "object" && userOrId !== null ? userOrId : { user_id: userId };
|
||||||
|
|
||||||
state.update((s) => ({
|
state.update((s) => ({
|
||||||
...s,
|
...s,
|
||||||
openedUser:
|
..._openingUserModalState(openedUser, userId),
|
||||||
typeof userOrId === "object" && userOrId !== null ? userOrId : { user_id: userId },
|
userActionBusy: s.userActionBusy,
|
||||||
openedUserDetail: null,
|
|
||||||
userMessageDraft: "",
|
|
||||||
userMessageConfirmOpen: false,
|
|
||||||
userExtendDays: 30,
|
|
||||||
userExtendHwidDevices: true,
|
|
||||||
userDetailLoading: true,
|
|
||||||
userDetailTab: "subscription",
|
|
||||||
userReferralsOpen: false,
|
|
||||||
userReferralsLoading: false,
|
|
||||||
userReferrals: [],
|
|
||||||
userReferralsTotal: 0,
|
|
||||||
userReferralsPage: 0,
|
|
||||||
userReferralsInviter: null,
|
|
||||||
userLogs: [],
|
|
||||||
userLogsTotal: 0,
|
|
||||||
userLogsPage: 0,
|
|
||||||
userLogsLoading: false,
|
|
||||||
userLogsLoaded: false,
|
|
||||||
userLogsUserId: userId,
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
if (!opts.skipPush) _pushUserPath(userId);
|
if (!opts.skipPush) _pushUserPath(userId);
|
||||||
@@ -164,13 +170,7 @@ export function createUsersStore({ api, onToast, at, routePrefix = "" }) {
|
|||||||
sub?.hwid_device_limit !== null && sub?.hwid_device_limit !== undefined;
|
sub?.hwid_device_limit !== null && sub?.hwid_device_limit !== undefined;
|
||||||
const hwidLimit = hasHwidLimit ? Number(sub?.hwid_device_limit) : null;
|
const hwidLimit = hasHwidLimit ? Number(sub?.hwid_device_limit) : null;
|
||||||
state.update((s) => {
|
state.update((s) => {
|
||||||
if (
|
if (!_isCurrentUserRequest(s, requestId, userId)) return s;
|
||||||
requestId !== _openUserRequestId ||
|
|
||||||
!s.openedUser ||
|
|
||||||
s.openedUser.user_id !== userId
|
|
||||||
) {
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
...s,
|
...s,
|
||||||
openedUserDetail: res,
|
openedUserDetail: res,
|
||||||
@@ -190,30 +190,18 @@ export function createUsersStore({ api, onToast, at, routePrefix = "" }) {
|
|||||||
let shouldClearPath = false;
|
let shouldClearPath = false;
|
||||||
let shouldShowError = false;
|
let shouldShowError = false;
|
||||||
state.update((s) => {
|
state.update((s) => {
|
||||||
if (
|
if (!_isCurrentUserRequest(s, requestId, userId)) return s;
|
||||||
requestId !== _openUserRequestId ||
|
|
||||||
!s.openedUser ||
|
|
||||||
s.openedUser.user_id !== userId
|
|
||||||
) {
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
shouldShowError = true;
|
shouldShowError = true;
|
||||||
shouldClearPath = true;
|
shouldClearPath = true;
|
||||||
_pathContext = null;
|
_pathContext = null;
|
||||||
return { ...s, openedUser: null };
|
return { ...s, ..._closedUserModalState() };
|
||||||
});
|
});
|
||||||
if (shouldShowError) onToast(res?.error || "load_failed");
|
if (shouldShowError) onToast(res?.error || "load_failed");
|
||||||
if (shouldClearPath && !opts.skipPush) _pushUserPath(null);
|
if (shouldClearPath && !opts.skipPush) _pushUserPath(null);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
state.update((s) => {
|
state.update((s) => {
|
||||||
if (
|
if (!_isCurrentUserRequest(s, requestId, userId)) return s;
|
||||||
requestId !== _openUserRequestId ||
|
|
||||||
!s.openedUser ||
|
|
||||||
s.openedUser.user_id !== userId
|
|
||||||
) {
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
return { ...s, userDetailLoading: false };
|
return { ...s, userDetailLoading: false };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -226,24 +214,7 @@ export function createUsersStore({ api, onToast, at, routePrefix = "" }) {
|
|||||||
wasOpen = Boolean(s.openedUser);
|
wasOpen = Boolean(s.openedUser);
|
||||||
return {
|
return {
|
||||||
...s,
|
...s,
|
||||||
openedUser: null,
|
..._closedUserModalState(),
|
||||||
openedUserDetail: null,
|
|
||||||
userDetailLoading: false,
|
|
||||||
userDeleteOpen: false,
|
|
||||||
userBanConfirmOpen: false,
|
|
||||||
userMessageConfirmOpen: false,
|
|
||||||
userReferralsOpen: false,
|
|
||||||
userReferralsLoading: false,
|
|
||||||
userReferrals: [],
|
|
||||||
userReferralsTotal: 0,
|
|
||||||
userReferralsPage: 0,
|
|
||||||
userReferralsInviter: null,
|
|
||||||
userLogs: [],
|
|
||||||
userLogsTotal: 0,
|
|
||||||
userLogsPage: 0,
|
|
||||||
userLogsLoading: false,
|
|
||||||
userLogsLoaded: false,
|
|
||||||
userLogsUserId: null,
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
if (wasOpen && !opts.skipPush) _pushUserPath(null);
|
if (wasOpen && !opts.skipPush) _pushUserPath(null);
|
||||||
|
|||||||
Reference in New Issue
Block a user