From 789712a47a2a4c429d1fb776ab5443d10f2b4889 Mon Sep 17 00:00:00 2001 From: Divyansh Anand <57003965+divyansh0908@users.noreply.github.com> Date: Sun, 29 Sep 2024 15:09:40 +0530 Subject: [PATCH 01/14] refactor: Remove the unused props --- src/components/Notify/NotifyForm.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/components/Notify/NotifyForm.tsx b/src/components/Notify/NotifyForm.tsx index a2f55221..c4735caf 100644 --- a/src/components/Notify/NotifyForm.tsx +++ b/src/components/Notify/NotifyForm.tsx @@ -1,3 +1,4 @@ +import React, { useContext, useEffect, useState } from 'react'; import { View, Text, @@ -8,8 +9,6 @@ import { Image, Keyboard, } from 'react-native'; -import React, { useContext, useEffect, useState } from 'react'; -import { Picker } from '@react-native-picker/picker'; import Colors from '../../constants/colors/Colors'; import StyleConfig from '../../utils/StyleConfig'; import { scale } from '../../utils/utils'; @@ -21,7 +20,7 @@ import { import { AuthContext } from '../../context/AuthContext'; import { firebase } from '@react-native-firebase/messaging'; -const NotifyForm = ({ notifyHandler }: { notifyHandler: () => void }) => { +const NotifyForm = () => { const [title, setTitle] = useState(''); const [description, setDescription] = useState(''); const [isDropDownSelected, setIsDropDownSelected] = useState(false); @@ -58,6 +57,7 @@ const NotifyForm = ({ notifyHandler }: { notifyHandler: () => void }) => { }); await sendNotification(title, description, selectedUser?.id, token); }; + useEffect(() => { const fetchData = async () => { const allUser = await getAllUsers(loggedInUserData?.token); @@ -67,6 +67,7 @@ const NotifyForm = ({ notifyHandler }: { notifyHandler: () => void }) => { fetchData(); getFCMToken(); }, [loggedInUserData?.token]); + return ( Title: From 73da022f0a78e35d7253a4629f15f4ea4548989b Mon Sep 17 00:00:00 2001 From: Divyansh Anand <57003965+divyansh0908@users.noreply.github.com> Date: Sun, 29 Sep 2024 15:11:25 +0530 Subject: [PATCH 02/14] style: Give some style to notification's title --- src/screens/NotifyScreen/NotifyScreen.tsx | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/screens/NotifyScreen/NotifyScreen.tsx b/src/screens/NotifyScreen/NotifyScreen.tsx index 2c7e77a3..8f4d3e10 100644 --- a/src/screens/NotifyScreen/NotifyScreen.tsx +++ b/src/screens/NotifyScreen/NotifyScreen.tsx @@ -1,4 +1,4 @@ -import { StyleSheet, View } from 'react-native'; +import { StyleSheet, Text, View } from 'react-native'; import React, { useContext } from 'react'; import LocalNotification from '../../actions/LocalNotification'; import { firebase } from '@react-native-firebase/messaging'; @@ -22,7 +22,8 @@ const NotifyScreen = () => { }; return ( - + Event Notifications + ); }; @@ -32,4 +33,16 @@ const styles = StyleSheet.create({ container: { padding: 16, }, + title: { + fontSize: 24, + fontWeight: 'bold', + color: '#333', + marginBottom: 20, + textAlign: 'center', + textTransform: 'uppercase', + letterSpacing: 1, + borderBottomWidth: 2, + borderBottomColor: Colors.Primary_Color, + paddingBottom: 10, + }, }); From dfef74628905dcc637a9ceec1e642af37249e9af Mon Sep 17 00:00:00 2001 From: Divyansh Anand <57003965+divyansh0908@users.noreply.github.com> Date: Sun, 29 Sep 2024 17:43:02 +0530 Subject: [PATCH 03/14] feat: Add env template --- .env.template | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .env.template diff --git a/.env.template b/.env.template new file mode 100644 index 00000000..b592d4e8 --- /dev/null +++ b/.env.template @@ -0,0 +1,9 @@ +BASE_UTL= +RDS_SESSION= +FIREBASE_PROJECT_ID= +FIREBASE_MESSAGING_SENDER_ID= +FIREBASE_STORAGE_BUCKET= +MOBILE_SDK_APP_ID= +OAUTH_CLIENT_ID= +FIREBASE_CURRENT_API_KEY= +OTHER_PLATFORM_OAUTH_CLIENT_ID= From a2bc9ea28e33d11b92012cde751c03a5548adcf9 Mon Sep 17 00:00:00 2001 From: Divyansh Anand <57003965+divyansh0908@users.noreply.github.com> Date: Sun, 29 Sep 2024 17:43:31 +0530 Subject: [PATCH 04/14] fix: Remove git tracking of google-services.json --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index d8350ee8..658a4925 100644 --- a/.gitignore +++ b/.gitignore @@ -61,6 +61,9 @@ buck-out/ # CocoaPods /ios/Pods/ +# ignore google-services +/android/app/google-services.json + #env From 8a0876c64ee359055ba86695fb8599f680336750 Mon Sep 17 00:00:00 2001 From: Divyansh Anand <57003965+divyansh0908@users.noreply.github.com> Date: Sun, 29 Sep 2024 17:45:09 +0530 Subject: [PATCH 05/14] feat: Add postinstall script to generate google-services.json --- bin/postInstall | 12 ++++++ package.json | 2 + src/service/googleServiceTemplate.js | 63 ++++++++++++++++++++++++++++ yarn.lock | 41 ++++++++++++++---- 4 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 bin/postInstall create mode 100644 src/service/googleServiceTemplate.js diff --git a/bin/postInstall b/bin/postInstall new file mode 100644 index 00000000..61078b1c --- /dev/null +++ b/bin/postInstall @@ -0,0 +1,12 @@ +#!usr/bin/env node +run('node ./src/service/googleServiceTemplate.js'); + +function run(command) { + console.info(`./bin/postInstall scripit running: ${command}`); + try { + require('child_process').execSync(command, { stdio: 'inherit' }); + } catch (err) { + console.error(`./bin/postInstall failed on command ${command}`); + process.exit(err.status); + } +} diff --git a/package.json b/package.json index 96da648d..03107d81 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "build-assets-folder": "cd android/app/src/main && if [ -d 'assets' ]; then rm -r assets; fi", "build": "mkdir -p android/app/src/main/assets && npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res && cd android && ./gradlew assembleDebug", "build-release": "mkdir -p android/app/src/main/assets && npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/build/intermediates/res/merged/release/ && rm -rf android/app/src/main/res/drawable-* && rm -rf android/app/src/main/res/raw/* && cd android && ./gradlew bundleRelease" + "postinstall": "node ./bin/postInstall" }, "dependencies": { "@babel/helper-hoist-variables": "^7.24.7", @@ -31,6 +32,7 @@ "@react-navigation/native-stack": "^6.9.12", "@react-navigation/stack": "^6.2.0", "axios": "^0.26.0", + "dotenv": "^16.4.5", "eslint-plugin-prettier": "^4.1.0", "moment": "^2.29.4", "react": "18.2.0", diff --git a/src/service/googleServiceTemplate.js b/src/service/googleServiceTemplate.js new file mode 100644 index 00000000..f5caba27 --- /dev/null +++ b/src/service/googleServiceTemplate.js @@ -0,0 +1,63 @@ +const fs = require('fs'); +const path = require('path'); +require('dotenv').config(); + +const googleServicesTemplate = { + project_info: { + project_id: process.env.FIREBASE_PROJECT_ID, + project_number: process.env.FIREBASE_MESSAGING_SENDER_ID, + storage_bucket: process.env.FIREBASE_STORAGE_BUCKET, + }, + client: [ + { + client_info: { + mobilesdk_app_id: process.env.MOBILE_SDK_APP_ID, + android_client_info: { + package_name: 'com.rdsapp', + }, + }, + oauth_client: [ + { + client_id: process.env.OAUTH_CLIENT_ID, + client_type: 3, + }, + ], + api_key: [ + { + current_key: process.env.FIREBASE_CURRENT_API_KEY, + }, + ], + services: { + appinvite_service: { + other_platform_oauth_client: [ + { + client_id: process.env.OTHER_PLATFORM_OAUTH_CLIENT_ID, + client_type: 3, + }, + ], + }, + }, + }, + ], + configuration_version: '1', +}; + +console.log('🥲🥲🥲🥲🥲', googleServicesTemplate); +// Write the file to the correct location +const outputPath = path.join( + __dirname, + '../..', + 'android', + 'app', + 'google-services.json', +); + +// Write the google-services.json file +if (fs.existsSync(outputPath)) { + console.log(`File ${outputPath} already exists. Overwriting...`); +} else { + console.log(`File ${outputPath} does not exist. Creating a new file...`); +} +fs.writeFileSync(outputPath, JSON.stringify(googleServicesTemplate, null, 2)); + +console.log('google-services.json has been generated'); diff --git a/yarn.lock b/yarn.lock index 587e83c3..c42cf7f0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2458,18 +2458,18 @@ resolved "https://registry.npmjs.org/@react-native-community/slider/-/slider-4.5.0.tgz" integrity sha512-pyUvNTvu5IfCI5abzqRfO/dd3A009RC66RXZE6t0gyOwI/j0QDlq9VZRv3rjkpuIvNTnsYj+m5BHlh0DkSYUyA== -"@react-native-firebase/app@^20.4.0": - version "20.4.0" - resolved "https://registry.yarnpkg.com/@react-native-firebase/app/-/app-20.4.0.tgz#a9c5c12505432d42f7d4a0c4265b8684dc8353fc" - integrity sha512-I3YswH5tq0kSezyFwyV0d3J+lnH+N/SdznFZ70Lr671X0YlrbEBb6BK7FzEiwq8rKeIPh+pbgnbYwzj/k9uEnQ== +"@react-native-firebase/app@20.5.0": + version "20.5.0" + resolved "https://registry.yarnpkg.com/@react-native-firebase/app/-/app-20.5.0.tgz#9dc2fbc6eee7fb00eefc9f88e404411068befd29" + integrity sha512-zW6jc9R8Ikxxo5qXv25No6QxdWOx37D4U0ppKZQoY6CzwGUzqc4oQUU9MclbUcx+H5p+GEzvT1EIGV6JG7/FAg== dependencies: firebase "10.12.2" superstruct "^0.6.2" -"@react-native-firebase/messaging@^20.4.0": - version "20.4.0" - resolved "https://registry.yarnpkg.com/@react-native-firebase/messaging/-/messaging-20.4.0.tgz#4f616bd5df0bf3feffc768968bec763ed2f702eb" - integrity sha512-Ju0rQLoJIzur+QQSQ3O2q/7aBnJa9dyT7kY1tt52vRJ38Bm3/W1NKcYPbbfqMV+N6Nelonlxs1/kaQmo/p+/AA== +"@react-native-firebase/messaging@20.5.0": + version "20.5.0" + resolved "https://registry.yarnpkg.com/@react-native-firebase/messaging/-/messaging-20.5.0.tgz#90eeed62d1a069b4fd56746afcfb33ee2a61ba0f" + integrity sha512-S3M9zHJ3zKRH6PuxxjOrV9i0uRO3S5rGYaY3s64BTh4iLEG46UDgJ5uA+WsY+9IsJtSpN4HYDcvnwoxasiEUfw== "@react-native-masked-view/masked-view@^0.2.6": version "0.2.9" @@ -3018,6 +3018,14 @@ resolved "https://registry.npmjs.org/@types/hammerjs/-/hammerjs-2.0.41.tgz" integrity sha512-ewXv/ceBaJprikMcxCmWU1FKyMAQ2X7a9Gtmzw8fcg2kIePI1crERDM818W+XYrxqdBBOdlf2rm137bU+BltCA== +"@types/hoist-non-react-statics@^3.3.0": + version "3.3.5" + resolved "https://registry.yarnpkg.com/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.5.tgz#dab7867ef789d87e2b4b0003c9d65c49cc44a494" + integrity sha512-SbcrWzkKBw2cdwRTwQAswfpB9g9LJWfjtUeW/jvNwbhC8cpmmNYVePa+ncbUe0rGTQ7G3Ff6mYUN2VMfLVr+Sg== + dependencies: + "@types/react" "*" + hoist-non-react-statics "^3.3.0" + "@types/hoist-non-react-statics@^3.3.1": version "3.3.1" resolved "https://registry.npmjs.org/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz" @@ -3118,6 +3126,16 @@ dependencies: "@types/react" "^17" +"@types/react-redux@^7.1.33": + version "7.1.33" + resolved "https://registry.yarnpkg.com/@types/react-redux/-/react-redux-7.1.33.tgz#53c5564f03f1ded90904e3c90f77e4bd4dc20b15" + integrity sha512-NF8m5AjWCkert+fosDsN3hAlHzpjSiXlVy9EgQEmLoBhaNXbmyeGs/aj5dQzKuF+/q+S7JQagorGDW8pJ28Hmg== + dependencies: + "@types/hoist-non-react-statics" "^3.3.0" + "@types/react" "*" + hoist-non-react-statics "^3.3.0" + redux "^4.0.0" + "@types/react-test-renderer@^18.0.0": version "18.3.0" resolved "https://registry.npmjs.org/@types/react-test-renderer/-/react-test-renderer-18.3.0.tgz" @@ -4547,6 +4565,11 @@ dot-case@^3.0.4: no-case "^3.0.4" tslib "^2.0.3" +dotenv@^16.4.5: + version "16.4.5" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.5.tgz#cdd3b3b604cb327e286b4762e13502f717cb099f" + integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg== + ee-first@1.1.1: version "1.1.1" resolved "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz" @@ -8154,7 +8177,7 @@ redux-saga@^1.2.3: dependencies: "@redux-saga/core" "^1.2.3" -redux@^4.0.4, redux@^4.2.1: +redux@^4.0.0, redux@^4.0.4, redux@^4.2.1: version "4.2.1" resolved "https://registry.npmjs.org/redux/-/redux-4.2.1.tgz" integrity sha512-LAUYz4lc+Do8/g7aeRa8JkyDErK6ekstQaqWQrNRW//MY1TvCEpMtpTWvlQ+FPbWCx+Xixu/6SHt5N0HR+SB4w== From 7f730fd3b5328b3c54d4d203b7ef84a592bc618f Mon Sep 17 00:00:00 2001 From: Divyansh Anand <57003965+divyansh0908@users.noreply.github.com> Date: Sun, 29 Sep 2024 17:54:00 +0530 Subject: [PATCH 06/14] refactor: Remove google-services.json --- android/app/google-services.json | 130 ------------------------------- 1 file changed, 130 deletions(-) delete mode 100644 android/app/google-services.json diff --git a/android/app/google-services.json b/android/app/google-services.json deleted file mode 100644 index f6ec121c..00000000 --- a/android/app/google-services.json +++ /dev/null @@ -1,130 +0,0 @@ -{ - "project_info": { - // "project_number": "260594870079", - // "firebase_url": "https://rds-backend-86606-default-rtdb.firebaseio.com", - // "project_id": "rds-backend-86606", - // "storage_bucket": "rds-backend-86606.appspot.com" - "project_number": "500338429689", - "project_id": "realdevsquad-10012", - "storage_bucket": "realdevsquad-10012.appspot.com" - }, - "client": [ - { - "client_info": { - "mobilesdk_app_id": "1:260594870079:android:96f715af50676f7a53958a", - "android_client_info": { - "package_name": "com.notifyapp" - } - }, - "oauth_client": [ - { - "client_id": "260594870079-jk3ak4lbadk26m6hlh16gi1rk4kd7nmb.apps.googleusercontent.com", - "client_type": 3 - } - ], - "api_key": [ - { - "current_key": "AIzaSyBB8iJ4Oi-WD9Ps3tBIipoy9yL4sgesh24" - } - ], - "services": { - "appinvite_service": { - "other_platform_oauth_client": [ - { - "client_id": "260594870079-jk3ak4lbadk26m6hlh16gi1rk4kd7nmb.apps.googleusercontent.com", - "client_type": 3 - } - ] - } - } - }, - { - "client_info": { - "mobilesdk_app_id": "1:260594870079:android:0d0291fc2c87387053958a", - "android_client_info": { - "package_name": "com.rdsapp" - } - }, - "oauth_client": [ - { - "client_id": "260594870079-jk3ak4lbadk26m6hlh16gi1rk4kd7nmb.apps.googleusercontent.com", - "client_type": 3 - } - ], - "api_key": [ - { - "current_key": "AIzaSyBB8iJ4Oi-WD9Ps3tBIipoy9yL4sgesh24" - } - ], - "services": { - "appinvite_service": { - "other_platform_oauth_client": [ - { - "client_id": "260594870079-jk3ak4lbadk26m6hlh16gi1rk4kd7nmb.apps.googleusercontent.com", - "client_type": 3 - } - ] - } - } - }, - { - "client_info": { - "mobilesdk_app_id": "1:260594870079:android:bdf1b672dd0f763b53958a", - "android_client_info": { - "package_name": "com.rdsappp" - } - }, - "oauth_client": [ - { - "client_id": "260594870079-jk3ak4lbadk26m6hlh16gi1rk4kd7nmb.apps.googleusercontent.com", - "client_type": 3 - } - ], - "api_key": [ - { - "current_key": "AIzaSyBB8iJ4Oi-WD9Ps3tBIipoy9yL4sgesh24" - } - ], - "services": { - "appinvite_service": { - "other_platform_oauth_client": [ - { - "client_id": "260594870079-jk3ak4lbadk26m6hlh16gi1rk4kd7nmb.apps.googleusercontent.com", - "client_type": 3 - } - ] - } - } - }, - { - "client_info": { - "mobilesdk_app_id": "1:500338429689:android:3466042d6816e27fbcdc28", - "android_client_info": { - "package_name": "com.rdsapp" - } - }, - "oauth_client": [ - { - "client_id": "500338429689-bs60eaons6m17p788ka6tkffqthk5arl.apps.googleusercontent.com", - "client_type": 3 - } - ], - "api_key": [ - { - "current_key": "AIzaSyCCk2qqhDfeEm7_QWehc-r3H6me6_T0C5s" - } - ], - "services": { - "appinvite_service": { - "other_platform_oauth_client": [ - { - "client_id": "500338429689-bs60eaons6m17p788ka6tkffqthk5arl.apps.googleusercontent.com", - "client_type": 3 - } - ] - } - } - } - ], - "configuration_version": "1" -} From 63d6e169b67451459d73ed575a2662efb5f83c6f Mon Sep 17 00:00:00 2001 From: Divyansh Anand <57003965+divyansh0908@users.noreply.github.com> Date: Sun, 29 Sep 2024 20:44:44 +0530 Subject: [PATCH 07/14] fix: Remove unused code from `NotificationScreen` --- src/screens/NotifyScreen/NotifyScreen.tsx | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/src/screens/NotifyScreen/NotifyScreen.tsx b/src/screens/NotifyScreen/NotifyScreen.tsx index 8f4d3e10..640da460 100644 --- a/src/screens/NotifyScreen/NotifyScreen.tsx +++ b/src/screens/NotifyScreen/NotifyScreen.tsx @@ -1,25 +1,10 @@ +import React from 'react'; import { StyleSheet, Text, View } from 'react-native'; -import React, { useContext } from 'react'; -import LocalNotification from '../../actions/LocalNotification'; -import { firebase } from '@react-native-firebase/messaging'; -import { AuthContext } from '../../context/AuthContext'; -import { postFcmToken } from '../AuthScreen/Util'; import NotifyForm from '../../components/Notify/NotifyForm'; +import Colors from '../../constants/colors/Colors'; const NotifyScreen = () => { - const { loggedInUserData } = useContext(AuthContext); - const notifyHandler = () => { - LocalNotification(); - getFCMToken(); - }; - const getFCMToken = async () => { - const fcmToken_ = await firebase.messaging().getToken(); - console.log('🚀 ~ getFCMToken ~ fcmToken_:', fcmToken_); - const token = loggedInUserData?.token; - - await postFcmToken(fcmToken_, token); - }; return ( Event Notifications From 0fb6d0afe05ab610c1c80d9ba00bf04e169eb7e6 Mon Sep 17 00:00:00 2001 From: Divyansh Anand <57003965+divyansh0908@users.noreply.github.com> Date: Sun, 29 Sep 2024 20:45:04 +0530 Subject: [PATCH 08/14] fix: Write test according to the modified code --- __tests__/screens/NotifyScreen.test.tsx | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/__tests__/screens/NotifyScreen.test.tsx b/__tests__/screens/NotifyScreen.test.tsx index 2afddd28..d1dcf07c 100644 --- a/__tests__/screens/NotifyScreen.test.tsx +++ b/__tests__/screens/NotifyScreen.test.tsx @@ -1,6 +1,7 @@ import React from 'react'; import { render, waitFor } from '@testing-library/react-native'; import NotifyScreen from '../../src/screens/NotifyScreen/NotifyScreen'; +import NotifyForm from '../../src/components/Notify/NotifyForm'; import { postFcmToken } from '../../src/screens/AuthScreen/Util'; import { firebase } from '@react-native-firebase/messaging'; @@ -8,26 +9,19 @@ jest.mock('@react-native-firebase/messaging', () => ({ firebase: { messaging: jest.fn(() => ({ getToken: jest.fn(() => Promise.resolve('mocked-fcm-token')), + requestPermission: jest.fn(() => Promise.resolve()), })), }, })); jest.mock('../../src/screens/AuthScreen/Util', () => ({ postFcmToken: jest.fn(), + getAllUsers: jest.fn(() => Promise.resolve([])), })); describe('NotifyScreen', () => { it('should render correctly with title and NotifyForm', async () => { const { getByText } = render(); expect(getByText('Event Notifications')).toBeTruthy(); - - await waitFor(() => { - expect(firebase.messaging().getToken).toHaveBeenCalled(); - }); - await waitFor(() => - expect(postFcmToken).toHaveBeenCalledWith( - 'mocked-fcm-token', - 'user-token', - ), - ); + // Wait for the getToken to be called }); }); From 179b039708c44686f554f7dc24cae1b79d53ba25 Mon Sep 17 00:00:00 2001 From: Divyansh Anand <57003965+divyansh0908@users.noreply.github.com> Date: Sun, 29 Sep 2024 20:47:05 +0530 Subject: [PATCH 09/14] feat: Update notify form to request for notification --- src/components/Notify/NotifyForm.tsx | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/components/Notify/NotifyForm.tsx b/src/components/Notify/NotifyForm.tsx index c4735caf..33c1abf7 100644 --- a/src/components/Notify/NotifyForm.tsx +++ b/src/components/Notify/NotifyForm.tsx @@ -42,14 +42,19 @@ const NotifyForm = () => { }; const getFCMToken = async () => { - const fcmToken_ = await firebase.messaging().getToken(); - console.log('🚀 ~ getFCMToken ~ fcmToken_:', fcmToken_); + const permission = await firebase.messaging().hasPermission(); + console.log('nfdeobns', permission); + if (permission) { + const fcmToken_ = await firebase.messaging().getToken(); + console.log('🚀 ~ getFCMToken ~ fcmToken_:', fcmToken_); - await postFcmToken(fcmToken_, token); + await postFcmToken(fcmToken_, token); + } else { + await firebase.messaging().requestPermission(); + } }; const handleButtonPress = async () => { // Handle the button press and perform necessary actions (e.g., send notification) - console.log('setSelected User', { title, description, @@ -66,7 +71,7 @@ const NotifyForm = () => { }; fetchData(); getFCMToken(); - }, [loggedInUserData?.token]); + }, []); return ( From b30102a2c41369df3cd0525f1bc74549e7e0cbb5 Mon Sep 17 00:00:00 2001 From: Divyansh Anand <57003965+divyansh0908@users.noreply.github.com> Date: Sun, 29 Sep 2024 20:47:22 +0530 Subject: [PATCH 10/14] feat: Add test for notification form component --- .../components/NotificationForm.test.tsx | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 __tests__/Goals/components/NotificationForm.test.tsx diff --git a/__tests__/Goals/components/NotificationForm.test.tsx b/__tests__/Goals/components/NotificationForm.test.tsx new file mode 100644 index 00000000..d3ca73b0 --- /dev/null +++ b/__tests__/Goals/components/NotificationForm.test.tsx @@ -0,0 +1,133 @@ +import React from 'react'; +import { render, fireEvent, waitFor } from '@testing-library/react-native'; +import NotifyForm from '../../../src/components/Notify/NotifyForm'; +import { AuthContext } from '../../../src/context/AuthContext'; +import { + postFcmToken, + sendNotification, + getAllUsers, +} from '../../../src/screens/AuthScreen/Util'; +import { firebase } from '@react-native-firebase/messaging'; + +// Mock the functions used in the component +jest.mock('../../../src/screens/AuthScreen/Util', () => ({ + postFcmToken: jest.fn(), + sendNotification: jest.fn(), + getAllUsers: jest.fn(() => Promise.resolve([])), // Mock getAllUsers with an empty array +})); + +jest.mock('@react-native-firebase/messaging', () => ({ + firebase: { + messaging: jest.fn(() => ({ + getToken: jest.fn(() => Promise.resolve('mocked-fcm-token')), + hasPermission: jest.fn(() => Promise.resolve(1)), // Mock permission granted + requestPermission: jest.fn(() => Promise.resolve()), // Mock permission request + })), + }, +})); + +describe('NotifyForm', () => { + const loggedInUserData = { token: 'user-token' }; + + const renderComponent = () => { + return render( + + + , + ); + }; + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('renders the form with title, description, and Notify button', () => { + const { getByText, getByPlaceholderText } = renderComponent(); + + expect(getByText('Title:')).toBeTruthy(); + expect(getByText('Description:')).toBeTruthy(); + expect(getByText('Notify To:')).toBeTruthy(); + expect(getByPlaceholderText('Enter title')).toBeTruthy(); + expect(getByPlaceholderText('Enter description')).toBeTruthy(); + expect(getByText('Notify')).toBeTruthy(); + }); + + it('retrieves FCM token and calls postFcmToken', async () => { + renderComponent(); + + await waitFor(() => { + expect(firebase.messaging().hasPermission).toHaveBeenCalled(); + expect(firebase.messaging().getToken).toHaveBeenCalled(); + expect(postFcmToken).toHaveBeenCalledWith( + 'mocked-fcm-token', + 'user-token', + ); + }); + }); + + it('fetches users and updates the dropdown', async () => { + const mockUsers = [ + { id: '1', username: 'john_doe', first_name: 'John', last_name: 'Doe' }, + { id: '2', username: 'jane_doe', first_name: 'Jane', last_name: 'Doe' }, + ]; + + getAllUsers.mockResolvedValue(mockUsers); // Mock resolved users + + const { getByTestId, getByText } = renderComponent(); + + // Wait for users to load + await waitFor(() => { + expect(getAllUsers).toHaveBeenCalledWith('user-token'); + }); + + const dropdown = getByTestId('dropdown'); + fireEvent.press(dropdown); // Simulate dropdown press to show user list + + await waitFor(() => { + expect(getByText('john_doe')).toBeTruthy(); + expect(getByText('jane_doe')).toBeTruthy(); + }); + }); + + it('selects a user from the dropdown and sends a notification', async () => { + const mockUsers = [ + { id: '1', username: 'john_doe', first_name: 'John', last_name: 'Doe' }, + ]; + + getAllUsers.mockResolvedValue(mockUsers); + + const { getByTestId, getByPlaceholderText, getByText } = renderComponent(); + + // Wait for users to load + await waitFor(() => { + expect(getAllUsers).toHaveBeenCalledWith('user-token'); + }); + + const dropdown = getByTestId('dropdown'); + fireEvent.press(dropdown); // Open dropdown + + // Select a user from the dropdown + await waitFor(() => { + fireEvent.press(getByText('john_doe')); + }); + + // Fill in title and description + fireEvent.changeText(getByPlaceholderText('Enter title'), 'Test Title'); + fireEvent.changeText( + getByPlaceholderText('Enter description'), + 'Test Description', + ); + + // Press Notify button + fireEvent.press(getByText('Notify')); + + await waitFor(() => { + expect(sendNotification).toHaveBeenCalledWith( + 'Test Title', + 'Test Description', + '1', + 'user-token', + ); + }); + }); +}); From 3b806be7730ad3d7bd38ca620eec18a64d4cbacc Mon Sep 17 00:00:00 2001 From: Divyansh Anand <57003965+divyansh0908@users.noreply.github.com> Date: Sun, 29 Sep 2024 20:48:04 +0530 Subject: [PATCH 11/14] feat: Add ios package for firebase notification --- package.json | 1 + yarn.lock | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/package.json b/package.json index 03107d81..57d4db8c 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "@babel/helper-hoist-variables": "^7.24.7", "@react-native-async-storage/async-storage": "^1.15.16", "@react-native-community/netinfo": "^5.9.9", + "@react-native-community/push-notification-ios": "^1.11.0", "@react-native-community/slider": "^4.4.3", "@react-native-firebase/app": "^20.4.0", "@react-native-firebase/messaging": "^20.4.0", diff --git a/yarn.lock b/yarn.lock index c42cf7f0..f29304f5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2453,6 +2453,13 @@ resolved "https://registry.npmjs.org/@react-native-community/netinfo/-/netinfo-5.9.9.tgz" integrity sha512-Gp0XV4BgabvzkL4Dp6JAsA2l9LcmgBAq3erCLdvRZmEFz7guCWTogQWVfFtl+IbU0uqfwfo9fm2+mQiwdudLCw== +"@react-native-community/push-notification-ios@^1.11.0": + version "1.11.0" + resolved "https://registry.yarnpkg.com/@react-native-community/push-notification-ios/-/push-notification-ios-1.11.0.tgz#d8ec4acfb52260cb779ed0379b9e197db7841b83" + integrity sha512-nfkUs8P2FeydOCR4r7BNmtGxAxI22YuGP6RmqWt6c8EEMUpqvIhNKWkRSFF3pHjkgJk2tpRb9wQhbezsqTyBvA== + dependencies: + invariant "^2.2.4" + "@react-native-community/slider@^4.4.3": version "4.5.0" resolved "https://registry.npmjs.org/@react-native-community/slider/-/slider-4.5.0.tgz" From c761f8d7624c15654d9ec5a097da168938031a1b Mon Sep 17 00:00:00 2001 From: Divyansh Anand <57003965+divyansh0908@users.noreply.github.com> Date: Tue, 1 Oct 2024 22:54:48 +0530 Subject: [PATCH 12/14] feat: Update test for NotifyScreen and NotificationForm --- .../Goals/components/NotificationForm.test.tsx | 7 ++----- __tests__/screens/NotifyScreen.test.tsx | 18 +++++++++--------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/__tests__/Goals/components/NotificationForm.test.tsx b/__tests__/Goals/components/NotificationForm.test.tsx index d3ca73b0..d1d59d11 100644 --- a/__tests__/Goals/components/NotificationForm.test.tsx +++ b/__tests__/Goals/components/NotificationForm.test.tsx @@ -4,10 +4,9 @@ import NotifyForm from '../../../src/components/Notify/NotifyForm'; import { AuthContext } from '../../../src/context/AuthContext'; import { postFcmToken, - sendNotification, getAllUsers, + sendNotification, } from '../../../src/screens/AuthScreen/Util'; -import { firebase } from '@react-native-firebase/messaging'; // Mock the functions used in the component jest.mock('../../../src/screens/AuthScreen/Util', () => ({ @@ -52,12 +51,10 @@ describe('NotifyForm', () => { expect(getByText('Notify')).toBeTruthy(); }); - it('retrieves FCM token and calls postFcmToken', async () => { + it('Calls postFcmToken', async () => { renderComponent(); await waitFor(() => { - expect(firebase.messaging().hasPermission).toHaveBeenCalled(); - expect(firebase.messaging().getToken).toHaveBeenCalled(); expect(postFcmToken).toHaveBeenCalledWith( 'mocked-fcm-token', 'user-token', diff --git a/__tests__/screens/NotifyScreen.test.tsx b/__tests__/screens/NotifyScreen.test.tsx index d1dcf07c..43af49e6 100644 --- a/__tests__/screens/NotifyScreen.test.tsx +++ b/__tests__/screens/NotifyScreen.test.tsx @@ -1,22 +1,22 @@ import React from 'react'; -import { render, waitFor } from '@testing-library/react-native'; +import { render } from '@testing-library/react-native'; import NotifyScreen from '../../src/screens/NotifyScreen/NotifyScreen'; -import NotifyForm from '../../src/components/Notify/NotifyForm'; -import { postFcmToken } from '../../src/screens/AuthScreen/Util'; -import { firebase } from '@react-native-firebase/messaging'; + +jest.mock('../../src/screens/AuthScreen/Util', () => ({ + postFcmToken: jest.fn(), + sendNotification: jest.fn(), + getAllUsers: jest.fn(() => Promise.resolve([])), // Mock getAllUsers with an empty array +})); jest.mock('@react-native-firebase/messaging', () => ({ firebase: { messaging: jest.fn(() => ({ getToken: jest.fn(() => Promise.resolve('mocked-fcm-token')), - requestPermission: jest.fn(() => Promise.resolve()), + hasPermission: jest.fn(() => Promise.resolve(1)), // Mock permission granted + requestPermission: jest.fn(() => Promise.resolve()), // Mock permission request })), }, })); -jest.mock('../../src/screens/AuthScreen/Util', () => ({ - postFcmToken: jest.fn(), - getAllUsers: jest.fn(() => Promise.resolve([])), -})); describe('NotifyScreen', () => { it('should render correctly with title and NotifyForm', async () => { const { getByText } = render(); From 9465ff4f238ab1880e55a04574c29ffa9504dfb1 Mon Sep 17 00:00:00 2001 From: Divyansh Anand <57003965+divyansh0908@users.noreply.github.com> Date: Thu, 3 Oct 2024 18:12:52 +0530 Subject: [PATCH 13/14] chore: Remove console logs --- src/components/Notify/NotifyForm.tsx | 2 -- src/service/googleServiceTemplate.js | 1 - 2 files changed, 3 deletions(-) diff --git a/src/components/Notify/NotifyForm.tsx b/src/components/Notify/NotifyForm.tsx index 33c1abf7..8a44b260 100644 --- a/src/components/Notify/NotifyForm.tsx +++ b/src/components/Notify/NotifyForm.tsx @@ -43,10 +43,8 @@ const NotifyForm = () => { const getFCMToken = async () => { const permission = await firebase.messaging().hasPermission(); - console.log('nfdeobns', permission); if (permission) { const fcmToken_ = await firebase.messaging().getToken(); - console.log('🚀 ~ getFCMToken ~ fcmToken_:', fcmToken_); await postFcmToken(fcmToken_, token); } else { diff --git a/src/service/googleServiceTemplate.js b/src/service/googleServiceTemplate.js index f5caba27..40a88c99 100644 --- a/src/service/googleServiceTemplate.js +++ b/src/service/googleServiceTemplate.js @@ -42,7 +42,6 @@ const googleServicesTemplate = { configuration_version: '1', }; -console.log('🥲🥲🥲🥲🥲', googleServicesTemplate); // Write the file to the correct location const outputPath = path.join( __dirname, From 9150ce9e35027b32d243839ed25e5e361d32c7ca Mon Sep 17 00:00:00 2001 From: Divyansh Anand <57003965+divyansh0908@users.noreply.github.com> Date: Thu, 3 Oct 2024 18:13:17 +0530 Subject: [PATCH 14/14] style: Update fontSize to use responsive value --- src/screens/NotifyScreen/NotifyScreen.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/screens/NotifyScreen/NotifyScreen.tsx b/src/screens/NotifyScreen/NotifyScreen.tsx index 640da460..074f3d2b 100644 --- a/src/screens/NotifyScreen/NotifyScreen.tsx +++ b/src/screens/NotifyScreen/NotifyScreen.tsx @@ -2,6 +2,7 @@ import React from 'react'; import { StyleSheet, Text, View } from 'react-native'; import NotifyForm from '../../components/Notify/NotifyForm'; import Colors from '../../constants/colors/Colors'; +import { scale } from '../../utils/utils'; const NotifyScreen = () => { @@ -19,7 +20,7 @@ const styles = StyleSheet.create({ padding: 16, }, title: { - fontSize: 24, + fontSize: scale(24), fontWeight: 'bold', color: '#333', marginBottom: 20,