Skip to content

Commit

Permalink
message data support
Browse files Browse the repository at this point in the history
  • Loading branch information
Krajcik Ondrej committed Oct 25, 2021
1 parent 49a5ba0 commit f78e94f
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 11 deletions.
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
4 changes: 2 additions & 2 deletions src/store/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export function addUserMessage(text: string, id?: string) {
store.dispatch(actions.addUserMessage(text, id));
}

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

0 comments on commit f78e94f

Please sign in to comment.