chatReducer function

ChatState chatReducer(
  1. ChatState state,
  2. dynamic action
)

Implementation

ChatState chatReducer(ChatState state, dynamic action) {
  if (action is LoadMessagesAction) {
    // Replace existing messages with the loaded messages from gRPC
    return state.copyWith(
      messages: List<Message_text>.from(action.messages),
      isLoading: false,  // Optional: set loading to false when messages load
    );
  } else if (action is AddMessageAction) {
    return state.copyWith(
      messages: List.from(state.messages)..add(action.message),
    );
  } else if (action is SetLoadingAction) {
    return state.copyWith(isLoading: action.isLoading);
  }

  if (action is LogoutAction) {
    return ChatState.initial();  // Reset to initial state
  }
  return state;
}