-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
|
@@ -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'; | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(() => { | ||
|
@@ -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`}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't it be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? ( | ||
|
There was a problem hiding this comment.
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.