diff --git a/src/components/MessageBar/MessageBar.tsx b/src/components/MessageBar/MessageBar.tsx index 143efff..f547396 100644 --- a/src/components/MessageBar/MessageBar.tsx +++ b/src/components/MessageBar/MessageBar.tsx @@ -1,36 +1,13 @@ import type { Component } from "solid-js"; -import { dispatch } from "../../store/state"; -import { UserActionTypes } from "../../store/user"; -import { AuthActionTypes } from "../../store/auth"; const MessageBar: Component = () => { - const onRefresh = () => { - dispatch({ - type: AuthActionTypes.FETCH_REFRESH_START, - }); - }; - const onSend = () => { - dispatch({ - type: UserActionTypes.FETCH_LOGGED_USER_ID_START, - }); - }; - return (
- -
diff --git a/src/store/auth/auth.ts b/src/store/auth/auth.ts index b304ddb..569b4ed 100644 --- a/src/store/auth/auth.ts +++ b/src/store/auth/auth.ts @@ -1,27 +1,22 @@ import { fetchLogin, fetchRefresh } from "../../services/auth"; +import { setState } from "../state"; import { AuthActionTypes, AuthAction } from "./actions"; import { IAuthState } from "./types"; -function authReducer(state: IAuthState, action: AuthAction): IAuthState { +function authReducer(_state: IAuthState, action: AuthAction) { switch (action.type) { case AuthActionTypes.FETCH_LOGIN_START: fetchLogin(action.payload.username, action.payload.password); - return state; + break; case AuthActionTypes.FETCH_LOGIN_FINISH: - return { - ...state, - session: action.payload, - }; + setState("auth", "session", action.payload); + break; case AuthActionTypes.FETCH_REFRESH_START: fetchRefresh(); - return state; + break; case AuthActionTypes.FETCH_REFRESH_FINISH: - return { - ...state, - session: action.payload, - }; - default: - return state; + setState("auth", "session", action.payload); + break; } } diff --git a/src/store/community/community.ts b/src/store/community/community.ts index 90c46e7..9c96aa9 100644 --- a/src/store/community/community.ts +++ b/src/store/community/community.ts @@ -1,33 +1,25 @@ import { fetchCommunity } from "../../services/community"; +import { setState } from "../state"; import { CommunityActionTypes, CommunityAction } from "./actions"; import { ICommunityState } from "./types"; -function communityReducer( - state: ICommunityState, - action: CommunityAction, -): ICommunityState { +function communityReducer(state: ICommunityState, action: CommunityAction) { switch (action.type) { case CommunityActionTypes.FETCH_COMMUNITY_START: fetchCommunity(action.payload); - return state; + break; case CommunityActionTypes.FETCH_COMMUNITY_FINISH: - return { - ...state, - communities: { - ...state.communities, - [action.payload.id]: action.payload, - }, - }; + setState("community", "communities", { + ...state.communities, + [action.payload.id]: action.payload, + }); + break; case CommunityActionTypes.SET_COMMUNITY: - return { - ...state, - communities: { - ...state.communities, - [action.payload.id]: action.payload, - }, - }; - default: - return state; + setState("community", "communities", { + ...state.communities, + [action.payload.id]: action.payload, + }); + break; } } diff --git a/src/store/reducers.ts b/src/store/reducers.ts index 4e25f89..80d03a6 100644 --- a/src/store/reducers.ts +++ b/src/store/reducers.ts @@ -4,12 +4,10 @@ import { AuthAction, authReducer } from "./auth"; import { UserAction, userReducer } from "./user"; import { CommunityAction, communityReducer } from "./community"; -function reducer(state: IState, action: Action): IState { - return { - auth: authReducer(state.auth, action as AuthAction), - user: userReducer(state.user, action as UserAction), - community: communityReducer(state.community, action as CommunityAction), - }; +function reducer(state: IState, action: Action) { + authReducer(state.auth, action as AuthAction); + userReducer(state.user, action as UserAction); + communityReducer(state.community, action as CommunityAction); } export { reducer }; diff --git a/src/store/state.ts b/src/store/state.ts index 40a10ef..bb35919 100644 --- a/src/store/state.ts +++ b/src/store/state.ts @@ -17,11 +17,7 @@ const [state, setState] = createStore({ }); function dispatch(action: Action) { - setState(reducer(state, action)); + reducer(state, action); } -async function dispatchAsync(action: Action) { - setState(reducer(state, action)); -} - -export { state, setState, dispatch, dispatchAsync }; +export { state, setState, dispatch }; diff --git a/src/store/user/user.ts b/src/store/user/user.ts index 0cfd8d0..746b4d2 100644 --- a/src/store/user/user.ts +++ b/src/store/user/user.ts @@ -3,48 +3,41 @@ import { fetchUser, fetchUserCommunities, } from "../../services/user"; +import { setState } from "../state"; import { UserActionTypes, UserAction } from "./actions"; import { IUserState } from "./types"; -function userReducer(state: IUserState, action: UserAction): IUserState { +function userReducer(state: IUserState, action: UserAction) { switch (action.type) { case UserActionTypes.FETCH_LOGGED_USER_ID_START: fetchLoggedUser(); - return state; + break; case UserActionTypes.FETCH_LOGGED_USER_ID_FINISH: - return { - ...state, - loggedUserId: action.payload.id, - }; + setState("user", "loggedUserId", action.payload.id); + break; case UserActionTypes.FETCH_USER_START: fetchUser(action.payload); - return state; + break; case UserActionTypes.FETCH_USER_FINISH: - return { - ...state, - users: { - ...state.users, - [action.payload.id]: action.payload, - }, - }; + setState("user", "users", { + ...state.users, + [action.payload.id]: action.payload, + }); + break; case UserActionTypes.FETCH_USER_COMMUNITIES_START: fetchUserCommunities(action.payload); - return state; + break; case UserActionTypes.FETCH_USER_COMMUNITIES_FINISH: - return { - ...state, - users: { - ...state.users, - [action.payload.id]: { - id: action.payload.id, - communities: action.payload.communities.map( - (community) => community.id, - ), - }, + setState("user", "users", { + ...state.users, + [action.payload.id]: { + id: action.payload.id, + communities: action.payload.communities.map( + (community) => community.id, + ), }, - }; - default: - return state; + }); + break; } } diff --git a/src/views/CommunityView/CommunityView.tsx b/src/views/CommunityView/CommunityView.tsx index f7f8673..a21cc36 100644 --- a/src/views/CommunityView/CommunityView.tsx +++ b/src/views/CommunityView/CommunityView.tsx @@ -1,35 +1,48 @@ -import type { Component } from "solid-js"; +import { createMemo, type Component } from "solid-js"; import { Community } from "../../components/Community"; +import { state } from "../../store/state"; const CommunityView: Component = () => { - const communitiesTest = [ - { - id: "21", - communityName: "Community 1", - avatar: "https://img.daisyui.com/images/profile/demo/yellingcat@192.webp", - }, - { - id: "22", - communityName: "Gamer's Lair", - avatar: "https://img.daisyui.com/images/profile/demo/yellingcat@192.webp", - }, - ]; + const communityIds = createMemo(() => { + const loggedUserId = state.user.loggedUserId; + if (!loggedUserId) { + return []; + } + + const loggedUser = state.user.users[loggedUserId]; + if (!loggedUser) { + return []; + } + + return loggedUser.communities ?? []; + }); const onCommunityClick = (id: string) => { console.log(id); }; + const mapCommunity = (communityId: string) => { + const community = state.community.communities[communityId]; + if (!community) { + return undefined; + } + + return ( + + ); + }; + return (
- {communitiesTest.map((community) => ( - - ))} + {communityIds().map(mapCommunity)}
); diff --git a/src/views/LoginView/LoginView.tsx b/src/views/LoginView/LoginView.tsx index 3e0c09a..007e88f 100644 --- a/src/views/LoginView/LoginView.tsx +++ b/src/views/LoginView/LoginView.tsx @@ -1,11 +1,14 @@ -import { createSignal, type Component } from "solid-js"; -import { dispatch } from "../../store/state"; +import { createEffect, createSignal, type Component } from "solid-js"; +import { dispatch, state } from "../../store/state"; import { AuthActionTypes } from "../../store/auth"; +import { useNavigate } from "@solidjs/router"; const LoginView: Component = () => { const [getUsername, setUsername] = createSignal(""); const [getPassword, setPassword] = createSignal(""); + const navigate = useNavigate(); + const onLogin = () => { dispatch({ type: AuthActionTypes.FETCH_LOGIN_START, @@ -16,6 +19,12 @@ const LoginView: Component = () => { }); }; + createEffect(() => { + if (state.auth.session?.id) { + navigate("/"); + } + }); + return (
diff --git a/src/views/MainView/MainView.tsx b/src/views/MainView/MainView.tsx index b4e764d..71c6d45 100644 --- a/src/views/MainView/MainView.tsx +++ b/src/views/MainView/MainView.tsx @@ -1,9 +1,33 @@ -import type { Component } from "solid-js"; +import { createEffect, createMemo, onMount, type Component } from "solid-js"; import { ChannelView } from "../ChannelView"; import { ChatView } from "../ChatView"; import { MemberView } from "../MemberView"; +import { dispatch, state } from "../../store/state"; +import { UserActionTypes } from "../../store/user"; +import { AuthActionTypes } from "../../store/auth"; const MainView: Component = () => { + onMount(() => { + dispatch({ + type: AuthActionTypes.FETCH_REFRESH_START, + }); + }); + + createEffect(() => { + if (state.auth.session?.token) { + dispatch({ type: UserActionTypes.FETCH_LOGGED_USER_ID_START }); + } + }); + + createEffect(() => { + if (state.user.loggedUserId) { + dispatch({ + type: UserActionTypes.FETCH_USER_COMMUNITIES_START, + payload: state.user.loggedUserId, + }); + } + }); + return (