Skip to content

Commit

Permalink
fix: guest permissions (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam-Zhao authored Oct 20, 2023
1 parent 5315218 commit c934135
Show file tree
Hide file tree
Showing 6 changed files with 489 additions and 561 deletions.
30 changes: 7 additions & 23 deletions src/components/developer/tokens/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { formatDate, getPaginatedList } from '../../../lib/utils';
import { useCopyToClipboard } from 'react-use';
import { LoadingButton } from '@mui/lab';
import { Link, useNavigate } from 'react-router-dom';
import { MyContext } from '../../menu/index';
import AddIcon from '@mui/icons-material/Add';
import CancelIcon from '@mui/icons-material/Cancel';
import DeleteIcon from '@mui/icons-material/Delete';
Expand All @@ -47,7 +46,6 @@ export default function PersonalAccessTokens() {
const [token, setToken] = useState<getTokensResponse[]>([]);
const [allTokens, setAllTokens] = useState<getTokensResponse[]>([]);
const navigate = useNavigate();
const user = useContext(MyContext);

const theme = createTheme({
palette: {
Expand All @@ -72,17 +70,10 @@ export default function PersonalAccessTokens() {

(async function () {
try {
if (user.name === 'root') {
const token = await getTokens({ page: 1, per_page: MAX_PAGE_SIZE });
const token = await getTokens({ page: 1, per_page: MAX_PAGE_SIZE });

setToken(token);
setIsLoading(false);
} else if (user.name !== '') {
const token = await getTokens({ user_id: String(user.id), page: 1, per_page: MAX_PAGE_SIZE });

setToken(token);
setIsLoading(false);
}
setToken(token);
setIsLoading(false);
} catch (error) {
if (error instanceof Error) {
setErrorMessage(true);
Expand All @@ -91,7 +82,7 @@ export default function PersonalAccessTokens() {
}
}
})();
}, [user]);
}, []);

useEffect(() => {
token.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());
Expand Down Expand Up @@ -121,17 +112,10 @@ export default function PersonalAccessTokens() {
setSuccessMessage(true);
setOpenDeletToken(false);

if (user.name === 'root') {
const token = await getTokens({ page: 1, per_page: DEFAULT_PAGE_SIZE });
const token = await getTokens({ page: 1, per_page: DEFAULT_PAGE_SIZE });

setToken(token);
setIsLoading(false);
} else if (user.name !== '') {
const token = await getTokens({ user_id: String(user.id), page: 1, per_page: DEFAULT_PAGE_SIZE });

setToken(token);
setIsLoading(false);
}
setToken(token);
setIsLoading(false);
} catch (error) {
if (error instanceof Error) {
setDeleteLoadingButton(false);
Expand Down
5 changes: 1 addition & 4 deletions src/components/developer/tokens/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ import {
FormGroup,
InputLabel,
} from '@mui/material';
import { useContext, useEffect, useState } from 'react';
import { useEffect, useState } from 'react';
import { formatDate, getExpiredTime } from '../../../lib/utils';
import { LoadingButton } from '@mui/lab';
import { useNavigate } from 'react-router-dom';
import { createTokens } from '../../../lib/api';
import HelpIcon from '@mui/icons-material/Help';
import { MyContext } from '../../menu/index';
import CancelIcon from '@mui/icons-material/Cancel';
import CheckCircleIcon from '@mui/icons-material/CheckCircle';

Expand Down Expand Up @@ -116,7 +115,6 @@ export default function CreateTokens() {
},
];

const user = useContext(MyContext);
const navigate = useNavigate();

useEffect(() => {
Expand Down Expand Up @@ -158,7 +156,6 @@ export default function CreateTokens() {
bio: bio.value,
scopes: filteredScopes,
expired_at: expiredTime,
user_id: user.id,
};

const canSubmit = Boolean(!formList.filter((item) => item.syncError).length);
Expand Down
103 changes: 28 additions & 75 deletions src/components/job/preheats/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ import {
import AddIcon from '@mui/icons-material/Add';
import MoreTimeIcon from '@mui/icons-material/MoreTime';
import { useNavigate, Link } from 'react-router-dom';
import { useContext, useEffect, useState } from 'react';
import { getJobs } from '../../../lib/api';
import { useEffect, useState } from 'react';
import { getJobs, getJobsResponse } from '../../../lib/api';
import { DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE } from '../../../lib/constants';
import { getDatetime, getPaginatedList } from '../../../lib/utils';
import { MyContext } from '../../menu/index';

export default function Preheats() {
const [errorMessage, setErrorMessage] = useState(false);
Expand All @@ -35,27 +34,8 @@ export default function Preheats() {
const [status, setStatus] = useState<string>('ALL');
const [shouldPoll, setShouldPoll] = useState(false);
const [openStatusSelect, setOpenStatusSelect] = useState(false);
const [allPreheats, setAllPreheats] = useState([
{
id: 0,
created_at: '',
updated_at: '',
is_del: 0,
task_id: '',
bio: '',
type: '',
state: '',
args: {
filter: '',
headers: {},
tag: '',
type: '',
url: '',
},
},
]);
const [allPreheats, setAllPreheats] = useState<getJobsResponse[]>([]);

const user = useContext(MyContext);
const navigate = useNavigate();

const theme = createTheme({
Expand All @@ -74,70 +54,43 @@ export default function Preheats() {
try {
setIsLoading(true);

if (user.name === 'root') {
const jobs = await getJobs({
page: 1,
per_page: MAX_PAGE_SIZE,
state: status === 'ALL' ? undefined : status,
});

setAllPreheats(jobs.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()));

const states = jobs.filter((obj) => obj.state !== 'SUCCESS' && obj.state !== 'FAILURE').length;
states === 0 ? setShouldPoll(false) : setShouldPoll(true);
const jobs = await getJobs({
page: 1,
per_page: MAX_PAGE_SIZE,
state: status === 'ALL' ? undefined : status,
});

setIsLoading(false);
} else if (user.name !== '') {
const jobs = await getJobs({
page: 1,
per_page: MAX_PAGE_SIZE,
state: status === 'ALL' ? undefined : status,
user_id: String(user.id),
});
setAllPreheats(jobs.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()));

setAllPreheats(jobs.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()));
const states = jobs.filter((obj) => obj.result.State !== 'SUCCESS' && obj.result.State !== 'FAILURE').length;
states === 0 ? setShouldPoll(false) : setShouldPoll(true);

const states = jobs.filter((obj) => obj.state !== 'SUCCESS' && obj.state !== 'FAILURE').length;
states === 0 ? setShouldPoll(false) : setShouldPoll(true);

setIsLoading(false);
}
setIsLoading(false);
} catch (error) {
if (error instanceof Error) {
setErrorMessage(true);
setErrorMessageText(error.message);
}
}
})();
}, [status, user]);
}, [status]);

useEffect(() => {
if (shouldPoll) {
const pollingInterval = setInterval(() => {
const pollPreheat = async () => {
try {
if (user.name === 'root') {
const jobs = await getJobs({
page: 1,
per_page: MAX_PAGE_SIZE,
state: status === 'ALL' ? undefined : status,
});
setAllPreheats(jobs.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()));
const jobs = await getJobs({
page: 1,
per_page: MAX_PAGE_SIZE,
state: status === 'ALL' ? undefined : status,
});
setAllPreheats(jobs.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()));

const states = jobs.filter((obj) => obj.state !== 'SUCCESS' && obj.state !== 'FAILURE').length;
states === 0 ? setShouldPoll(false) : setShouldPoll(true);
} else if (user.name !== '') {
const jobs = await getJobs({
page: 1,
per_page: MAX_PAGE_SIZE,
state: status === 'ALL' ? undefined : status,
user_id: String(user.id),
});
setAllPreheats(jobs.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()));

const states = jobs.filter((obj) => obj.state !== 'SUCCESS' && obj.state !== 'FAILURE').length;
states === 0 ? setShouldPoll(false) : setShouldPoll(true);
}
const states = jobs.filter(
(obj) => obj.result.State !== 'SUCCESS' && obj.result.State !== 'FAILURE',
).length;
states === 0 ? setShouldPoll(false) : setShouldPoll(true);
} catch (error) {
if (error instanceof Error) {
setErrorMessage(true);
Expand All @@ -153,7 +106,7 @@ export default function Preheats() {
clearInterval(pollingInterval);
};
}
}, [status, shouldPoll, user]);
}, [status, shouldPoll]);

const statusList = [
{ lable: 'Pending', name: 'PENDING' },
Expand Down Expand Up @@ -270,13 +223,13 @@ export default function Preheats() {
<Box sx={{ display: 'flex', alignItems: 'flex-start', width: '60%' }}>
{isLoading ? (
<Skeleton variant="circular" width="1.4rem" height="1.4rem" />
) : item.state === 'SUCCESS' ? (
) : item.result.State === 'SUCCESS' ? (
<Box
component="img"
sx={{ width: '1.3rem', height: '1.3rem' }}
src="/icons/job/preheat/success.svg"
/>
) : item.state === 'FAILURE' ? (
) : item.result.State === 'FAILURE' ? (
<Box
component="img"
sx={{ width: '1.3rem', height: '1.3rem' }}
Expand Down Expand Up @@ -342,13 +295,13 @@ export default function Preheats() {
<Box sx={{ display: 'flex', alignItems: 'flex-start', width: '60%' }}>
{isLoading ? (
<Skeleton variant="circular" width="1.4rem" height="1.4rem" />
) : item.state === 'SUCCESS' ? (
) : item.result.State === 'SUCCESS' ? (
<Box
component="img"
sx={{ width: '1.3rem', height: '1.3rem' }}
src="/icons/job/preheat/success.svg"
/>
) : item.state === 'FAILURE' ? (
) : item.result.State === 'FAILURE' ? (
<Box
component="img"
sx={{ width: '1.3rem', height: '1.3rem' }}
Expand Down
5 changes: 1 addition & 4 deletions src/components/job/preheats/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
Alert,
Paper,
} from '@mui/material';
import { useContext, useEffect, useState } from 'react';
import { useEffect, useState } from 'react';
import HelpIcon from '@mui/icons-material/Help';
import { LoadingButton } from '@mui/lab';
import CancelIcon from '@mui/icons-material/Cancel';
Expand All @@ -33,7 +33,6 @@ import { useNavigate } from 'react-router-dom';
import { createJob, getClusters } from '../../../lib/api';
import { MAX_PAGE_SIZE } from '../../../lib/constants';
import styles from './new.module.css';
import { MyContext } from '../../menu/index';
import AddIcon from '@mui/icons-material/Add';

export default function NewPreheat() {
Expand All @@ -53,7 +52,6 @@ export default function NewPreheat() {
const [clusterID, setClusterID] = useState<number[]>([]);

const navigate = useNavigate();
const user = useContext(MyContext);

const theme = createTheme({
palette: {
Expand Down Expand Up @@ -305,7 +303,6 @@ export default function NewPreheat() {
headers: headerList,
},
cdn_cluster_ids: clusterID,
user_id: user.id,
};

if (canSubmit) {
Expand Down
Loading

0 comments on commit c934135

Please sign in to comment.