Add channel and role api

This commit is contained in:
Aslan 2026-01-02 16:23:21 +01:00
parent 8881070cac
commit 1fbd120404
22 changed files with 538 additions and 2 deletions

View file

@ -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<IFetchChannelResponse> => {
return await callApi(HTTP.GET, `channel/${request.id}`);
};
const createChannelApi = async (
request: ICreateChannelRequest,
): Promise<ICreateChannelResponse> => {
return await callApi(HTTP.POST, `channel`, request);
};
const updateChannelApi = async (
request: IUpdateChannelRequest,
): Promise<IUpdateChannelResponse> => {
return await callApi(HTTP.PATCH, `channel/${request.id}`, request);
};
const removeChannelApi = async (
request: IRemoveChannelRequest,
): Promise<IRemoveChannelResponse> => {
return await callApi(HTTP.DELETE, `channel/${request.id}`, request);
};
export {
fetchChannelApi,
createChannelApi,
updateChannelApi,
removeChannelApi,
};

2
src/api/channel/index.ts Normal file
View file

@ -0,0 +1,2 @@
export * from "./channel";
export * from "./types";

47
src/api/channel/types.ts Normal file
View file

@ -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,
};

2
src/api/role/index.ts Normal file
View file

@ -0,0 +1,2 @@
export * from "./role";
export * from "./types";

37
src/api/role/role.ts Normal file
View file

@ -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<IFetchRoleResponse> => {
return await callApi(HTTP.GET, `role/${request.id}`);
};
const createRoleApi = async (
request: ICreateRoleRequest,
): Promise<ICreateRoleResponse> => {
return await callApi(HTTP.POST, `role`, request);
};
const updateRoleApi = async (
request: IUpdateRoleRequest,
): Promise<IUpdateRoleResponse> => {
return await callApi(HTTP.PATCH, `role/${request.id}`, request);
};
const removeRoleApi = async (
request: IRemoveRoleRequest,
): Promise<IRemoveRoleResponse> => {
return await callApi(HTTP.DELETE, `role/${request.id}`, request);
};
export { fetchRoleApi, createRoleApi, updateRoleApi, removeRoleApi };

47
src/api/role/types.ts Normal file
View file

@ -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,
};

View file

@ -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 };

View file

@ -0,0 +1 @@
export * from "./channel";

View file

@ -0,0 +1 @@
export * from "./role";

56
src/services/role/role.ts Normal file
View file

@ -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 };

View file

@ -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 };

View file

@ -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 };

View file

@ -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 };

View file

@ -0,0 +1,3 @@
export * from "./channel";
export * from "./actions";
export * from "./types";

View file

@ -0,0 +1,11 @@
interface IChannelState {
channels: Record<string, IChannel>;
}
interface IChannel {
id: string;
name?: string;
communityId?: string;
}
export { type IChannelState, type IChannel };

View file

@ -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 };

42
src/store/role/actions.ts Normal file
View file

@ -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 };

3
src/store/role/index.ts Normal file
View file

@ -0,0 +1,3 @@
export * from "./role";
export * from "./actions";
export * from "./types";

52
src/store/role/role.ts Normal file
View file

@ -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 };

11
src/store/role/types.ts Normal file
View file

@ -0,0 +1,11 @@
interface IRoleState {
roles: Record<string, IRole>;
}
interface IRole {
id: string;
name?: string;
communityId?: string;
}
export { type IRoleState, type IRole };

View file

@ -14,6 +14,12 @@ const [state, setState] = createStore<IState>({
community: {
communities: {},
},
channel: {
channels: {},
},
role: {
roles: {},
},
});
function dispatch(action: Action) {

View file

@ -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 };