Skip to content

Commit dc1b0f3

Browse files
Akpjunior94Ifycode
authored andcommitted
update collabocat auth
1 parent e7fe5f6 commit dc1b0f3

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

src/@core/api-client.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ export const apiClient = async <T>(endpoint: string, options: RequestInit = {}):
1616
headers,
1717
});
1818

19+
const data = await response.json();
20+
1921
if (!response.ok) {
20-
const errorData = await response.json();
21-
throw new Error(errorData.message || `Error ${response.status}: ${response.statusText}`);
22+
// 🔥 throw structured error instead of plain Error
23+
throw {
24+
status: response.status,
25+
message: data?.error?.message || response.statusText,
26+
raw: data
27+
};
2228
}
2329

2430
// Type casting to ensure that response data is in the expected format
25-
return (await response.json()) as T;
31+
return data as T;
2632
} catch (error) {
2733
console.log(`API Error [${API_BASE_URL}${endpoint}]:`, error);
2834
throw error;

src/components/@hooks_state/useSubmitIssue.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { useState } from 'react';
1+
import { useEffect, useState } from 'react';
22
import { createContainer } from 'unstated-next';
33
import { submitIssue, submitAnonymousIssue } from '../../@core/submitIssue';
44
import { GitHubIssueTemplateContainer } from './useTemplate';
5-
import { signInWithGithub } from '../../@core/auth';
65

76
function useSubmitIssueState (){
87
const { issueBody, setIssueBody, issueTitle, setIssueTitle, setDropdownButtonTitle, dropdownButtonText } = GitHubIssueTemplateContainer.useContainer();
@@ -15,19 +14,9 @@ function useSubmitIssueState (){
1514
setShowButton(!showButton);
1615
}
1716

18-
const loginWithGithub = () => {
19-
signInWithGithub()
20-
}
21-
2217
// Shared submission logic
2318
const handleSubmission = async (data: { title: string; body: string }, isAnonymous: boolean) => {
2419
try {
25-
// If the user is not authenticated, prompt them to log in
26-
if (!isAnonymous) {
27-
setToastrMessage({ message: 'Please log in to submit an issue.' });
28-
setShowGithubLoginButton(true);
29-
return;
30-
}
3120
const response = isAnonymous ? await submitAnonymousIssue(data) : await submitIssue(data);
3221

3322
setIssueTitle('');
@@ -40,11 +29,12 @@ function useSubmitIssueState (){
4029
setDropdownButtonTitle(dropdownButtonText);
4130

4231
return response;
43-
} catch (error) {
44-
setToastrMessage({
45-
message: 'Failed to submit issue'
46-
});
47-
throw error;
32+
} catch (error: any) {
33+
console.log('Error submitting issue:', error);
34+
if (error?.status === 401) {
35+
setShowGithubLoginButton(true);
36+
setToastrMessage({message: 'Please Login as a GitHub User to submit an issue ticket'});
37+
}
4838
}
4939
};
5040

@@ -71,7 +61,6 @@ function useSubmitIssueState (){
7161
handleSubmitAnonymous,
7262
handleButtonClick,
7363
showButton,
74-
loginWithGithub,
7564
showGithubLoginButton
7665
};
7766
}

src/components/@section_modal/ModalPopup.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const ModalPopup: React.FunctionComponent<ModalPopupProps> = (props: Moda
1010
const { instanceId, updateInstanceId } = GlobalContainer.useContainer();
1111
console.log('ModalPopup instance_id:', instanceId);
1212
const { issueBody, setIssueBody, issueTitle, setIssueTitle } = GitHubIssueTemplateContainer.useContainer();
13-
const { toastrMessage, handleSubmit, showButton, handleButtonClick, handleSubmitAnonymous, loginWithGithub, showGithubLoginButton } = SubmitIssueContainer.useContainer();
13+
const { toastrMessage, handleSubmit, showButton, handleButtonClick, handleSubmitAnonymous, showGithubLoginButton } = SubmitIssueContainer.useContainer();
1414

1515
return (
1616
<>
@@ -67,7 +67,7 @@ export const ModalPopup: React.FunctionComponent<ModalPopupProps> = (props: Moda
6767
</div>
6868
)}
6969
{showGithubLoginButton && (
70-
<button className="bb-collabocate_github-button" onClick={loginWithGithub}>Login with Github</button>
70+
<a href='/login' className="bb-collabocate_github-button">Login with Github</a>
7171
)}
7272
</div>
7373
</div>

0 commit comments

Comments
 (0)