Skip to content

Commit 299ad80

Browse files
committed
Authenticated to Status
1 parent ff1acf4 commit 299ad80

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/pages/index.tsx

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
} from "@mdi/js";
3232
import moment from "moment";
3333

34-
import { AuthenticationType, OAuth2 } from "@/types/general";
34+
import { Status, OAuth2 } from "@/types/general";
3535
import { GitHub } from "@/lib/github";
3636
import {
3737
IssueElement,
@@ -57,11 +57,11 @@ interface HomeProps {
5757
const github = new GitHub();
5858

5959
export default function Home({ clientId }: HomeProps): ReactElement {
60-
const [auth, setAuth] = useAuth();
6160
const [alert, setAlert] = useState<string>();
62-
const [authenticated, setAuthenticated] = useState<AuthenticationType>(0);
61+
const [auth, setAuth] = useAuth();
6362
const [authorizeUrl, setAuthorizeUrl] = useState<string>();
6463
const [repositoryData, setRepositoryData] = useRepository();
64+
const [status, setStatus] = useState<Status>(Status.NotAuthorized);
6565
const [, setUserData] = useUser();
6666
const [, setViewerData] = useViewer();
6767

@@ -72,7 +72,7 @@ export default function Home({ clientId }: HomeProps): ReactElement {
7272
setAlert(undefined);
7373
setAuthorizeUrl(undefined);
7474

75-
if (authenticated > AuthenticationType.NotAuthorized) return;
75+
if (status > Status.NotAuthorized) return;
7676
console.log("Authenticating...");
7777

7878
let oAuthData: OAuth2 | null = null;
@@ -96,12 +96,12 @@ export default function Home({ clientId }: HomeProps): ReactElement {
9696
);
9797
})();
9898
}
99-
setAuthenticated(AuthenticationType.Authenticated);
99+
setStatus(Status.Authenticated);
100100
return;
101101
}
102102

103103
if (code && code.length > 0 && state && state.length > 0) {
104-
setAuthenticated(AuthenticationType.Authenticating);
104+
setStatus(Status.Authenticating);
105105
console.log("Using code and state:", code, state);
106106
(async () => {
107107
oAuthData = await github.authenticate(
@@ -133,19 +133,19 @@ export default function Home({ clientId }: HomeProps): ReactElement {
133133

134134
setAuth(oAuthData);
135135
github.setAuth(oAuthData);
136-
setAuthenticated(AuthenticationType.Authenticated);
136+
setStatus(Status.Authenticated);
137137
})();
138138
return;
139139
} else {
140140
const url = github.getAuthorizeUrl(clientId, window.location.origin);
141141
console.log("Generating authorize URL:", url);
142142
setAuthorizeUrl(url);
143143
}
144-
}, [authenticated, clientId, code, router, setAuth, state]);
144+
}, [status, clientId, code, router, setAuth, state]);
145145

146146
useEffect(() => {
147-
console.log("Authenticated:", authenticated);
148-
if (authenticated !== AuthenticationType.Authenticated) return;
147+
console.log("Status:", status);
148+
if (status !== Status.Authenticated) return;
149149
(async () => {
150150
if (!github.auth)
151151
if (!auth) return;
@@ -159,16 +159,19 @@ export default function Home({ clientId }: HomeProps): ReactElement {
159159
}
160160
setViewerData(data.viewer);
161161
})();
162-
}, [auth, authenticated, setViewerData]);
162+
}, [auth, status, setViewerData]);
163163

164164
useEffect(() => {
165-
if (authenticated !== AuthenticationType.Authenticated) return;
165+
if (status !== Status.Authenticated) return;
166166
let type: string | null = null,
167167
owner: string | null = null,
168168
repository: string | null = null;
169169
const currentRepositoryStr =
170170
window.localStorage.getItem("currentRepository");
171-
if (!currentRepositoryStr) return;
171+
if (!currentRepositoryStr) {
172+
setStatus(Status.NoRepository);
173+
return;
174+
}
172175
try {
173176
const currentRepository = JSON.parse(currentRepositoryStr);
174177
type = currentRepository.type;
@@ -205,7 +208,7 @@ export default function Home({ clientId }: HomeProps): ReactElement {
205208
}
206209
setUserData(data.user);
207210
});
208-
}, [authenticated, setRepositoryData, setUserData]);
211+
}, [status, setRepositoryData, setUserData]);
209212

210213
const daysSince = useMemo<number>(() => {
211214
const firstDate = moment().subtract(1, "month").startOf("month").toDate();
@@ -441,7 +444,13 @@ export default function Home({ clientId }: HomeProps): ReactElement {
441444
container
442445
alignContent="space-around"
443446
justifyContent="space-around">
444-
<CircularProgress color="primary" />
447+
{status === Status.NoRepository ? (
448+
<Typography variant="h4" noWrap>
449+
Please select a repository
450+
</Typography>
451+
) : (
452+
<CircularProgress color="primary" />
453+
)}
445454
</Grid>
446455
)}
447456
</Grid>

src/types/general.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ export interface Picker {
33
label: string;
44
}
55

6-
export enum AuthenticationType {
6+
export enum Status {
77
NotAuthorized = 0,
88
Authenticating = 1,
99
Authenticated = 2,
1010
NotAuthenticated = 3,
11+
NoRepository = 4,
1112
}
1213

1314
export interface OAuth2 {

0 commit comments

Comments
 (0)