import { fetchCommunity, createCommunity, updateCommunity, removeCommunity, fetchCommunityChannels, fetchCommunityRoles, fetchCommunityMembers, fetchCommunityInvites, } from "../../services/community"; import { setState } from "../state"; import { CommunityActionTypes, CommunityAction } from "./actions"; import { ICommunityState } from "./types"; function communityReducer(_state: ICommunityState, action: CommunityAction) { switch (action.type) { case CommunityActionTypes.SET_COMMUNITY: setState( "community", "communities", action.payload.id, action.payload, ); break; case CommunityActionTypes.SET_ACTIVE_COMMUNITY: setState("community", "active", action.payload); break; case CommunityActionTypes.FETCH_COMMUNITY_START: fetchCommunity(action.payload); break; case CommunityActionTypes.FETCH_COMMUNITY_FINISH: setState( "community", "communities", action.payload.id, action.payload, ); break; case CommunityActionTypes.CREATE_COMMUNITY_START: createCommunity(action.payload.name); break; case CommunityActionTypes.CREATE_COMMUNITY_FINISH: setState( "community", "communities", action.payload.id, action.payload, ); break; case CommunityActionTypes.UPDATE_COMMUNITY_START: updateCommunity( action.payload.id, action.payload.name, action.payload.description, ); break; case CommunityActionTypes.UPDATE_COMMUNITY_FINISH: setState( "community", "communities", action.payload.id, action.payload, ); break; case CommunityActionTypes.REMOVE_COMMUNITY_START: removeCommunity(action.payload); break; case CommunityActionTypes.REMOVE_COMMUNITY_FINISH: setState("community", "communities", (communities) => { const copy = { ...communities }; delete copy[action.payload.id]; return copy; }); break; case CommunityActionTypes.FETCH_COMMUNITY_CHANNELS_START: fetchCommunityChannels(action.payload); break; case CommunityActionTypes.FETCH_COMMUNITY_CHANNELS_FINISH: setState("community", "communities", action.payload.id, { channels: action.payload.channels.map((channel) => channel.id), }); break; case CommunityActionTypes.FETCH_COMMUNITY_ROLES_START: fetchCommunityRoles(action.payload); break; case CommunityActionTypes.FETCH_COMMUNITY_ROLES_FINISH: setState("community", "communities", action.payload.id, { roles: action.payload.roles.map((role) => role.id), }); break; case CommunityActionTypes.FETCH_COMMUNITY_MEMBERS_START: fetchCommunityMembers(action.payload); break; case CommunityActionTypes.FETCH_COMMUNITY_MEMBERS_FINISH: setState("community", "communities", action.payload.id, { members: action.payload.members.map((member) => member.id), }); break; case CommunityActionTypes.FETCH_COMMUNITY_INVITES_START: fetchCommunityInvites(action.payload); break; case CommunityActionTypes.FETCH_COMMUNITY_INVITES_FINISH: setState("community", "communities", action.payload.id, { invites: action.payload.invites.map((invite) => invite.id), }); break; } } export { communityReducer };