diff --git a/src/api/channel/channel.ts b/src/api/channel/channel.ts new file mode 100644 index 0000000..beb42c9 --- /dev/null +++ b/src/api/channel/channel.ts @@ -0,0 +1,42 @@ +import { callApi, HTTP } from "../tools"; +import { + IFetchChannelRequest, + IFetchChannelResponse, + ICreateChannelRequest, + ICreateChannelResponse, + IUpdateChannelRequest, + IUpdateChannelResponse, + IRemoveChannelRequest, + IRemoveChannelResponse, +} from "./types"; + +const fetchChannelApi = async ( + request: IFetchChannelRequest, +): Promise => { + return await callApi(HTTP.GET, `channel/${request.id}`); +}; + +const createChannelApi = async ( + request: ICreateChannelRequest, +): Promise => { + return await callApi(HTTP.POST, `channel`, request); +}; + +const updateChannelApi = async ( + request: IUpdateChannelRequest, +): Promise => { + return await callApi(HTTP.PATCH, `channel/${request.id}`, request); +}; + +const removeChannelApi = async ( + request: IRemoveChannelRequest, +): Promise => { + return await callApi(HTTP.DELETE, `channel/${request.id}`, request); +}; + +export { + fetchChannelApi, + createChannelApi, + updateChannelApi, + removeChannelApi, +}; diff --git a/src/api/channel/index.ts b/src/api/channel/index.ts new file mode 100644 index 0000000..d2c9cbe --- /dev/null +++ b/src/api/channel/index.ts @@ -0,0 +1,2 @@ +export * from "./channel"; +export * from "./types"; diff --git a/src/api/channel/types.ts b/src/api/channel/types.ts new file mode 100644 index 0000000..797ff36 --- /dev/null +++ b/src/api/channel/types.ts @@ -0,0 +1,47 @@ +interface IFetchChannel { + id: string; + name: string; + communityId: string; + creationDate: number; +} + +interface IFetchChannelRequest { + id: string; +} + +interface IFetchChannelResponse extends IFetchChannel {} + +interface ICreateChannelRequest { + name: string; + communityId: string; +} + +interface ICreateChannelResponse extends IFetchChannel {} + +interface IUpdateChannelRequest { + id: string; + name?: string; +} + +interface IUpdateChannelResponse extends IFetchChannel {} + +interface IRemoveChannelRequest { + id: string; +} + +interface IRemoveChannelResponse { + id: string; + communityId: string; +} + +export { + type IFetchChannel, + type IFetchChannelRequest, + type IFetchChannelResponse, + type ICreateChannelRequest, + type ICreateChannelResponse, + type IUpdateChannelRequest, + type IUpdateChannelResponse, + type IRemoveChannelResponse, + type IRemoveChannelRequest, +}; diff --git a/src/api/role/index.ts b/src/api/role/index.ts new file mode 100644 index 0000000..2ae6a59 --- /dev/null +++ b/src/api/role/index.ts @@ -0,0 +1,2 @@ +export * from "./role"; +export * from "./types"; diff --git a/src/api/role/role.ts b/src/api/role/role.ts new file mode 100644 index 0000000..4c9d762 --- /dev/null +++ b/src/api/role/role.ts @@ -0,0 +1,37 @@ +import { callApi, HTTP } from "../tools"; +import { + IFetchRoleRequest, + IFetchRoleResponse, + ICreateRoleRequest, + ICreateRoleResponse, + IUpdateRoleRequest, + IUpdateRoleResponse, + IRemoveRoleRequest, + IRemoveRoleResponse, +} from "./types"; + +const fetchRoleApi = async ( + request: IFetchRoleRequest, +): Promise => { + return await callApi(HTTP.GET, `role/${request.id}`); +}; + +const createRoleApi = async ( + request: ICreateRoleRequest, +): Promise => { + return await callApi(HTTP.POST, `role`, request); +}; + +const updateRoleApi = async ( + request: IUpdateRoleRequest, +): Promise => { + return await callApi(HTTP.PATCH, `role/${request.id}`, request); +}; + +const removeRoleApi = async ( + request: IRemoveRoleRequest, +): Promise => { + return await callApi(HTTP.DELETE, `role/${request.id}`, request); +}; + +export { fetchRoleApi, createRoleApi, updateRoleApi, removeRoleApi }; diff --git a/src/api/role/types.ts b/src/api/role/types.ts new file mode 100644 index 0000000..ab15a58 --- /dev/null +++ b/src/api/role/types.ts @@ -0,0 +1,47 @@ +interface IFetchRole { + id: string; + name: string; + communityId: string; + creationDate: number; +} + +interface IFetchRoleRequest { + id: string; +} + +interface IFetchRoleResponse extends IFetchRole {} + +interface ICreateRoleRequest { + name: string; + communityId: string; +} + +interface ICreateRoleResponse extends IFetchRole {} + +interface IUpdateRoleRequest { + id: string; + name?: string; +} + +interface IUpdateRoleResponse extends IFetchRole {} + +interface IRemoveRoleRequest { + id: string; +} + +interface IRemoveRoleResponse { + id: string; + communityId: string; +} + +export { + type IFetchRole, + type IFetchRoleRequest, + type IFetchRoleResponse, + type ICreateRoleRequest, + type ICreateRoleResponse, + type IUpdateRoleRequest, + type IUpdateRoleResponse, + type IRemoveRoleResponse, + type IRemoveRoleRequest, +}; diff --git a/src/services/channel/channel.ts b/src/services/channel/channel.ts new file mode 100644 index 0000000..aeab36c --- /dev/null +++ b/src/services/channel/channel.ts @@ -0,0 +1,56 @@ +import { + fetchChannelApi, + createChannelApi, + updateChannelApi, + removeChannelApi, +} from "../../api/channel"; +import { ChannelActionTypes } from "../../store/channel"; +import { dispatch } from "../../store/state"; + +const fetchChannel = async (id: string) => { + const data = await fetchChannelApi({ + id: id, + }); + + dispatch({ + type: ChannelActionTypes.FETCH_CHANNEL_FINISH, + payload: data, + }); +}; + +const createChannel = async (name: string, communityId: string) => { + const data = await createChannelApi({ + name: name, + communityId: communityId, + }); + + dispatch({ + type: ChannelActionTypes.CREATE_CHANNEL_FINISH, + payload: data, + }); +}; + +const updateChannel = async (id: string, name?: string) => { + const data = await updateChannelApi({ + id: id, + name: name, + }); + + dispatch({ + type: ChannelActionTypes.UPDATE_CHANNEL_FINISH, + payload: data, + }); +}; + +const removeChannel = async (id: string) => { + const data = await removeChannelApi({ + id: id, + }); + + dispatch({ + type: ChannelActionTypes.REMOVE_CHANNEL_FINISH, + payload: data, + }); +}; + +export { fetchChannel, createChannel, updateChannel, removeChannel }; diff --git a/src/services/channel/index.ts b/src/services/channel/index.ts new file mode 100644 index 0000000..6bef221 --- /dev/null +++ b/src/services/channel/index.ts @@ -0,0 +1 @@ +export * from "./channel"; diff --git a/src/services/role/index.ts b/src/services/role/index.ts new file mode 100644 index 0000000..d405600 --- /dev/null +++ b/src/services/role/index.ts @@ -0,0 +1 @@ +export * from "./role"; diff --git a/src/services/role/role.ts b/src/services/role/role.ts new file mode 100644 index 0000000..aa7d706 --- /dev/null +++ b/src/services/role/role.ts @@ -0,0 +1,56 @@ +import { + fetchRoleApi, + createRoleApi, + updateRoleApi, + removeRoleApi, +} from "../../api/role"; +import { RoleActionTypes } from "../../store/role"; +import { dispatch } from "../../store/state"; + +const fetchRole = async (id: string) => { + const data = await fetchRoleApi({ + id: id, + }); + + dispatch({ + type: RoleActionTypes.FETCH_ROLE_FINISH, + payload: data, + }); +}; + +const createRole = async (name: string, communityId: string) => { + const data = await createRoleApi({ + name: name, + communityId: communityId, + }); + + dispatch({ + type: RoleActionTypes.CREATE_ROLE_FINISH, + payload: data, + }); +}; + +const updateRole = async (id: string, name?: string) => { + const data = await updateRoleApi({ + id: id, + name: name, + }); + + dispatch({ + type: RoleActionTypes.UPDATE_ROLE_FINISH, + payload: data, + }); +}; + +const removeRole = async (id: string) => { + const data = await removeRoleApi({ + id: id, + }); + + dispatch({ + type: RoleActionTypes.REMOVE_ROLE_FINISH, + payload: data, + }); +}; + +export { fetchRole, createRole, updateRole, removeRole }; diff --git a/src/store/actions.ts b/src/store/actions.ts index bace320..cfbeedb 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -1,9 +1,21 @@ import { AuthActionTypes, AuthAction } from "./auth"; import { UserActionTypes, UserAction } from "./user"; import { CommunityActionTypes, CommunityAction } from "./community"; +import { ChannelActionTypes, ChannelAction } from "./channel"; +import { RoleActionTypes, RoleAction } from "./role"; -type ActionTypes = AuthActionTypes | UserActionTypes | CommunityActionTypes; +type ActionTypes = + | AuthActionTypes + | UserActionTypes + | CommunityActionTypes + | ChannelActionTypes + | RoleActionTypes; -type Action = AuthAction | UserAction | CommunityAction; +type Action = + | AuthAction + | UserAction + | CommunityAction + | ChannelAction + | RoleAction; export { type Action, type ActionTypes }; diff --git a/src/store/channel/actions.ts b/src/store/channel/actions.ts new file mode 100644 index 0000000..b068c34 --- /dev/null +++ b/src/store/channel/actions.ts @@ -0,0 +1,45 @@ +import { + ICreateChannelRequest, + IFetchChannelResponse, + ICreateChannelResponse, + IUpdateChannelResponse, + IRemoveChannelResponse, +} from "../../api/channel"; + +enum ChannelActionTypes { + FETCH_CHANNEL_START = "FETCH_COMMUNITY_START", + FETCH_CHANNEL_FINISH = "FETCH_COMMUNITY_FINISH", + CREATE_CHANNEL_START = "CREATE_CHANNEL_START", + CREATE_CHANNEL_FINISH = "CREATE_CHANNEL_FINISH", + UPDATE_CHANNEL_START = "UPDATE_CHANNEL_START", + UPDATE_CHANNEL_FINISH = "UPDATE_CHANNEL_FINISH", + REMOVE_CHANNEL_START = "REMOVE_CHANNEL_START", + REMOVE_CHANNEL_FINISH = "REMOVE_CHANNEL_FINISH", +} + +type ChannelAction = + | { type: ChannelActionTypes.FETCH_CHANNEL_START; payload: string } + | { + type: ChannelActionTypes.FETCH_CHANNEL_FINISH; + payload: IFetchChannelResponse; + } + | { + type: ChannelActionTypes.CREATE_CHANNEL_START; + payload: ICreateChannelRequest; + } + | { + type: ChannelActionTypes.CREATE_CHANNEL_FINISH; + payload: ICreateChannelResponse; + } + | { type: ChannelActionTypes.UPDATE_CHANNEL_START; payload: string } + | { + type: ChannelActionTypes.UPDATE_CHANNEL_FINISH; + payload: IUpdateChannelResponse; + } + | { type: ChannelActionTypes.REMOVE_CHANNEL_START; payload: string } + | { + type: ChannelActionTypes.REMOVE_CHANNEL_FINISH; + payload: IRemoveChannelResponse; + }; + +export { ChannelActionTypes, type ChannelAction }; diff --git a/src/store/channel/channel.ts b/src/store/channel/channel.ts new file mode 100644 index 0000000..c254959 --- /dev/null +++ b/src/store/channel/channel.ts @@ -0,0 +1,52 @@ +import { + fetchChannel, + createChannel, + updateChannel, + removeChannel, +} from "../../services/channel"; +import { setState } from "../state"; +import { ChannelActionTypes, ChannelAction } from "./actions"; +import { IChannelState } from "./types"; + +function channelReducer(state: IChannelState, action: ChannelAction) { + switch (action.type) { + case ChannelActionTypes.FETCH_CHANNEL_START: + fetchChannel(action.payload); + break; + case ChannelActionTypes.FETCH_CHANNEL_FINISH: + setState("channel", "channels", { + ...state.channels, + [action.payload.id]: action.payload, + }); + break; + case ChannelActionTypes.CREATE_CHANNEL_START: + createChannel(action.payload.name, action.payload.communityId); + break; + case ChannelActionTypes.CREATE_CHANNEL_FINISH: + setState("channel", "channels", { + ...state.channels, + [action.payload.id]: action.payload, + }); + break; + case ChannelActionTypes.UPDATE_CHANNEL_START: + updateChannel(action.payload); + break; + case ChannelActionTypes.UPDATE_CHANNEL_FINISH: + setState("channel", "channels", { + ...state.channels, + [action.payload.id]: action.payload, + }); + break; + case ChannelActionTypes.REMOVE_CHANNEL_START: + removeChannel(action.payload); + break; + case ChannelActionTypes.REMOVE_CHANNEL_FINISH: + setState("channel", "channels", { + ...state.channels, + [action.payload.id]: undefined, + }); + break; + } +} + +export { channelReducer }; diff --git a/src/store/channel/index.ts b/src/store/channel/index.ts new file mode 100644 index 0000000..06d90ae --- /dev/null +++ b/src/store/channel/index.ts @@ -0,0 +1,3 @@ +export * from "./channel"; +export * from "./actions"; +export * from "./types"; diff --git a/src/store/channel/types.ts b/src/store/channel/types.ts new file mode 100644 index 0000000..cfbac09 --- /dev/null +++ b/src/store/channel/types.ts @@ -0,0 +1,11 @@ +interface IChannelState { + channels: Record; +} + +interface IChannel { + id: string; + name?: string; + communityId?: string; +} + +export { type IChannelState, type IChannel }; diff --git a/src/store/reducers.ts b/src/store/reducers.ts index 80d03a6..aa54721 100644 --- a/src/store/reducers.ts +++ b/src/store/reducers.ts @@ -3,11 +3,15 @@ import { Action } from "./actions"; import { AuthAction, authReducer } from "./auth"; import { UserAction, userReducer } from "./user"; import { CommunityAction, communityReducer } from "./community"; +import { ChannelAction, channelReducer } from "./channel"; +import { RoleAction, roleReducer } from "./role"; function reducer(state: IState, action: Action) { authReducer(state.auth, action as AuthAction); userReducer(state.user, action as UserAction); communityReducer(state.community, action as CommunityAction); + channelReducer(state.channel, action as ChannelAction); + roleReducer(state.role, action as RoleAction); } export { reducer }; diff --git a/src/store/role/actions.ts b/src/store/role/actions.ts new file mode 100644 index 0000000..694b445 --- /dev/null +++ b/src/store/role/actions.ts @@ -0,0 +1,42 @@ +import { + ICreateRoleRequest, + IFetchRoleResponse, + ICreateRoleResponse, + IUpdateRoleResponse, + IRemoveRoleResponse, +} from "../../api/role"; + +enum RoleActionTypes { + FETCH_ROLE_START = "FETCH_ROLE_START", + FETCH_ROLE_FINISH = "FETCH_ROLE_FINISH", + CREATE_ROLE_START = "CREATE_ROLE_START", + CREATE_ROLE_FINISH = "CREATE_ROLE_FINISH", + UPDATE_ROLE_START = "UPDATE_ROLE_START", + UPDATE_ROLE_FINISH = "UPDATE_ROLE_FINISH", + REMOVE_ROLE_START = "REMOVE_ROLE_START", + REMOVE_ROLE_FINISH = "REMOVE_ROLE_FINISH", +} + +type RoleAction = + | { type: RoleActionTypes.FETCH_ROLE_START; payload: string } + | { + type: RoleActionTypes.FETCH_ROLE_FINISH; + payload: IFetchRoleResponse; + } + | { type: RoleActionTypes.CREATE_ROLE_START; payload: ICreateRoleRequest } + | { + type: RoleActionTypes.CREATE_ROLE_FINISH; + payload: ICreateRoleResponse; + } + | { type: RoleActionTypes.UPDATE_ROLE_START; payload: string } + | { + type: RoleActionTypes.UPDATE_ROLE_FINISH; + payload: IUpdateRoleResponse; + } + | { type: RoleActionTypes.REMOVE_ROLE_START; payload: string } + | { + type: RoleActionTypes.REMOVE_ROLE_FINISH; + payload: IRemoveRoleResponse; + }; + +export { RoleActionTypes, type RoleAction }; diff --git a/src/store/role/index.ts b/src/store/role/index.ts new file mode 100644 index 0000000..f1b9dfc --- /dev/null +++ b/src/store/role/index.ts @@ -0,0 +1,3 @@ +export * from "./role"; +export * from "./actions"; +export * from "./types"; diff --git a/src/store/role/role.ts b/src/store/role/role.ts new file mode 100644 index 0000000..1a4ab5d --- /dev/null +++ b/src/store/role/role.ts @@ -0,0 +1,52 @@ +import { + fetchRole, + createRole, + updateRole, + removeRole, +} from "../../services/role"; +import { setState } from "../state"; +import { RoleActionTypes, RoleAction } from "./actions"; +import { IRoleState } from "./types"; + +function roleReducer(state: IRoleState, action: RoleAction) { + switch (action.type) { + case RoleActionTypes.FETCH_ROLE_START: + fetchRole(action.payload); + break; + case RoleActionTypes.FETCH_ROLE_FINISH: + setState("role", "roles", { + ...state.roles, + [action.payload.id]: action.payload, + }); + break; + case RoleActionTypes.CREATE_ROLE_START: + createRole(action.payload.name, action.payload.communityId); + break; + case RoleActionTypes.CREATE_ROLE_FINISH: + setState("role", "roles", { + ...state.roles, + [action.payload.id]: action.payload, + }); + break; + case RoleActionTypes.UPDATE_ROLE_START: + updateRole(action.payload); + break; + case RoleActionTypes.UPDATE_ROLE_FINISH: + setState("role", "roles", { + ...state.roles, + [action.payload.id]: action.payload, + }); + break; + case RoleActionTypes.REMOVE_ROLE_START: + removeRole(action.payload); + break; + case RoleActionTypes.REMOVE_ROLE_FINISH: + setState("role", "roles", { + ...state.roles, + [action.payload.id]: undefined, + }); + break; + } +} + +export { roleReducer }; diff --git a/src/store/role/types.ts b/src/store/role/types.ts new file mode 100644 index 0000000..16a8893 --- /dev/null +++ b/src/store/role/types.ts @@ -0,0 +1,11 @@ +interface IRoleState { + roles: Record; +} + +interface IRole { + id: string; + name?: string; + communityId?: string; +} + +export { type IRoleState, type IRole }; diff --git a/src/store/state.ts b/src/store/state.ts index bb35919..d9b2380 100644 --- a/src/store/state.ts +++ b/src/store/state.ts @@ -14,6 +14,12 @@ const [state, setState] = createStore({ community: { communities: {}, }, + channel: { + channels: {}, + }, + role: { + roles: {}, + }, }); function dispatch(action: Action) { diff --git a/src/store/types.ts b/src/store/types.ts index 334c00a..894ea7e 100644 --- a/src/store/types.ts +++ b/src/store/types.ts @@ -1,11 +1,15 @@ import { IAuthState } from "./auth"; import { IUserState } from "./user"; import { ICommunityState } from "./community"; +import { IChannelState } from "./channel"; +import { IRoleState } from "./role"; interface IState { auth: IAuthState; user: IUserState; community: ICommunityState; + channel: IChannelState; + role: IRoleState; } export { type IState };