Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message date support - Load chat message history #275

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,14 @@ As of v3.0, messages now have an optional ID that can be added on creation.If yo
- params:
- text: string (supports markdown)
- id: string (optional)
- date: Date (optional)
- Method to add a new message written as a response to a user input.

- **addUserMessage**
- params:
- text: string (supports markdown)
- id: string (optional)
- date: Date (optional)
- This method will add a new message written as a user. Keep in mind it will not trigger the prop handleNewUserMessage()

- **addLinkSnippet**
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ declare const Widget: ElementType;

export function addUserMessage(text: string): void;
export function addUserMessage(text: string, id: string): void;
export function addUserMessage(text: string, id: string, date: Date): void;

export function addResponseMessage(text: string): void;
export function addResponseMessage(text: string, id: string): void;
export function addResponseMessage(text: string, id: string, date: Date): void;

export function addLinkSnippet(link: { link: string, title: string, target?: string }): void;
export function addLinkSnippet(link: { link: string, title: string, target?: string }, id: string): void;
Expand Down
10 changes: 6 additions & 4 deletions src/store/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ export function toggleInputDisabled(): actionsTypes.ToggleInputDisabled {
};
}

export function addUserMessage(text: string, id?: string): actionsTypes.AddUserMessage {
export function addUserMessage(text: string, id?: string, date?: Date): actionsTypes.AddUserMessage {
return {
type: actionsTypes.ADD_NEW_USER_MESSAGE,
text,
id
id,
date,
};
}

export function addResponseMessage(text: string, id?: string): actionsTypes.AddResponseMessage {
export function addResponseMessage(text: string, id?: string, date?: Date): actionsTypes.AddResponseMessage {
return {
type: actionsTypes.ADD_NEW_RESPONSE_MESSAGE,
text,
id
id,
date,
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/store/actions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ export interface AddUserMessage {
type: typeof ADD_NEW_USER_MESSAGE;
text: string;
id?: string;
date?: Date;
}

export interface AddResponseMessage {
type: typeof ADD_NEW_RESPONSE_MESSAGE;
text: string;
id?: string;
date?: Date;
}

export interface ToggleMsgLoader {
Expand Down
8 changes: 4 additions & 4 deletions src/store/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import store from '.';
import * as actions from './actions';
import { LinkParams, ImageState } from './types';

export function addUserMessage(text: string, id?: string) {
store.dispatch(actions.addUserMessage(text, id));
export function addUserMessage(text: string, id?: string, date?: Date) {
store.dispatch(actions.addUserMessage(text, id, date));
}

export function addResponseMessage(text: string, id?: string) {
store.dispatch(actions.addResponseMessage(text, id));
export function addResponseMessage(text: string, id?: string, date?: Date) {
store.dispatch(actions.addResponseMessage(text, id, date));
}

export function addLinkSnippet(link: LinkParams, id?: string) {
Expand Down
8 changes: 4 additions & 4 deletions src/store/reducers/messagesReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ const initialState = {
};

const messagesReducer = {
[ADD_NEW_USER_MESSAGE]: (state: MessagesState, { text, showClientAvatar, id }) =>
({ ...state, messages: [...state.messages, createNewMessage(text, MESSAGE_SENDER.CLIENT, id)]}),
[ADD_NEW_USER_MESSAGE]: (state: MessagesState, { text, showClientAvatar, id, date }) =>
({ ...state, messages: [...state.messages, createNewMessage(text, MESSAGE_SENDER.CLIENT, id, date)]}),

[ADD_NEW_RESPONSE_MESSAGE]: (state: MessagesState, { text, id }) =>
({ ...state, messages: [...state.messages, createNewMessage(text, MESSAGE_SENDER.RESPONSE, id)], badgeCount: state.badgeCount + 1 }),
[ADD_NEW_RESPONSE_MESSAGE]: (state: MessagesState, { text, id, date }) =>
({ ...state, messages: [...state.messages, createNewMessage(text, MESSAGE_SENDER.RESPONSE, id, date)], badgeCount: state.badgeCount + 1 }),

[ADD_NEW_LINK_SNIPPET]: (state: MessagesState, { link, id }) =>
({ ...state, messages: [...state.messages, createLinkSnippet(link, id)] }),
Expand Down
3 changes: 2 additions & 1 deletion src/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ export function createNewMessage(
text: string,
sender: string,
id?: string,
date?: Date,
): MessageI {
return {
type: MESSAGES_TYPES.TEXT,
component: Message,
text,
sender,
timestamp: new Date(),
timestamp: date ? date : new Date(),
showAvatar: true,
customId: id,
unread: sender === MESSAGE_SENDER.RESPONSE
Expand Down