-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
894bcba
commit c84fb58
Showing
10 changed files
with
225 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import gql from 'graphql-tag'; | ||
|
||
export const UserFragment = gql` | ||
fragment UserFragment on User { | ||
id | ||
name | ||
} | ||
`; | ||
|
||
export const MessageFragment = gql` | ||
fragment MessageFragment on Message { | ||
id | ||
text | ||
createdAt | ||
sender @type(name: "User") { | ||
...UserFragment | ||
} | ||
recipient @type(name: "User") { | ||
...UserFragment | ||
} | ||
} | ||
${UserFragment} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import gql from 'graphql-tag'; | ||
|
||
import { UserFragment, MessageFragment } from './fragments.graphql'; | ||
|
||
export default gql` | ||
{ | ||
chats @rest(type: "Chat", path: "/chats") { | ||
id @export(as: "chatId") | ||
members @rest(type: "[User]", path: "/chats/:chatId/members") { | ||
...UserFragment | ||
} | ||
recentMessage @type(name: "Message") { | ||
...MessageFragment | ||
} | ||
} | ||
} | ||
${UserFragment} | ||
${MessageFragment} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { of, combineLatest } from 'rxjs'; | ||
import { map, mergeMap } from 'rxjs/operators'; | ||
|
||
function api(path: string) { | ||
return `http://localhost:4000${path}`; | ||
} | ||
|
||
@Injectable() | ||
export class ChatsService { | ||
constructor(private http: HttpClient) {} | ||
|
||
getChats() { | ||
this.fetchChats().pipe( | ||
mergeMap(chats => | ||
combineLatest(chats.map(chat => this.resolveChat(chat))), | ||
), | ||
); | ||
} | ||
|
||
private fetchChats() { | ||
return this.http.get<ChatsResponse>(api('/chats')); | ||
} | ||
|
||
private fetchUser(link: string) { | ||
return this.http.get<UserResponse>(link); | ||
} | ||
|
||
private resolveChat(chat: ChatResponse) { | ||
return combineLatest(chat.members.map(link => this.fetchUser(link))).pipe( | ||
map(members => { | ||
return { | ||
...chat, | ||
messages: [], | ||
members, | ||
}; | ||
}), | ||
); | ||
} | ||
} | ||
|
||
export type Link = string; | ||
export type ID = string; | ||
|
||
export interface ChatResponse { | ||
id: ID; | ||
members: Link[]; | ||
messages: Link; | ||
recentMessage: MessageResponse; | ||
} | ||
|
||
export type ChatsResponse = ChatResponse[]; | ||
|
||
export interface UserResponse { | ||
id: ID; | ||
name: string; | ||
} | ||
|
||
export type ChatMessagesResponse = MessageResponse[]; | ||
|
||
export interface MessageResponse { | ||
id: ID; | ||
text: string; | ||
createdAt: string; | ||
sender: UserResponse; | ||
recipient: UserResponse; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Action } from '@ngrx/store'; | ||
import { Chat, Message, ID } from '../../whatsapp'; | ||
|
||
export enum ActionTypes { | ||
LoadChats = '[Chats] Load', | ||
LoadChatsSuccess = '[Chats] Load Success', | ||
LoadChatsFailure = '[Chats] Load Failure', | ||
} | ||
|
||
// All chats | ||
|
||
export class LoadChats implements Action { | ||
readonly type = ActionTypes.LoadChats; | ||
} | ||
|
||
export class LoadChatsSuccess implements Action { | ||
readonly type = ActionTypes.LoadChatsSuccess; | ||
constructor( | ||
public payload: { | ||
chats: Chat[]; | ||
}, | ||
) {} | ||
} | ||
|
||
export class LoadChatsFailure implements Action { | ||
readonly type = ActionTypes.LoadChatsFailure; | ||
} | ||
|
||
export type ChatAction = LoadChats | LoadChatsSuccess | LoadChatsFailure; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Effect, Actions, ofType } from '@ngrx/effects'; | ||
import { Observable, of, combineLatest } from 'rxjs'; | ||
import { mergeMap, catchError, first, take } from 'rxjs/operators'; | ||
import { Store } from '@ngrx/store'; | ||
import { AppState } from '../app.state'; | ||
import { | ||
ChatAction, | ||
ActionTypes, | ||
LoadChatsSuccess, | ||
LoadChatsFailure, | ||
} from './chat.actions'; | ||
import { User } from '../../whatsapp'; | ||
import { ChatsService } from '../chats.service'; | ||
|
||
@Injectable() | ||
export class ChatEffects { | ||
constructor( | ||
protected actions$: Actions<ChatAction>, | ||
protected store$: Store<AppState>, | ||
protected chats: ChatsService, | ||
) {} | ||
|
||
@Effect() | ||
loadChats$: Observable<ChatAction> = this.actions$.pipe( | ||
ofType(ActionTypes.LoadChats), | ||
mergeMap(_ => { | ||
return this.chats.getChats().pipe( | ||
mergeMap(chats => of(new LoadChatsSuccess({ chats }))), | ||
catchError(() => of(new LoadChatsFailure())), | ||
); | ||
}), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ChatAction, ActionTypes } from './chat.actions'; | ||
import { ChatState } from './chat.state'; | ||
|
||
export const initialState: ChatState = []; | ||
|
||
export function chatReducer( | ||
state = initialState, | ||
action: ChatAction, | ||
): ChatState { | ||
switch (action.type) { | ||
case ActionTypes.LoadChatsSuccess: { | ||
const { chats } = action.payload; | ||
|
||
return chats; | ||
} | ||
|
||
default: | ||
return state; | ||
} | ||
} |