File size: 2,031 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import * as actionTypes from "./actionTypes";
import { combineReducers } from "redux";

const INITIAL_USER_STATE = {
	user: null
};

const userReducer = (state = INITIAL_USER_STATE, action) => {
	switch (action.type) {
		case actionTypes.LOGIN: {
			return {
				...state,
				user: action.payload.user,
				isLoading: false
			};
		}
		default:
			return state;
	}
};

const INITIAL_SERVER_STATE = {
	joinedServers: null,
	totalServers: null,
	isLoading: true,
	messages: null,
	server: null,
	channel: {
		categoryID: "general",
		id: "0",
		name: "general"
	},
	dm: "totalServers",
	role: null,
	roles: null,
	dms: null,
	call: null
};

const serverReducer = (state = INITIAL_SERVER_STATE, action) => {
	switch (action.type) {
		case actionTypes.ADD_TOTAL_SERVER: {
			return {
				...state,
				totalServers: action.payload
			};
		}

		case actionTypes.SELECT_SERVER: {
			return {
				...state,
				...action.payload
			};
		}
		case actionTypes.UPDATE_SERVER: {
			const servers = { ...state.joinedServers } || {};
			servers[action.payload.id] = action.payload.server;
			return {
				...state,
				joinedServers: servers
			};
		}
		case actionTypes.SET_SERVER_LOADING: {
			return {
				...state,
				isLoading: action.payload
			};
		}
		case actionTypes.REMOVE_SERVER: {
			const servers = { ...state.joinedServers } || {};
			delete servers[action.payload];
			return {
				...state,
				joinedServers: servers
			};
		}
		case actionTypes.ADD_MESSAGES: {
			const messages = { ...state.messages };
			messages[action.payload.serverId] = action.payload.channels;
			return {
				...state,
				messages
			};
		}
		case actionTypes.ADD_DM: {
			const dms = { ...state.dms } || {};
			dms[action.payload.id] = action.payload.dms;
			return {
				...state,
				dms: dms
			};
		}
		case actionTypes.SET_CALL: {
			return {
				...state,
				call: action.payload
			};
		}
		default:
			return state;
	}
};

const rootReducer = combineReducers({
	user: userReducer,
	server: serverReducer
});

export default rootReducer;