Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server Sorting: Sort servers from A-Z or Z-A. #4951

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 36 additions & 7 deletions resources/scripts/components/dashboard/DashboardContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import React, { useEffect, useState } from 'react';
import { Server } from '@/api/server/getServer';
import getServers from '@/api/getServers';
import ServerRow from '@/components/dashboard/ServerRow';
Expand All @@ -13,20 +13,43 @@ import useSWR from 'swr';
import { PaginatedResult } from '@/api/http';
import Pagination from '@/components/elements/Pagination';
import { useLocation } from 'react-router-dom';
import Select from '../elements/Select';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be using proper paths, e.g.

import Select from '@/components/elements/Select';


export default () => {
const { search } = useLocation();
const defaultPage = Number(new URLSearchParams(search).get('page') || '1');

const [page, setPage] = useState(!isNaN(defaultPage) && defaultPage > 0 ? defaultPage : 1);
const { clearFlashes, clearAndAddHttpError } = useFlash();
const uuid = useStoreState(state => state.user.data!.uuid);
const rootAdmin = useStoreState(state => state.user.data!.rootAdmin);
const uuid = useStoreState((state) => state.user.data!.uuid);
const rootAdmin = useStoreState((state) => state.user.data!.rootAdmin);
Comment on lines -23 to +25

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be reverted I believe, just use prettier

const [showOnlyAdmin, setShowOnlyAdmin] = usePersistedState(`${uuid}:show_all_servers`, false);

const [sort, setSort] = useState(1);
const sortServers = (serverList: Server[]) => {
return serverList.sort((a, b) => {
const nameA = a.name.charAt(0).toUpperCase();
const nameB = b.name.charAt(0).toUpperCase();
if (sort === 1) {
if (nameA < nameB) return -1;
if (nameA > nameB) return 1;
return 0;
} else {
if (nameA > nameB) return -1;
if (nameA < nameB) return 1;
return 0;
}
});
};

const { data: servers, error } = useSWR<PaginatedResult<Server>>(
['/api/client/servers', showOnlyAdmin && rootAdmin, page],
() => getServers({ page, type: showOnlyAdmin && rootAdmin ? 'admin' : undefined }),
['/api/client/servers', showOnlyAdmin && rootAdmin, page, sort],
() =>
getServers({ page, type: showOnlyAdmin && rootAdmin ? 'admin' : undefined }).then((response) => {
if ('items' in response) {
return { ...response, items: sortServers(response.items) };
}
return response;
})
);

useEffect(() => {
Expand Down Expand Up @@ -58,8 +81,14 @@ export default () => {
<Switch
name={'show_all_servers'}
defaultChecked={showOnlyAdmin}
onChange={() => setShowOnlyAdmin(s => !s)}
onChange={() => setShowOnlyAdmin((s) => !s)}
/>
<div css={tw`mx-3`}>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be ml-3? How does it currently look?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a screenshot. Because the switch doesn't have a margin right (without your PR) and you added it to your element

<Select value={sort} onChange={(e) => setSort(Number(e.target.value))}>
<option value='1'>A-Z</option>
<option value='2'>Z-A</option>
</Select>
</div>
</div>
)}
{!servers ? (
Expand Down