2323 </div >
2424 <div class =" mt-8" >
2525 <div v-if =" messages.length" >
26- <h1 class =" text-xl md:text-base text-gray-matcha text-center font-bold" >Messages</h1 >
27- <div ></div >
26+ <h1 class =" text-xl md:text-base text-gray-matcha text-left font-bold" >Messages</h1 >
27+ <div class =" overflow-scroll mt-4" >
28+ <Message v-on:chat =" chat" v-for =" message in messages" :key =" message.with_user.id" v-bind:message =" message" ></Message >
29+ </div >
2830 </div >
2931 <div v-else class =" flex items-center" >
3032 <div >
4244 v-if =" chatWithUserId"
4345 v-bind:chatWithUserId =" chatWithUserId"
4446 v-on:close-chat =" closeChat()" ></Chat >
45- <div v-if =" !chatWithUserId" class =" pl-8 w-full text-left max-w-2xl invisible md:visible md:relative md:ml-4" >
47+ <div v-if =" !chatWithUserId" class =" text-center pl-8 py-8 w-full max-w-2xl invisible md:visible md:relative md:ml-4" >
4648 <div v-if =" !matches.length && !messages.length" >
4749 <h1 class =" text-4xl text-gray-400 font-bold" >Use Browse, Search</h1 >
4850 <h1 class =" text-4xl text-gray-400 font-bold" >to like people</h1 >
6567import NavBar from ' @/components/shared/NavBar.vue' ;
6668import Match from ' @/components/app/matches/Match.vue' ;
6769import Chat from ' @/components/app/matches/Chat.vue' ;
70+ import Message from ' @/components/app/matches/Message.vue' ;
6871
6972export default {
7073 name: ' Matches' ,
7174 components: {
7275 Chat,
7376 NavBar,
7477 Match,
78+ Message,
7579 },
7680 data : () => ({
7781 matches: [],
@@ -84,18 +88,17 @@ export default {
8488 const matchesRequest = await this .$http .get (' /matches' );
8589 const { matches } = matchesRequest .data ;
8690 for (let i = 0 ; i < matches .length ; i += 1 ) {
87- const userRequest = await this .$http .get (` /users/${ matches[i].user_1 } ` );
88- this .matches .push (userRequest .data );
91+ for (let j = 0 ; j < this .messages .length ; j += 1 ) {
92+ if (this .messages [j].length && this .messages [j][0 ].to_id !== matches[i].user_1 ) {
93+ const userRequest = await this .$http .get (` /users/${ matches[i].user_1 } ` );
94+ this .matches .push (userRequest .data );
95+ }
96+ }
8997 }
9098 },
9199 async fetchMessages () {
92100 const messagesRequest = await this .$http .get (' /conversations' );
93- const messages = messagesRequest .data ;
94- for (let i = 0 ; i <= messages .length ; i += 1 ) {
95- const userChattingWith = await this .$http .get (` /users/${ messages[i].with_user } ` );
96- messages[i].user = userChattingWith .data ;
97- }
98- this .messages = messages;
101+ this .messages = messagesRequest .data .conversations ;
99102 },
100103 async chat (... args ) {
101104 const [id ] = args;
@@ -106,11 +109,35 @@ export default {
106109 closeChat () {
107110 this .chatWithUserId = null ;
108111 },
112+ openMessageOnMd (e ) {
113+ if (e .target .innerWidth >= 768 && ! this .chatWithUserId ) {
114+ if (this .messages .length && e .target .innerWidth >= 768 ) {
115+ this .chatWithUserId = this .messages [0 ].with_user .id ;
116+ }
117+ }
118+ },
119+ async fetchData () {
120+ window .addEventListener (' resize' , this .openMessageOnMd );
121+ await this .fetchMessages ();
122+ await this .fetchMatches ();
123+ if (window .innerWidth >= 768 && this .messages .length ) {
124+ this .chatWithUserId = this .messages [0 ].with_user .id ;
125+ }
126+ this .fetchingDone = true ;
127+ },
109128 },
110129 async beforeMount () {
111- await this .fetchMatches ();
112- await this .fetchMessages ();
113- this .fetchingDone = true ;
130+ await this .fetchData ();
131+ },
132+ deactivated () {
133+ if (! this .$route .path .startsWith (' /users' )) {
134+ this .matches = [];
135+ this .messages = [];
136+ this .chatWithUserId = null ;
137+ this .fetchingDone = false ;
138+ this .fetchData ();
139+ this .$el .scrollTop = 0 ;
140+ }
114141 },
115142};
116143 </script >
0 commit comments