Skip to content

Commit 10439f0

Browse files
authored
Merge pull request #232 from abhinavkrin/new/react-native-project
React Native Project
2 parents fcb9d6c + 2db989d commit 10439f0

File tree

118 files changed

+29465
-6210
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+29465
-6210
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"workspaces": [
77
"packages/*"
88
],
9+
"scripts": {
10+
"postinstall": "cd packages/auth && yarn build && cd ../api && yarn build"
11+
},
912
"devDependencies": {
1013
"esbuild": "^0.17.19",
1114
"lerna": "^6.6.2",

packages/api/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"type": "module",
99
"scripts": {
1010
"test": "echo \"Error: no test specified\" && exit 1",
11-
"refresh-auth": "cd ../auth && yarn build && cd ../api && yarn remove @embeddedchat/auth && yarn add ./packages/auth",
1211
"build": "rollup -c",
1312
"dev": "yarn parcel playground/index.html"
1413
},
@@ -21,8 +20,7 @@
2120
"rollup-plugin-serve": "^2.0.2"
2221
},
2322
"dependencies": {
24-
"@embeddedchat/auth": "./packages/auth",
25-
"@rocket.chat/sdk": "^1.0.0-alpha.42",
26-
"axios": "^1.4.0"
23+
"@embeddedchat/auth": "1.0.0",
24+
"@rocket.chat/sdk": "^1.0.0-alpha.42"
2725
}
2826
}

packages/api/playground/playground.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ async function printResult(result) {
3232
}
3333

