Skip to content

Commit 6fd696b

Browse files
committed
Split SnippetsContext into separate files
1 parent f55cbc7 commit 6fd696b

17 files changed

+452
-211
lines changed

‎client/src/store/SnippetsContext.tsx

Lines changed: 0 additions & 178 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { User, UserWithRole, Response } from '../../../typescript/interfaces';
2+
import React from 'react';
3+
import axios from 'axios';
4+
import { authErrorHandler } from '../../../utils';
5+
6+
interface Params {
7+
token: string;
8+
setIsAuthenticated: (v: React.SetStateAction<boolean>) => void;
9+
setUser: (v: React.SetStateAction<UserWithRole | null>) => void;
10+
}
11+
12+
export const getUserProfile = async (params: Params) => {
13+
const { token, setIsAuthenticated, setUser } = params;
14+
15+
try {
16+
const res = await axios.get<Response<User>>('/api/auth/me', {
17+
headers: {
18+
Authorization: `Bearer ${token}`
19+
}
20+
});
21+
22+
const { ...user } = res.data.data;
23+
24+
setUser({
25+
...user,
26+
isAdmin: user.role === 'admin'
27+
});
28+
29+
setIsAuthenticated(true);
30+
} catch (err) {
31+
authErrorHandler({ err, setIsAuthenticated, setUser });
32+
}
33+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './registerUser';
22
export * from './loginUser';
33
export * from './logoutUser';
4+
export * from './getUserProfile';

‎client/src/store/auth/actions/loginUser.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { User, Response, UserWithRole } from '../../../typescript/interfaces';
2-
import { errorHandler } from '../../../utils';
2+
import { authErrorHandler } from '../../../utils';
33
import axios from 'axios';
44
import React from 'react';
55

@@ -31,9 +31,7 @@ export const loginUser = async (params: Params) => {
3131
localStorage.setItem('token', resToken);
3232

3333
setIsAuthenticated(true);
34-
35-
// redirect to snippets? / home?
3634
} catch (err) {
37-
errorHandler(err);
35+
authErrorHandler({ err, setIsAuthenticated, setUser });
3836
}
3937
};

‎client/src/store/auth/actions/registerUser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { User, Response, UserWithRole } from '../../../typescript/interfaces';
2-
import { errorHandler } from '../../../utils';
2+
import { authErrorHandler } from '../../../utils';
33
import axios from 'axios';
44
import React from 'react';
55

@@ -32,6 +32,6 @@ export const registerUser = async (params: Params) => {
3232

3333
setIsAuthenticated(true);
3434
} catch (err) {
35-
errorHandler(err);
35+
authErrorHandler({ err, setIsAuthenticated, setUser });
3636
}
3737
};

‎client/src/store/auth/index.tsx

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
import { createContext, ReactNode, useState } from 'react';
2-
import axios from 'axios';
1+
import { ReactNode, useState } from 'react';
2+
import { AuthContext } from '..';
33

4-
import { loginUser, logoutUser, registerUser } from './actions';
4+
import { getUserProfile, loginUser, logoutUser, registerUser } from './actions';
55

66
import {
77
AuthContext as Context,
8-
Response,
9-
User,
108
UserWithRole
119
} from '../../typescript/interfaces';
12-
import { errorHandler } from '../../utils';
13-
14-
export const AuthContext = createContext<Context>({
15-
isAuthenticated: false,
16-
user: null,
17-
autoLogin: () => {},
18-
login: () => {},
19-
logout: () => {},
20-
register: () => {}
21-
});
2210

2311
interface Props {
2412
children: ReactNode;
@@ -47,17 +35,7 @@ export const AuthContextProvider = (props: Props): JSX.Element => {
4735
};
4836

4937
const getProfile = async (token: string) => {
50-
try {
51-
const res = await axios.get<Response<User>>('/api/auth/me', {
52-
headers: {
53-
Authorization: `Bearer ${token}`
54-
}
55-
});
56-
57-
console.log(res.data.data);
58-
} catch (err) {
59-
errorHandler(err);
60-
}
38+
await getUserProfile({ token, setIsAuthenticated, setUser });
6139
};
6240

6341
const context: Context = {

‎client/src/store/contexts.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { createContext } from 'react';
2+
3+
import {
4+
SnippetsContext as SnippetsContextInterface,
5+
AuthContext as AuthContextInterface,
6+
NewSnippet,
7+
SearchQuery
8+
} from '../typescript/interfaces';
9+
10+
export const SnippetsContext = createContext<SnippetsContextInterface>({
11+
snippets: [],
12+
searchResults: [],
13+
currentSnippet: null,
14+
tagCount: [],
15+
getSnippets: () => {},
16+
getSnippetById: (id: number) => {},
17+
setSnippet: (id: number) => {},
18+
createSnippet: (snippet: NewSnippet) => {},
19+
updateSnippet: (snippet: NewSnippet, id: number, isLocal?: boolean) => {},
20+
deleteSnippet: (id: number) => {},
21+
toggleSnippetPin: (id: number) => {},
22+
countTags: () => {},
23+
searchSnippets: (query: SearchQuery) => {}
24+
});
25+
26+
export const AuthContext = createContext<AuthContextInterface>({
27+
isAuthenticated: false,
28+
user: null,
29+
autoLogin: () => {},
30+
login: () => {},
31+
logout: () => {},
32+
register: () => {}
33+
});

‎client/src/store/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export * from './SnippetsContext';
1+
export * from './snippets';
22
export * from './auth';
3+
export * from './contexts';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { SetStateAction } from 'react';
2+
import { errorHandler } from '../../../utils';
3+
import { Response, TagCount } from '../../../typescript/interfaces';
4+
import axios from 'axios';
5+
6+
interface Params {
7+
setTagCount: (v: SetStateAction<TagCount[]>) => void;
8+
}
9+
10+
export const countTagsAction = async (params: Params) => {
11+
const { setTagCount } = params;
12+
13+
const token = `Bearer ${localStorage.token}`;
14+
15+
try {
16+
const res = await axios.get<Response<TagCount[]>>(
17+
'/api/snippets/statistics/count',
18+
{
19+
headers: {
20+
Authorization: token
21+
}
22+
}
23+
);
24+
25+
setTagCount(res.data.data);
26+
} catch (err) {
27+
errorHandler(err);
28+
}
29+
};

0 commit comments

Comments
 (0)