Skip to content

Commit

Permalink
refactor: context type file and basic reducer funtion setup
Browse files Browse the repository at this point in the history
  • Loading branch information
nichgalzin committed Jun 5, 2024
1 parent d661e90 commit b63c900
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
12 changes: 12 additions & 0 deletions context/contextTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface IConversationState {
allConversations: AllConversationsType;
currentConversation: ConversationCardType | undefined;
showConversationsList: boolean;
currentUserId: string;
}

export type ConversationActionType =
| { type: 'SET_ALL_CONVERSATIONS'; payload: AllConversationsType }
| { type: 'SET_CURRENT_CONVERSATION'; payload: ConversationCardType }
| { type: 'SET_SHOW_CONVERSATIONS_LIST'; payload: boolean }
| { type: 'SET_CURRENT_USER_ID'; payload: string };
19 changes: 2 additions & 17 deletions context/conversationContext.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
'use client';
import { Dispatch, createContext, useContext, useReducer } from 'react';
import {
AllConversationsType,
ConversationCardType,
} from '@/types/messagingTypes';
import { ConversationActionType, IConversationState } from './contextTypes';
import conversationReducer from './conversationStore';

type ConversationContextProviderProps = {
children: React.ReactNode;
};

interface IConversationState {
allConversations: AllConversationsType;
currentConversation: ConversationCardType | undefined;
showConversationsList: boolean;
currentUserId: string;
}

type ConversationContextType = {
conversationState: IConversationState;
dispatch: Dispatch<ConversationActionType>;
};

type ConversationActionType =
| { type: 'SET_ALL_CONVERSATIONS'; payload: AllConversationsType }
| { type: 'SET_CURRENT_CONVERSATION'; payload: ConversationCardType }
| { type: 'SET_SHOW_CONVERSATIONS_LIST'; payload: boolean }
| { type: 'SET_CURRENT_USER_ID'; payload: string };

export const ConversationContext =
createContext<ConversationContextType | null>(null);

Expand Down
19 changes: 19 additions & 0 deletions context/conversationStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ConversationActionType, IConversationState } from './contextTypes';

export default function conversationReducer(
state: IConversationState,
action: ConversationActionType
): IConversationState {
switch (action.type) {
case 'SET_ALL_CONVERSATIONS':
return { ...state, allConversations: action.payload };
case 'SET_CURRENT_CONVERSATION':
return { ...state, currentConversation: action.payload };
case 'SET_SHOW_CONVERSATIONS_LIST':
return { ...state, showConversationsList: action.payload };
case 'SET_CURRENT_USER_ID':
return { ...state, currentUserId: action.payload };
default:
return state;
}
}

0 comments on commit b63c900

Please sign in to comment.