3434
const msgListener = msg => {
35+
console.log('Add Message', msg._id);
3536
const idx = messages.findIndex(m => m._id === msg._id);
3637
if (idx === -1) {
3738
messages.push(msg)
@@ -48,6 +49,7 @@ const onConfigChange = async (e) => {
4849
await api.close()
4950
api = new EmbeddedChatApi(host, roomId, { deleteToken, getToken, saveToken });
5051
api.auth.onAuthChange((user) => {
52+
console.log('auth change', !!user)
5153
showAuth(user)
5254
if (user) {
5355
api.connect()
@@ -57,6 +59,8 @@ const onConfigChange = async (e) => {
5759
} else {
5860
api.removeMessageListener(msgListener);
5961
}
62+
63+
console.log(api.auth.authListeners.length);
6064
})
6165
}
6266

@@ -84,8 +88,7 @@ window.addEventListener('DOMContentLoaded', () => {
8488
const host = hostInput.value;
8589
const room = roomInput.value;
8690
api = new EmbeddedChatApi(host, room, { deleteToken, getToken, saveToken });
87-
hostInput.addEventListener("change", onConfigChange)
88-
hostInput.addEventListener("change", onConfigChange)
91+
hostInput.addEventListener("change", onConfigChange);
8992
onConfigChange();
9093

9194
document.getElementById("logoutBtn").addEventListener("click", () => api.auth.logout())

packages/api/src/EmbeddedChatApi.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export default class EmbeddedChatApi {
155155
await this.rcClient.connect({});
156156
const token = (await this.auth.getCurrentUser())?.authToken;
157157
await this.rcClient.resume({ token });
158-
await this.rcClient.subscribe('stream-room-messages', this.rid);
158+
await this.rcClient.subscribeRoom(this.rid);
159159
await this.rcClient.onMessage((data: any) => {
160160
if (!data) {
161161
return;
@@ -174,7 +174,6 @@ export default class EmbeddedChatApi {
174174
'stream-notify-room',
175175
`${this.rid}/user-activity`
176176
);
177-
await this.rcClient.subscribeRoom(this.rid);
178177
await this.rcClient.onStreamData('stream-notify-room', (ddpMessage: any) => {
179178
const [roomId, event] = ddpMessage.fields.eventName.split('/');
180179

packages/auth/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
},
1414
"author": "",
1515
"license": "ISC",
16-
"dependencies": {
17-
"axios": "^1.4.0"
18-
},
1916
"devDependencies": {
2017
"parcel": "^2.9.1",
2118
"rollup": "^3.23.0",

packages/auth/src/Api.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,58 @@
1-
import axios, { AxiosRequestConfig } from "axios";
1+
export class ApiError extends Error {
2+
response: Response;
3+
constructor(
4+
response: Response,
5+
message?: string | undefined,
6+
options?: ErrorOptions | undefined,
7+
...other: any[]
8+
) {
9+
super(message, options, ...other as []);
10+
this.response = response;
11+
}
12+
}
213

314
export class Api {
415
baseUrl: string;
516
constructor(baseUrl: string) {
617
this.baseUrl = baseUrl;
718
}
8-
getAxiosConfig = (config: AxiosRequestConfig) => {
19+
getFetchConfig = (config: RequestInit) => {
920
const headers = {
1021
'Content-Type': 'application/json',
1122
...(config?.headers || {})
1223
}
13-
const axiosConfig: AxiosRequestConfig = {
24+
const requestInit: RequestInit = {
1425
...config,
1526
headers,
16-
baseURL: this.baseUrl
1727
}
18-
return axiosConfig;
28+
return requestInit;
1929
}
20-
async post(endpoint: string, data: any, config: AxiosRequestConfig = {}) {
21-
return axios.post(endpoint, data, this.getAxiosConfig(config));
30+
async request(method: string = 'GET', endpoint: string, data: any, config: RequestInit) {
31+
const url = new URL(endpoint, this.baseUrl).toString();
32+
const response = await fetch(url, {
33+
body: data ? JSON.stringify(data) : undefined,
34+
method,
35+
headers: {
36+
'Content-Type': 'application/json'
37+
}
38+
});
39+
if (!response.ok) {
40+
throw new ApiError(response, "Failed Api Request for "+endpoint);
41+
}
42+
const jsonData = await response.json();
43+
return { data: jsonData };
44+
}
45+
46+
async post(endpoint: string, data: any, config: RequestInit = {}) {
47+
return this.request('POST', endpoint, data, this.getFetchConfig(config));
2248
}
23-
async get(endpoint: string, config: AxiosRequestConfig = {}) {
24-
return axios.get(endpoint, this.getAxiosConfig(config));
49+
async get(endpoint: string, config: RequestInit = {}) {
50+
return this.request('GET', endpoint, null, this.getFetchConfig(config));
2551
}
26-
async put(endpoint: string, data: any, config: AxiosRequestConfig = {}) {
27-
return axios.put(endpoint, data, this.getAxiosConfig(config));
52+
async put(endpoint: string, data: any, config: RequestInit = {}) {
53+
return this.request('PUT', endpoint, data, this.getFetchConfig(config));
2854
}
29-
async delete(endpoint: string, config: AxiosRequestConfig = {}) {
30-
return axios.delete(endpoint, this.getAxiosConfig(config));
55+
async delete(endpoint: string, config: RequestInit = {}) {
56+
return this.request('DELETE', endpoint, null, this.getFetchConfig(config));
3157
}
3258
}

packages/auth/src/RocketChatAuth.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class RocketChatAuth {
3838
callback(user);
3939
}
4040

41+
async removeAuthListener(callback: (user: object | null) => void) {
42+
this.authListeners = this.authListeners.filter( cb => cb !== callback );
43+
}
44+
4145
notifyAuthListeners() {
4246
this.authListeners.forEach(cb => cb(this.currentUser));
4347
}
@@ -154,14 +158,19 @@ class RocketChatAuth {
154158
*/
155159
async load() {
156160
// const {user, lastFetched} = JSON.parse(localStorage.getItem("ec_user") || "{}");
157-
const token = await this.getToken();
158-
const user = await this.loginWithResumeToken(token);
159-
if (user) {
160-
this.lastFetched = new Date();
161-
this.setUser(user);
162-
await this.getCurrentUser(); // refresh the token if needed
161+
try {
162+
const token = await this.getToken();
163+
if (token) {
164+
const user = await this.loginWithResumeToken(token); // will notifyAuthListeners on successful login
165+
if (user) {
166+
this.lastFetched = new Date();
167+
await this.getCurrentUser(); // refresh the token if needed
168+
}
169+
}
170+
} catch (e) {
171+
console.log('Failed to login user on initial load. Sign in.')
172+
this.notifyAuthListeners();
163173
}
164-
this.notifyAuthListeners();
165174
}
166175

167176
/**

packages/htmlembed/dist/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
<body>
1010
<div>
1111
<div id="embeddedchat"></div>
12-
<link rel="stylesheet" href="http://127.0.0.1:4001/rocketchat-icon.css">
1312
<script src="http://127.0.0.1:4001/embeddedchat.js"></script>
1413
<script>
1514
// all props for the EmbeddedChat of @embeddedchat/react will go here
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"parser": "@babel/eslint-parser",
3+
"parserOptions": {
4+
"ecmaFeatures": {
5+
"jsx": true
6+
}
7+
},
8+
"env": {
9+
"browser": true,
10+
"react-native/react-native": true
11+
},
12+
"plugins": ["react", "react-native"],
13+
"extends": ["eslint:recommended", "plugin:react/recommended"],
14+
"rules": {
15+
"react-native/no-unused-styles": 2,
16+
"react-native/split-platform-components": 2,
17+
"react-native/no-raw-text": 2,
18+
"no-undef": "error",
19+
"react/prop-types": "off",
20+
"no-unused-vars": "warn"
21+
}
22+
}

packages/react-native/.gitignore

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
npm-debug.log
8+
yarn-error.log
9+
10+
# testing
11+
/coverage
12+
13+
# production
14+
/build
15+
16+
# misc
17+
.DS_Store
18+
.env.local
19+
.env.development.local
20+
.env.test.local
21+
.env.production.local
22+
.eslintcache
23+
24+
npm-debug.log*
25+
yarn-debug.log*
26+
yarn-error.log*
27+
28+
# OSX
29+
#
30+
.DS_Store
31+
32+
# Xcode
33+
#
34+
build/
35+
*.pbxuser
36+
!default.pbxuser
37+
*.mode1v3
38+
!default.mode1v3
39+
*.mode2v3
40+
!default.mode2v3
41+
*.perspectivev3
42+
!default.perspectivev3
43+
xcuserdata
44+
*.xccheckout
45+
*.moved-aside
46+
DerivedData
47+
*.hmap
48+
*.ipa
49+
*.xcuserstate
50+
project.xcworkspace
51+
52+
# Android/IntelliJ
53+
#
54+
build/
55+
.idea
56+
.gradle
57+
local.properties
58+
*.iml
59+
*.hprof
60+
61+
# BUCK
62+
buck-out/
63+
\.buckd/
64+
*.keystore
65+
66+
# fastlane
67+
#
68+
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
69+
# screenshots whenever they are needed.
70+
# For more information about the recommended setup visit:
71+
# https://docs.fastlane.tools/best-practices/source-control/
72+
73+
*/fastlane/report.xml
74+
*/fastlane/Preview.html
75+
*/fastlane/screenshots
76+
77+
# Bundle artifacts
78+
*.jsbundle
79+
80+
# CocoaPods
81+
/ios/Pods/
82+
83+
# Expo
84+
.expo/*
85+
web-build/
86+

0 commit comments

Comments
 (0)