moving online
This commit is contained in:
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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 }
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user