moving online

This commit is contained in:
QkoSad
2022-11-07 19:38:16 +02:00
parent 53737bce7f
commit 6b5c61de0b
34 changed files with 2005 additions and 144 deletions
+22
View File
@@ -0,0 +1,22 @@
import { createContext, useReducer } from "react";
import alertReducer from "./AlertReducer";
const AlertContext = createContext();
export const AlertProvider = ({ children }) => {
const initialState = null;
const [state, dispatch] = useReducer(alertReducer, initialState);
const setAlert = (msg, type)=>{
dispatch({
type:"SET_ALERT",
payload:{msg,type}
})
setTimeout(()=> dispatch({type:"REMOVE_ALERT"}),3000)
}
return (
<AlertContext.Provider value={{ alert: state ,setAlert}}>
{children}
</AlertContext.Provider>
);
};
export default AlertContext;
+11
View File
@@ -0,0 +1,11 @@
const alertReducer = (state, action) => {
switch (action.type) {
case "SET_ALERT":
return action.payload;
case "REMOVE_ALERT":
return null;
default:
return state;
}
};
export default alertReducer;
+27
View File
@@ -0,0 +1,27 @@
import axios from 'axios'
const GITHUB_URL = process.env.REACT_APP_GITHUB_URL
const GITHUB_TOKEN = process.env.REACT_APP_GITHUB_TOKEN
const github = axios.create({
baseURL: GITHUB_URL,
headers: { Authorization: `token ${GITHUB_TOKEN}` },
})
export const searchUsers = async (text) => {
const params = new URLSearchParams({
q: text,
})
const response = await github.get(`/search/users?${params}`)
return response.data.items
}
export const getUserAndRepos = async (login) => {
const [user, repos] = await Promise.all([
github.get(`/users/${login}`),
github.get(`/users/${login}/repos`),
])
return { user: user.data, repos: repos.data }
}
+25
View File
@@ -0,0 +1,25 @@
import { createContext, useReducer } from "react";
import githubReducer from "./GithubReducer";
const GithubContext = createContext();
export const GithubProvider = ({ children }) => {
const initialState = {
users: [],
user:{},
repos:[],
loading: false,
};
const [state, dispatch] = useReducer(githubReducer, initialState);
return (
<GithubContext.Provider
value={{
...state,
dispatch,
}}
>
{children}
</GithubContext.Provider>
);
};
export default GithubContext;
+30
View File
@@ -0,0 +1,30 @@
const githubReducer = (state, action) => {
switch (action.type) {
case "GET_USERS":
return {
...state,
users: action.payload,
loading: false,
};
case "SET_LOADING":
return {
...state,
loading: true,
};
case "CLEAR_USERS":
return {
...state,
users: [],
};
case "GET_USER_AND_REPOS":
return{
...state,
user: action.payload.user,
repos: action.payload.repos,
loading: false
}
default:
return state;
}
};
export default githubReducer;