Skip to content

Commit

Permalink
♻️ refactor(api): Sort and Announcement
Browse files Browse the repository at this point in the history
  • Loading branch information
web-ppanel committed Dec 23, 2024
1 parent 93a0a88 commit 38d5616
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 35 deletions.
33 changes: 17 additions & 16 deletions apps/admin/app/dashboard/server/node-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export default function NodeTable() {
await createNode({
...params,
enable: false,
});
} as API.CreateNodeRequest);
toast.success(t('copySuccess'));
ref.current?.refresh();
setLoading(false);
Expand Down Expand Up @@ -258,26 +258,27 @@ export default function NodeTable() {
const sourceIndex = items.findIndex((item) => String(item.id) === source);
const targetIndex = items.findIndex((item) => String(item.id) === target);

const originalSorts = items.map((item) => ({ id: item.id, sort: item.sort || item.id }));
const originalSortMap = new Map(items.map((item) => [item.id, item.sort || item.id]));

const [movedItem] = items.splice(sourceIndex, 1);
items.splice(targetIndex, 0, movedItem!);

const updatedItems = items.map((item) => {
const originalSort = originalSorts.find((sortItem) => sortItem.id === item.id)?.sort;
return {
...item,
sort: originalSort !== undefined ? originalSort : item.sort,
};
});
nodeSort({
sort: updatedItems.map((item) => {
return {
id: item.id,
sort: item.sort,
};
}),
const updatedItems = items.map((item, index) => {
const originalSort = originalSortMap.get(item.id);
const newSort = originalSort !== undefined ? originalSort : item.sort;
return { ...item, sort: newSort };
});

const changedItems = updatedItems.filter(
(item) => originalSortMap.get(item.id) !== item.sort,
);

if (changedItems.length > 0) {
nodeSort({
sort: changedItems.map((item) => ({ id: item.id, sort: item.sort })),
});
}

return updatedItems;
}}
/>
Expand Down
31 changes: 16 additions & 15 deletions apps/admin/app/dashboard/subscribe/subscribe-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,26 +266,27 @@ export default function SubscribeTable() {
const sourceIndex = items.findIndex((item) => String(item.id) === source);
const targetIndex = items.findIndex((item) => String(item.id) === target);

const originalSorts = items.map((item) => ({ id: item.id, sort: item.sort || item.id }));
const originalSortMap = new Map(items.map((item) => [item.id, item.sort || item.id]));

const [movedItem] = items.splice(sourceIndex, 1);
items.splice(targetIndex, 0, movedItem!);

const updatedItems = items.map((item) => {
const originalSort = originalSorts.find((sortItem) => sortItem.id === item.id)?.sort;
return {
...item,
sort: originalSort !== undefined ? originalSort : item.sort,
};
});
subscribeSort({
sort: updatedItems.map((item) => {
return {
id: item.id,
sort: item.sort,
};
}),
const updatedItems = items.map((item, index) => {
const originalSort = originalSortMap.get(item.id);
const newSort = originalSort !== undefined ? originalSort : item.sort;
return { ...item, sort: newSort };
});

const changedItems = updatedItems.filter(
(item) => originalSortMap.get(item.id) !== item.sort,
);

if (changedItems.length > 0) {
subscribeSort({
sort: changedItems.map((item) => ({ id: item.id, sort: item.sort })),
});
}

return updatedItems;
}}
/>
Expand Down
10 changes: 8 additions & 2 deletions apps/admin/services/admin/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ declare namespace API {
protocol: string;
config: Record<string, any>;
enable: boolean;
sort: number;
};

type CreateOrderRequest = {
Expand Down Expand Up @@ -276,14 +277,18 @@ declare namespace API {
type GetAnnouncementListParams = {
page: number;
size: number;
enable?: boolean;
show?: boolean;
pinned?: boolean;
popup?: boolean;
search?: string;
};

type GetAnnouncementListRequest = {
page: number;
size: number;
enable?: boolean;
show?: boolean;
pinned?: boolean;
popup?: boolean;
search?: string;
};

Expand Down Expand Up @@ -891,6 +896,7 @@ declare namespace API {
protocol: string;
config: Record<string, any>;
enable: boolean;
sort: number;
};

type UpdateOrderStatusRequest = {
Expand Down
4 changes: 3 additions & 1 deletion apps/user/app/(main)/(user)/announcement/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export default function Page() {
queryFn: async () => {
const { data } = await queryAnnouncement({
page: 1,
size: 20,
size: 99,
pinned: false,
popup: false,
});
return data.data?.announcements || [];
},
Expand Down
4 changes: 3 additions & 1 deletion apps/user/components/announcement/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export default async function Announcement({
data = await queryAnnouncement(
{
page: 1,
size: 50,
size: 10,
pinned: type === 'pinned',
popup: type === 'popup',
},
{
skipErrorHandler: true,
Expand Down
4 changes: 4 additions & 0 deletions apps/user/services/user/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,15 @@ declare namespace API {
type QueryAnnouncementParams = {
page: number;
size: number;
pinned: boolean;
popup: boolean;
};

type QueryAnnouncementRequest = {
page: number;
size: number;
pinned: boolean;
popup: boolean;
};

type QueryAnnouncementResponse = {
Expand Down

0 comments on commit 38d5616

Please sign in to comment.