8
8
< script src ="
https://cdn.jsdelivr.net/npm/[email protected] /dist/jquery.slim.min.js "
crossorigin ="
anonymous "
> </ script >
9
9
< script src ="
https://cdn.jsdelivr.net/npm/[email protected] /dist/js/bootstrap.bundle.min.js "
crossorigin ="
anonymous "
> </ script >
10
10
< script src ="https://pages-github.b-cdn.net/webcomponents/input.js "> </ script >
11
- < script src ="https://cdnjs.cloudflare.com/ajax/libs/gun/0.2020.1240/gun.min.js " integrity ="sha512-DXhMSo4XT1xlS/IsgKbFlUz34Twe8z56oIt+NwVmwmVeN27nXfdZCN35ruF5ZLa8NfTfjugpdLIoFi2DtSyIWA== " crossorigin ="anonymous " referrerpolicy ="no-referrer "> </ script >
12
- < script src ="https://cdnjs.cloudflare.com/ajax/libs/gun/0.2020.1240/axe.js " integrity ="sha512-2IOSn3n0le20q8/BKHvoAPwtcYiAsDh8XumTZvdbjQnh1uD/QBaBdQY20Cx6EckT/+dcT+wveJvyZtljhkjW3g== " crossorigin ="anonymous " referrerpolicy ="no-referrer "> </ script >
13
-
11
+ < script src ="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2 "> </ script >
14
12
</ head >
15
13
< body >
16
14
< div class ="container-fluid " id ="biodata-wrapper ">
@@ -33,7 +31,7 @@ <h1>LiveChat</h1>
33
31
< p class ="m-0 text-primary " style ="font-weight: bold; " id ="visitor-name "> </ p >
34
32
< p style ="font-size: 12px; " id ="visitor-phone "> </ p >
35
33
</ div >
36
- < div id ="chat " style ="height: 300px; " class ="border mb-1 ">
34
+ < div id ="chat " style ="height: 300px;overflow-y: scroll; " class ="border mb-1 ">
37
35
</ div >
38
36
< div id ="input-wrapper " class ="d-flex ">
39
37
< div class ="flex-grow-1 mr-1 ">
@@ -49,7 +47,17 @@ <h1>LiveChat</h1>
49
47
</ div >
50
48
51
49
< script >
52
- const gun = Gun ( "https://ddb.bimasoft.web.id/gun" ) ;
50
+
51
+ const db = supabase . createClient (
52
+ "https://supabase-api.b.app.web.id" ,
53
+ "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ewogICJyb2xlIjogImFub24iLAogICJpc3MiOiAic3VwYWJhc2UiLAogICJpYXQiOiAxNzI4NDg5NjAwLAogICJleHAiOiAxODg2MjU2MDAwCn0.Z0Qi_fL4s-gLFeCkTxp1hTtCHK7Nd26BDj_rNfNc4XU" ,
54
+ ) ;
55
+
56
+ const scrollDiv = ( ) => {
57
+ const chat = document . getElementById ( "chat" ) ;
58
+ chat . scrollTop = chat . scrollHeight ;
59
+ } ;
60
+
53
61
const btn = document . getElementById ( "send-button" ) ;
54
62
const btnBio = document . getElementById ( "save-biodata" ) ;
55
63
@@ -62,15 +70,31 @@ <h1>LiveChat</h1>
62
70
initChat ( ) ;
63
71
}
64
72
65
-
66
73
btn . onclick = ( ) => {
67
- const username = localStorage . getItem ( "nowa" ) ;
74
+ if ( getVal ( "input" ) === "" ) return ;
68
75
76
+ const username = localStorage . getItem ( "nowa" ) ;
69
77
const msg = `Client : ${ getVal ( "input" ) } ` ;
70
- gun . get ( "bimasoft" ) . get ( "livechat" ) . get ( username ) . set ( msg ) ;
78
+
71
79
setVal ( "input" , "" ) ;
80
+ // setfocus
81
+ document . getElementById ( "input-input" ) . focus ( ) ;
82
+
83
+ db . from ( 'livechat' ) . insert ( {
84
+ from : username ,
85
+ msg
86
+ } )
87
+ . then ( ( ) => {
88
+ } )
72
89
} ;
73
90
91
+ document . getElementById ( "input-input" ) . addEventListener ( "keyup" , function ( event ) {
92
+ if ( event . keyCode === 13 ) {
93
+ event . preventDefault ( ) ;
94
+ btn . click ( ) ;
95
+ }
96
+ } ) ;
97
+
74
98
btnBio . onclick = ( ) => {
75
99
const nama = getVal ( "nama" ) ;
76
100
const nowa = getVal ( "nowa" ) ;
@@ -82,27 +106,24 @@ <h1>LiveChat</h1>
82
106
} ;
83
107
84
108
function initChat ( ) {
109
+ console . log ( "CHAT INIT !" ) ;
85
110
const username = localStorage . getItem ( "nowa" ) ;
86
111
const nama = localStorage . getItem ( "nama" ) ;
87
112
88
113
document . getElementById ( "visitor-name" ) . innerText = nama ;
89
114
document . getElementById ( "visitor-phone" ) . innerText = username ;
90
115
91
- gun
92
- . get ( 'bimasoft' )
93
- . get ( 'livechat-ping' )
94
- . put ( username ) ;
95
-
96
- gun
97
- . get ( "bimasoft" )
98
- . get ( "livechat" )
99
- . get ( username )
100
- . on ( chats => {
116
+ db
117
+ . from ( 'livechat' )
118
+ . select ( )
119
+ . eq ( 'from' , username )
120
+ . order ( 'created_at' , { ascending : true } ) . limit ( 100 ) . then ( data => {
121
+ console . log ( data ) ;
101
122
document . getElementById ( "chat" ) . innerHTML = "" ;
102
- for ( const key of Object . keys ( chats ) ) {
123
+ for ( const key of Object . keys ( data . data ) ) {
103
124
if ( key === "_" ) continue ;
104
125
105
- const msg = chats [ key ]
126
+ const msg = data . data [ key ] . msg
106
127
const div = document . createElement ( "div" ) ;
107
128
if ( msg . includes ( "Client : " ) ) {
108
129
div . className = "border ml-2 my-2 bg-secondary text-white py-1 px-3 rounded w-75" ;
@@ -113,7 +134,30 @@ <h1>LiveChat</h1>
113
134
div . innerText = msg . replace ( "Client : " , "" ) . replace ( "Operator : " , "" ) ;
114
135
document . getElementById ( "chat" ) . appendChild ( div ) ;
115
136
}
137
+
138
+ scrollDiv ( ) ;
116
139
} )
140
+
141
+ db . channel ( 'livechat' ) . on ( 'postgres_changes' , {
142
+ schema : 'public' ,
143
+ table : 'livechat' ,
144
+ event : 'INSERT' ,
145
+ filter : `from=eq.${ username } `
146
+ } , ( payload ) => {
147
+ const msg = payload . new . msg ;
148
+ const div = document . createElement ( "div" ) ;
149
+ if ( msg . includes ( "Client : " ) ) {
150
+ div . className = "border ml-2 my-2 bg-secondary text-white py-1 px-3 rounded w-75" ;
151
+ }
152
+ if ( msg . includes ( "Operator : " ) ) {
153
+ div . className = "border my-2 ml-auto mr-2 bg-primary text-white py-1 px-3 rounded w-75" ;
154
+ }
155
+ div . innerText = msg . replace ( "Client : " , "" ) . replace ( "Operator : " , "" ) ;
156
+ document . getElementById ( "chat" ) . appendChild ( div ) ;
157
+
158
+ scrollDiv ( ) ;
159
+
160
+ } ) . subscribe ( ) ;
117
161
}
118
162
</ script >
119
163
</ body >
0 commit comments