{"size": 1194, "ext": "js", "max_stars_repo_path": "client/store/singleUser.js", "max_stars_repo_name": "PJ-Stackathon/Stackathon", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "client/store/singleUser.js", "max_issues_repo_name": "PJ-Stackathon/Stackathon", "max_issues_repo_issues_event_min_datetime": "2022-03-11T16:20:32.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-14T10:21:58.000Z", "max_forks_repo_path": "client/store/singleUser.js", "max_forks_repo_name": "PJ-Stackathon/Stackathon", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 24, "max_forks_count": null, "avg_line_length": 19.5737704918, "max_line_length": 83, "alphanum_fraction": 0.6323283082}
import axios from "axios";

// ACTION TYPES:
const SET_SINGLE_USER = "SET_SINGLE_USER";

// ACTION CREATORS:
export const setSingleUser = user => ({
	type: SET_SINGLE_USER,
	user
});

// THUNK CREATORS:
export const fetchSingleUser = userId => {
	return async dispatch => {
		try {
			const token = window.localStorage.getItem("token");
			if (token) {
				const { data } = await axios.get(`/api/users/${userId}`, {
					headers: {
						authorization: token
					}
				});
				dispatch(setSingleUser(data));
			}
		} catch (error) {
			console.log(error);
		}
	};
};

export const updateUser = updatedUser => {
	return async dispatch => {
		try {
			const token = window.localStorage.getItem("token");
			if (token) {
				const { data } = await axios.put(`/api/users/${updatedUser.id}`, updatedUser, {
					headers: {
						authorization: token
					}
				});
				dispatch(setSingleUser(data));
			}
		} catch (error) {
			console.log(error);
		}
	};
};

// INITIAL STATE:
const initialState = {};

// SUB-REDUCER:
export default function singleUserReducer(state = initialState, action) {
	switch (action.type) {
		case SET_SINGLE_USER:
			return action.user;
		default:
			return state;
	}
}
