Skip to content

Commit f45c03a

Browse files
committed
Support filtering to own/all servers if user is an admin
1 parent 67c6be9 commit f45c03a

File tree

4 files changed

+86
-19
lines changed

4 files changed

+86
-19
lines changed

resources/scripts/api/getServers.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import { rawDataToServerObject, Server } from '@/api/server/getServer';
22
import http, { getPaginationSet, PaginatedResult } from '@/api/http';
33

4-
export default (query?: string): Promise<PaginatedResult<Server>> => {
4+
export default (query?: string, includeAdmin?: boolean): Promise<PaginatedResult<Server>> => {
55
return new Promise((resolve, reject) => {
6-
http.get(`/api/client`, { params: { include: [ 'allocation' ], query } })
6+
http.get(`/api/client`, {
7+
params: {
8+
include: [ 'allocation' ],
9+
// eslint-disable-next-line @typescript-eslint/camelcase
10+
filter: includeAdmin ? 'all' : undefined,
11+
query,
12+
},
13+
})
714
.then(({ data }) => resolve({
815
items: (data.data || []).map((datum: any) => rawDataToServerObject(datum.attributes)),
916
pagination: getPaginationSet(data.meta.pagination),

resources/scripts/components/dashboard/DashboardContainer.tsx

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,63 @@ import getServers from '@/api/getServers';
44
import ServerRow from '@/components/dashboard/ServerRow';
55
import Spinner from '@/components/elements/Spinner';
66
import PageContentBlock from '@/components/elements/PageContentBlock';
7+
import useFlash from '@/plugins/useFlash';
8+
import { httpErrorToHuman } from '@/api/http';
9+
import FlashMessageRender from '@/components/FlashMessageRender';
10+
import { useStoreState } from 'easy-peasy';
11+
import { usePersistedState } from '@/plugins/usePersistedState';
12+
import Switch from '@/components/elements/Switch';
713

814
export default () => {
9-
const [ servers, setServers ] = useState<null | Server[]>(null);
15+
const { addError, clearFlashes } = useFlash();
16+
const [ servers, setServers ] = useState<Server[]>([]);
17+
const [ loading, setLoading ] = useState(true);
18+
const { rootAdmin } = useStoreState(state => state.user.data!);
19+
const [ showAdmin, setShowAdmin ] = usePersistedState('show_all_servers', false);
1020

11-
const loadServers = () => getServers().then(data => setServers(data.items));
21+
const loadServers = () => {
22+
clearFlashes();
23+
setLoading(true);
24+
25+
getServers(undefined, showAdmin)
26+
.then(data => setServers(data.items))
27+
.catch(error => {
28+
console.error(error);
29+
addError({ message: httpErrorToHuman(error) });
30+
})
31+
.then(() => setLoading(false));
32+
};
1233

1334
useEffect(() => {
1435
loadServers();
15-
}, []);
16-
17-
if (servers === null) {
18-
return <Spinner size={'large'} centered={true}/>;
19-
}
36+
}, [ showAdmin ]);
2037

2138
return (
2239
<PageContentBlock>
23-
{servers.length > 0 ?
24-
servers.map(server => (
25-
<ServerRow key={server.uuid} server={server} className={'mt-2'}/>
26-
))
27-
:
28-
<p className={'text-center text-sm text-neutral-400'}>
29-
It looks like you have no servers.
40+
<FlashMessageRender className={'mb-4'}/>
41+
{rootAdmin &&
42+
<div className={'mb-2 flex justify-end items-center'}>
43+
<p className={'uppercase text-xs text-neutral-400 mr-2'}>
44+
{showAdmin ? 'Showing all servers' : 'Showing your servers'}
3045
</p>
46+
<Switch
47+
name={'show_all_servers'}
48+
defaultChecked={showAdmin}
49+
onChange={() => setShowAdmin(s => !s)}
50+
/>
51+
</div>
52+
}
53+
{loading ?
54+
<Spinner centered={true} size={'large'}/>
55+
:
56+
servers.length > 0 ?
57+
servers.map(server => (
58+
<ServerRow key={server.uuid} server={server} className={'mt-2'}/>
59+
))
60+
:
61+
<p className={'text-center text-sm text-neutral-400'}>
62+
There are no servers associated with your account.
63+
</p>
3164
}
3265
</PageContentBlock>
3366
);

resources/scripts/components/elements/Switch.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const ToggleContainer = styled.div`
3636

3737
export interface SwitchProps {
3838
name: string;
39-
label: string;
39+
label?: string;
4040
description?: string;
4141
defaultChecked?: boolean;
4242
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
@@ -48,7 +48,7 @@ const Switch = ({ name, label, description, defaultChecked, onChange, children }
4848

4949
return (
5050
<div className={'flex items-center'}>
51-
<ToggleContainer className={'mr-4 flex-none'}>
51+
<ToggleContainer className={'flex-none'}>
5252
{children
5353
|| <input
5454
id={uuid}
@@ -60,17 +60,21 @@ const Switch = ({ name, label, description, defaultChecked, onChange, children }
6060
}
6161
<label htmlFor={uuid}/>
6262
</ToggleContainer>
63-
<div className={'w-full'}>
63+
{(label || description) &&
64+
<div className={'ml-4 w-full'}>
65+
{label &&
6466
<label
6567
className={classNames('input-dark-label cursor-pointer', { 'mb-0': !!description })}
6668
htmlFor={uuid}
6769
>{label}</label>
70+
}
6871
{description &&
6972
<p className={'input-help'}>
7073
{description}
7174
</p>
7275
}
7376
</div>
77+
}
7478
</div>
7579
);
7680
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Dispatch, SetStateAction, useEffect, useState } from 'react';
2+
3+
export function usePersistedState<S = undefined> (key: string, defaultValue: S): [S | undefined, Dispatch<SetStateAction<S | undefined>>] {
4+
const [state, setState] = useState(
5+
() => {
6+
try {
7+
const item = localStorage.getItem(key);
8+
9+
return JSON.parse(item || (String(defaultValue)));
10+
} catch (e) {
11+
console.warn('Failed to retrieve persisted value from store.', e);
12+
13+
return defaultValue;
14+
}
15+
}
16+
);
17+
18+
useEffect(() => {
19+
localStorage.setItem(key, JSON.stringify(state))
20+
}, [key, state]);
21+
22+
return [ state, setState ];
23+
}

0 commit comments

Comments
 (0)