Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live Camera Feed Enhancement(Review, QA and testing) #6223

Closed
wants to merge 193 commits into from
Closed
Show file tree
Hide file tree
Changes from 162 commits
Commits
Show all changes
193 commits
Select commit Hold shift + click to select a range
96fcc41
Merge pull request #1 from coronasafe/develop
JahnabDutta May 26, 2023
c348b50
Change MUI components to CARE components in ResultList.tsx
JahnabDutta May 26, 2023
b1fea78
Merge pull request #2 from coronasafe/develop
JahnabDutta May 26, 2023
393e374
Merge pull request #3 from coronasafe/develop
JahnabDutta May 26, 2023
0657d09
Merge branch 'develop' into develop
khavinshankar May 30, 2023
5296973
Merge branch 'coronasafe:develop' into develop
JahnabDutta May 31, 2023
a8eec96
replace MUI components in src/Components/Shifting/ListFilter.tsx
JahnabDutta May 31, 2023
a4c0702
change LegacySelectField to SelectFormField
JahnabDutta May 31, 2023
8ab013c
fix handChange issue
JahnabDutta May 31, 2023
b7c14e0
Merge branch 'develop' into develop
khavinshankar Jun 1, 2023
2cd2d15
Merge branch 'coronasafe:develop' into develop
JahnabDutta Jun 2, 2023
2be8dc5
fix shiftFilter issues
JahnabDutta Jun 2, 2023
ecf8294
add cypress tests
JahnabDutta Jun 2, 2023
f4016fd
Merge branch 'develop' into develop
nihal467 Jun 5, 2023
7b53f44
change DateRangePicker to DateRangeFormField and add cpyress tests
JahnabDutta Jun 5, 2023
ffff958
Merge original repository to develop repository
JahnabDutta Jun 5, 2023
2e4a9c0
CareUI: Adds `UserAutocompleteFormField` and used it in Assigned To f…
rithviknishad Jun 8, 2023
142f5ca
delete unused `UserSelect2`
rithviknishad Jun 8, 2023
eb3baaf
Merge branch 'develop' into tailwind/userautocompleteformfield
nihal467 Jun 9, 2023
b405d68
Merge branch 'develop' into tailwind/userautocompleteformfield
nihal467 Jun 10, 2023
aa9b9df
Merge branch 'coronasafe:develop' into develop
JahnabDutta Jun 11, 2023
2161dbf
change UserSelect to UserAutoCompleteFormField in ListFilter.tsx and …
JahnabDutta Jun 11, 2023
dc602d3
remove unnecessary grid around DateRangeFormField
JahnabDutta Jun 13, 2023
cc5342f
replace FieldLabel with SelectFormField label
JahnabDutta Jun 13, 2023
65761b2
resolve merge confilcts with ListFilter and cypress test
JahnabDutta Jun 13, 2023
07e9b3c
savepoint
JahnabDutta Jun 28, 2023
2082245
Merge branch 'develop' of https://github.com/JahnabDutta/care_fe into…
JahnabDutta Jun 30, 2023
194680e
Merge branch 'develop' of https://github.com/JahnabDutta/care_fe into…
JahnabDutta Jul 6, 2023
3a81d93
add boundary preset functionality
JahnabDutta Jul 8, 2023
eacf431
add initial UI to boundaryPreset
JahnabDutta Jul 11, 2023
155a55d
Merge branch 'develop' of https://github.com/JahnabDutta/care_fe into…
JahnabDutta Jul 11, 2023
75d9f26
Merge branch 'develop' into add-boundary-preset
JahnabDutta Jul 11, 2023
f00804d
fix updateBoundaryPreset
JahnabDutta Jul 11, 2023
afb0ec0
boundary preset ui
JahnabDutta Jul 12, 2023
36a2718
add boundary calculation algorithm
JahnabDutta Jul 12, 2023
8bb0486
Merge branch 'develop' of https://github.com/JahnabDutta/care_fe into…
JahnabDutta Jul 14, 2023
1e8ffd7
add details
JahnabDutta Jul 14, 2023
62dd642
merge develop branch
JahnabDutta Jul 14, 2023
886bdf4
add UpdateCameraBoundary skeleton
JahnabDutta Jul 14, 2023
d0dd9a4
add directional movement, expand/shrink functionality
JahnabDutta Jul 15, 2023
67bf478
Merge branch 'coronasafe:develop' into add-boundary-preset
JahnabDutta Jul 15, 2023
ca79cd7
fix conditional render bugs with updateBoundaryPreset
JahnabDutta Jul 15, 2023
5b48432
implement scroll
JahnabDutta Jul 15, 2023
c9496bf
add previewBoundary and ConfirmDialog to deleteBoundary
JahnabDutta Jul 16, 2023
7cf39f7
refactor boundarypreset screen
JahnabDutta Jul 16, 2023
b320f53
add loading to preview
JahnabDutta Jul 16, 2023
a06c086
modify UI for updateboundaryPreset
JahnabDutta Jul 16, 2023
472a235
conditional render of feedbuttons in liveFeed
JahnabDutta Jul 17, 2023
f9df5d5
refactor calcBoundary
JahnabDutta Jul 17, 2023
45b2506
refactor directional boundary update
JahnabDutta Jul 17, 2023
f1176a0
add null checks in onvifcamera file
JahnabDutta Jul 17, 2023
aef2c60
refactor and fix bugs
JahnabDutta Jul 18, 2023
0c29460
Merge branch 'coronasafe:develop' into add-boundary-preset
JahnabDutta Jul 18, 2023
398bd4d
shift camera configuration into LiveFeed, changes to UI remain
JahnabDutta Jul 20, 2023
2e7d6d9
remove loading prop
JahnabDutta Jul 21, 2023
4b4296f
Merge branch 'develop' into add-boundary-preset
JahnabDutta Jul 21, 2023
461e079
Merge branch 'add-boundary-preset' of https://github.com/JahnabDutta/…
JahnabDutta Jul 21, 2023
8b4be8a
Merge branch 'develop' of https://github.com/JahnabDutta/care_fe into…
JahnabDutta Jul 22, 2023
01af16b
solve package-lock.json conflicts
JahnabDutta Jul 22, 2023
d3657cf
initialize locking in frontend
JahnabDutta Jul 22, 2023
d10864f
display asset occupied user name
JahnabDutta Jul 24, 2023
ca776c2
Merge branch 'develop' of https://github.com/JahnabDutta/care_fe into…
JahnabDutta Jul 24, 2023
e2bf89b
merge conflicts in package-lock solved
JahnabDutta Jul 24, 2023
bba9658
change UI for adding point preset
JahnabDutta Jul 24, 2023
f6eca76
Merge branch 'develop' of https://github.com/JahnabDutta/care_fe into…
JahnabDutta Jul 26, 2023
c6ac0ce
merge upstream develop with local add-boundary-preset
JahnabDutta Jul 26, 2023
9936777
change UI for add/delete boundary
JahnabDutta Jul 26, 2023
c05b304
change UI for add/delete boundary-2
JahnabDutta Jul 26, 2023
0fdd074
make updateboundary UI skeleton
JahnabDutta Jul 26, 2023
238cfdc
add alternate functions for boundary move
JahnabDutta Jul 27, 2023
82714bb
disable buttons depending on boundary
JahnabDutta Jul 27, 2023
a29e70e
added updation info
JahnabDutta Jul 27, 2023
cfe8d60
Merge branch 'coronasafe:develop' into add-boundary-preset
JahnabDutta Jul 29, 2023
2e5cbf0
Merge branch 'add-boundary-preset' of https://github.com/JahnabDutta/…
JahnabDutta Jul 29, 2023
34c47c5
add red flash around border
JahnabDutta Jul 30, 2023
c009835
add disabled feature on bed
JahnabDutta Aug 1, 2023
a207a5b
Merge branch 'develop' of https://github.com/JahnabDutta/care_fe into…
JahnabDutta Aug 1, 2023
6269385
solve merge conflicts
JahnabDutta Aug 1, 2023
e864cc7
UI changes
JahnabDutta Aug 2, 2023
a39b731
UI changes-2
JahnabDutta Aug 2, 2023
f032fac
privacy toggle feature
JahnabDutta Aug 5, 2023
983a96f
enable privacy on feed
JahnabDutta Aug 6, 2023
508fea5
Merge branch 'coronasafe:develop' into add-boundary-preset
JahnabDutta Aug 6, 2023
1cb09df
Merge branch 'coronasafe:develop' into patient-privacy-feature
JahnabDutta Aug 6, 2023
32ed93b
add privacy check from consultationmodel
JahnabDutta Aug 6, 2023
a4d9c93
Merge branch 'add-boundary-preset' of https://github.com/JahnabDutta/…
JahnabDutta Aug 6, 2023
cf5205e
move privacytoggle to left
JahnabDutta Aug 6, 2023
e89b795
Merge branch 'develop' into add-boundary-preset
JahnabDutta Aug 7, 2023
0657245
merge conflicts
JahnabDutta Aug 7, 2023
e4f4e6a
update privacy privilideges, change privacy button,privacy screen
JahnabDutta Aug 8, 2023
6f3c57a
Merge branch 'coronasafe:develop' into add-boundary-preset
JahnabDutta Aug 9, 2023
271927a
Merge branch 'coronasafe:develop' into lock-asset-feature
JahnabDutta Aug 9, 2023
dfce418
Merge branch 'develop' of https://github.com/JahnabDutta/care_fe into…
JahnabDutta Aug 9, 2023
b0619fb
merge conflicts
JahnabDutta Aug 9, 2023
e523b7c
move privacytoggle into new file
JahnabDutta Aug 10, 2023
5a20a91
Merge branch 'develop' of https://github.com/JahnabDutta/care_fe into…
JahnabDutta Aug 12, 2023
4a99291
Revert "fix no data issue in notice borad on hard reload (#6025)"
JahnabDutta Aug 12, 2023
3cfada4
Revert "fixes #6062; align tooltip of `RecordMeta` relative time (#60…
JahnabDutta Aug 12, 2023
dc293a8
register service workers
JahnabDutta Aug 12, 2023
c94b33c
fix feed.tsx conflicts
JahnabDutta Aug 12, 2023
188a2c8
merge add boundary to lock asset branch
JahnabDutta Aug 12, 2023
10b7f70
Merge branch 'lock-asset-feature' of https://github.com/JahnabDutta/c…
JahnabDutta Aug 12, 2023
e990612
add useMessageListner hook
JahnabDutta Aug 15, 2023
bfa1fbc
add subscribe for info button
JahnabDutta Aug 18, 2023
a33dca4
implement real time unlock-asset in frontend
JahnabDutta Aug 19, 2023
2c9268c
add support for locking/unlocking without subscription
JahnabDutta Aug 19, 2023
3c654c5
Merge branch 'develop' of https://github.com/JahnabDutta/care_fe into…
JahnabDutta Aug 21, 2023
2585b02
conflicts fixed
JahnabDutta Aug 21, 2023
338643f
merge conflicts fixed
JahnabDutta Aug 21, 2023
0e12642
merge conflicts fixed
JahnabDutta Aug 21, 2023
8c3c492
changes in patientPrivacyToggle component
JahnabDutta Aug 21, 2023
571bcca
add privacy feature in feed page itself
JahnabDutta Aug 22, 2023
aae3af4
change props of privacytoggle component
JahnabDutta Aug 22, 2023
f12126d
add timeout in camera access
JahnabDutta Aug 22, 2023
4485d56
Merge branch 'develop' into add-boundary-preset
JahnabDutta Aug 23, 2023
1dfaf6b
Merge branch 'develop' into patient-privacy-feature
JahnabDutta Aug 23, 2023
09e65e0
Merge branch 'develop' into lock-asset-feature
JahnabDutta Aug 23, 2023
e88a0bb
crash/window close support & request access
JahnabDutta Aug 25, 2023
bf1c754
fix merge conflicts
JahnabDutta Aug 25, 2023
9cc3411
solve merge conflicts
JahnabDutta Aug 25, 2023
ab58415
solve conflicts
JahnabDutta Aug 25, 2023
e62efbe
add cypress test
JahnabDutta Aug 27, 2023
c04f2e9
Merge branch 'add-boundary-preset' into patient-privacy-feature
JahnabDutta Aug 27, 2023
66d7d4a
add privacy cypress tests
JahnabDutta Aug 27, 2023
1a212c9
add comments to functions for readability
JahnabDutta Aug 28, 2023
76c0ce5
Merge branch 'add-boundary-preset' of https://github.com/JahnabDutta/…
JahnabDutta Aug 28, 2023
78a1d33
Merge branch 'add-boundary-preset' into patient-privacy-feature
JahnabDutta Aug 28, 2023
0333d6b
add comments
JahnabDutta Aug 28, 2023
c0d1a8a
add comments
JahnabDutta Aug 28, 2023
5057647
add comments
JahnabDutta Aug 28, 2023
ef0c22e
change bg of privacy buttons
JahnabDutta Aug 29, 2023
8e2fdae
merge upstream develop
JahnabDutta Aug 29, 2023
febfa20
merge upstream develop
JahnabDutta Aug 29, 2023
973c017
merge upstream develop
JahnabDutta Aug 30, 2023
140ee41
merge upstream develop
JahnabDutta Aug 30, 2023
a503745
merge upstream develop
JahnabDutta Aug 30, 2023
31f3ecf
merge upstream develop
JahnabDutta Aug 30, 2023
e7f36b5
revert extra files
JahnabDutta Aug 30, 2023
e14c123
Merge branch 'add-boundary-preset' into patient-privacy-feature
JahnabDutta Aug 30, 2023
36e5278
revert changes in other files
JahnabDutta Aug 30, 2023
6d71f33
Merge branch 'add-boundary-preset' into lock-asset-feature
JahnabDutta Aug 30, 2023
57cd0bf
Merge branch 'lock-asset-feature' into qa-testing
JahnabDutta Sep 5, 2023
da93272
Merge branch 'coronasafe:develop' into qa-testing
JahnabDutta Sep 5, 2023
e4e450d
fix cypress test for patient privacy
JahnabDutta Sep 5, 2023
131e3c7
Merge branch 'qa-testing' of https://github.com/JahnabDutta/care_fe i…
JahnabDutta Sep 5, 2023
b819da6
tests on staging backend
JahnabDutta Sep 5, 2023
cf4a564
Revert "tests on staging backend"
JahnabDutta Sep 5, 2023
7746f63
Merge branch 'coronasafe:develop' into qa-testing
JahnabDutta Sep 6, 2023
49d04bd
change notif for update boundary
JahnabDutta Sep 6, 2023
1968986
merge
JahnabDutta Sep 6, 2023
3df9221
requested changes
JahnabDutta Sep 6, 2023
a389f5d
Merge branch 'develop' into qa-testing
khavinshankar Nov 15, 2023
80a8917
add patient privacy toggle in consultation feed tab
khavinshankar Nov 15, 2023
7a7c0b7
Remove unused dependency in Feed component
khavinshankar Nov 15, 2023
9ad0b19
increased stream refresh time
khavinshankar Nov 15, 2023
cc163f5
Merge branch 'develop' into qa-testing
khavinshankar Nov 20, 2023
b726f11
add type for subscription status
Pranshu1902 Nov 30, 2023
b9f5e92
revert return conditions
Pranshu1902 Dec 1, 2023
c4b9b2c
Merge branch 'develop' into qa-testing
Pranshu1902 Dec 4, 2023
b228778
fix merge errors
Pranshu1902 Dec 4, 2023
e6d243e
fetchFacility method called once
Pranshu1902 Dec 4, 2023
801c3b1
Merge branch 'develop' into janab-qa-testing
Pranshu1902 Dec 8, 2023
7b558db
Merge branch 'develop' into janab-qa-testing
Pranshu1902 Dec 26, 2023
3ebcea8
fix deployment issue
Pranshu1902 Dec 26, 2023
8b646d3
refactor
Pranshu1902 Dec 27, 2023
8ef4279
enhance buttons ui
Pranshu1902 Dec 27, 2023
67c20f2
improve popup size
Pranshu1902 Dec 27, 2023
ae54406
use dummy data in cypress
Pranshu1902 Dec 27, 2023
856972e
Merge branch 'develop' into janab-qa-testing
Pranshu1902 Dec 28, 2023
7f6718c
fix camera boundary cypress test
Pranshu1902 Dec 28, 2023
6c7cb60
fix patient privacy cypress test
Pranshu1902 Dec 29, 2023
2a32255
refactor tests
Pranshu1902 Dec 29, 2023
af0f552
Merge branch 'develop' into janab-qa-testing
Pranshu1902 Jan 13, 2024
0cb16c8
replace dispatch with request
Pranshu1902 Jan 13, 2024
5abf173
Merge branch 'develop' into janab-qa-testing
Pranshu1902 Jan 25, 2024
1705003
Merge branch 'develop' into janab-qa-testing
Pranshu1902 Feb 2, 2024
fbc38e0
fix privacy lock
Pranshu1902 Feb 3, 2024
6989020
Merge branch 'develop' into qa-testing
Pranshu1902 Feb 12, 2024
e87b917
Merge branch 'develop' into qa-testing
Pranshu1902 Feb 19, 2024
cef6d2e
fix import error
Pranshu1902 Feb 19, 2024
9ecbaf5
use new routes
Pranshu1902 Feb 21, 2024
fd140bf
use updated routes
Pranshu1902 Feb 24, 2024
9fe5125
Merge branch 'develop' of github.com:coronasafe/care_fe into qa-testing
khavinshankar Feb 25, 2024
5300669
replace with request
Pranshu1902 Mar 7, 2024
c5a09b2
use request in patientprivacy
Pranshu1902 Mar 7, 2024
a894881
update response body type in api.tsx
Pranshu1902 Mar 7, 2024
f8b8676
replace to request
Pranshu1902 Mar 7, 2024
fb9c605
remove firerequest unused methods
Pranshu1902 Mar 7, 2024
2500430
Merge branch 'develop' into qa-testing
Pranshu1902 Mar 7, 2024
98087f9
fix icons using careicon
Pranshu1902 Mar 9, 2024
dd6fc4e
fix privacy toggle and store locked_by
Pranshu1902 Mar 9, 2024
98cb2a9
refactor
Pranshu1902 Mar 9, 2024
e05f396
replace with request
Pranshu1902 Mar 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions cypress/e2e/camera_spec/camera_boundary.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { cy, describe, before, beforeEach, it } from "local-cypress";
const user = { username: "devdistrictadmin", password: "Coronasafe@123" };
describe("Camera Boundary", () => {
before(() => {
cy.loginByApi(user.username, user.password);
cy.saveLocalStorage();
});
beforeEach(() => {
cy.restoreLocalStorage();
cy.awaitUrl("/assets");
cy.get("input[id='search']").type("Dev Camera");
cy.contains("a", "Dev Camera").contains("a", "ICU").click();
cy.get("button[id='configure-asset']").click();
});

it("Add new boundary", () => {
cy.get("input[name='bed']").type("bed 01");
cy.get("li[role='option']").contains("Bed 01").click();
cy.wait(2000);
cy.intercept("**/api/v1/assetbed/**").as("addBoundary");
cy.get("button[id='add-boundary-preset']").click();
cy.wait("@addBoundary");
});

it("Update boundary", () => {
cy.get("input[name='bed']").type("bed 01");
cy.get("li[role='option']").contains("Bed 01").click();
cy.wait(2000);
cy.get("button[id='update-boundary-preset']").click();
cy.intercept("**/api/v1/assetbed/**").as("updateBoundary");
cy.get("button")
.find("svg.care-svg-icon__baseline.care-l-angle-right")
.should("be.visible")
.first()
.click();
cy.wait("@updateBoundary");
cy.get("button").contains("Next").click();
cy.get("button")
.find("svg.care-svg-icon__baseline.care-l-angle-right")
.should("be.visible")
.first()
.click();
cy.wait("@updateBoundary");
cy.get("button").contains("Next").click();
cy.get("button")
.find("svg.care-svg-icon__baseline.care-l-angle-up")
.should("be.visible")
.first()
.click();
cy.wait("@updateBoundary");
cy.get("button").contains("Next").click();
cy.get("button")
.find("svg.care-svg-icon__baseline.care-l-angle-down")
.should("be.visible")
.first()
.click();
cy.wait("@updateBoundary");
cy.get("button").contains("Done").click();
});

it("Delete boundary", () => {
cy.get("input[name='bed']").type("bed 01");
cy.get("li[role='option']").contains("Bed 01").click();
cy.wait(1000);
cy.intercept("**/api/v1/assetbed/**").as("deleteBoundary");
cy.get("button[id='delete-boundary-preset']").click();
cy.get("button").contains("Delete").click();
cy.wait("@deleteBoundary");
});
});
25 changes: 25 additions & 0 deletions cypress/e2e/camera_spec/patient_privacy.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { cy, describe, before, beforeEach, it } from "local-cypress";
const user = { username: "devdistrictadmin", password: "Coronasafe@123" };

describe("Patient Privacy", () => {
before(() => {
cy.loginByApi(user.username, user.password);
cy.saveLocalStorage();
});
beforeEach(() => {
cy.restoreLocalStorage();
cy.awaitUrl("/patients");
cy.intercept("**/api/v1/consultation/**").as("consultation");
cy.get("input[id='name']").type("Dummy Patient");
cy.contains("a", "Dummy Patient").contains("a", "Dummy Facility 1").click();
cy.wait("@consultation");
cy.get("a").contains("Camera Feed").click();
cy.wait("@consultation");
});

it("Toggle privacy", () => {
cy.intercept("**/api/v1/consultationbed/**").as("togglePrivacy");
cy.get("button[id='privacy-toggle']").click();
cy.wait("@togglePrivacy");
});
});
10 changes: 9 additions & 1 deletion src/Common/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ export const RHYTHM_CHOICES: Array<OptionsType> = [
{ id: 10, text: "IRREGULAR", desc: "Irregular" },
];

export const LOCATION_BED_TYPES: Array<any> = [
export const LOCATION_BED_TYPES: Array<{ id: string; name: string }> = [
{ id: "ISOLATION", name: "Isolation" },
{ id: "ICU", name: "ICU" },
{ id: "BED_WITH_OXYGEN_SUPPORT", name: "Bed with oxygen support" },
Expand Down Expand Up @@ -1020,6 +1020,14 @@ export const XLSXAssetImportSchema = {
},
};

export type direction = "left" | "right" | "up" | "down" | null;
export interface BoundaryRange {
max_x: number;
min_x: number;
max_y: number;
min_y: number;
}

export const USER_TYPES_MAP = {
Pharmacist: "Pharmacist",
Volunteer: "Volunteer",
Expand Down
59 changes: 59 additions & 0 deletions src/Common/hooks/useFeedPTZ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ interface PTZPayload {
x: number;
y: number;
zoom: number;
id?: string | null;
camera_state?: PTZState | null;
}

export interface PTZState {
Expand Down Expand Up @@ -55,6 +57,9 @@ interface UseMSEMediaPlayerReturnType {
getCameraStatus: (options: IOptions) => void;
getPresets: (options: IOptions) => void;
gotoPreset: (payload: IGotoPresetPayload, options: IOptions) => void;
lockAsset: (options: IOptions) => void;
unlockAsset: (options: IOptions) => void;
requestAccess: (options: IOptions) => void;
}

interface IOptions {
Expand Down Expand Up @@ -196,6 +201,57 @@ export const getPTZPayload = (
return { x, y, zoom };
};

const lockAsset =
(config: IAsset, dispatch: any) =>
async (options: IOptions = {}) => {
if (!config.id) return;
const resp = await dispatch(
operateAsset(config.id, {
action: {
type: "lock_asset",
},
})
);
resp &&
(resp.status === 200
? options?.onSuccess && options.onSuccess(resp.data.result)
: options?.onError && options.onError(resp));
};

const requestAccess =
(config: IAsset, dispatch: any) =>
async (options: IOptions = {}) => {
if (!config.id) return;
const resp = await dispatch(
operateAsset(config.id, {
action: {
type: "request_access",
},
})
);
resp &&
(resp.status === 200
? options?.onSuccess && options.onSuccess(resp.data.result)
: options?.onError && options.onError(resp));
};

const unlockAsset =
(config: IAsset, dispatch: any) =>
async (options: IOptions = {}) => {
if (!config.id) return;
const resp = await dispatch(
operateAsset(config.id, {
action: {
type: "unlock_asset",
},
})
);
resp &&
(resp.status === 200
? options?.onSuccess && options.onSuccess(resp.data.result)
: options?.onError && options.onError(resp));
};

export const useFeedPTZ = ({
config,
dispatch,
Expand All @@ -207,5 +263,8 @@ export const useFeedPTZ = ({
getCameraStatus: getCameraStatus(config, dispatch),
getPresets: getPresets(config, dispatch),
gotoPreset: gotoPreset(config, dispatch),
lockAsset: lockAsset(config, dispatch),
unlockAsset: unlockAsset(config, dispatch),
requestAccess: requestAccess(config, dispatch),
};
};
138 changes: 138 additions & 0 deletions src/Common/hooks/useNotificationSubscribe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { useDispatch } from "react-redux";
import {
getUserPnconfig,
updateUserPnconfig,
getPublicKey,
} from "../../Redux/actions";
Pranshu1902 marked this conversation as resolved.
Show resolved Hide resolved
import { useState } from "react";
import * as Sentry from "@sentry/browser";
import { useTranslation } from "react-i18next";
import { Error } from "../../Utils/Notifications.js";
import useAuthUser from "../../Common/hooks/useAuthUser";

type SubscriptionStatusType =
| ""
| "NotSubscribed"
| "SubscribedOnThisDevice"
| "SubscribedOnAnotherDevice";

export default function useNotificationSubscribe() {
const { username } = useAuthUser();
const dispatch: any = useDispatch();
const [subscriptionStatus, setSubscriptionStatus] =
useState<SubscriptionStatusType>("");
const [isSubscribing, setIsSubscribing] = useState(false);
const { t } = useTranslation();

const intialSubscriptionState = async () => {
try {
const res = await dispatch(getUserPnconfig({ username: username }));
const reg = await navigator.serviceWorker.ready;
const subscription = await reg.pushManager.getSubscription();
if (!subscription && !res?.data?.pf_endpoint) {
setSubscriptionStatus("NotSubscribed");
} else if (subscription?.endpoint === res?.data?.pf_endpoint) {
setSubscriptionStatus("SubscribedOnThisDevice");
} else {
setSubscriptionStatus("SubscribedOnAnotherDevice");
}
} catch (error) {
Sentry.captureException(error);
}
};

const handleSubscribeClick = () => {
const status = subscriptionStatus;
if (status === "NotSubscribed" || status === "SubscribedOnAnotherDevice") {
subscribe();
} else {
unsubscribe();
}
};

const unsubscribe = () => {
navigator.serviceWorker.ready
.then(function (reg) {
setIsSubscribing(true);
reg.pushManager
.getSubscription()
.then(function (subscription) {
subscription
?.unsubscribe()
.then(async function (_successful) {
const data = {
pf_endpoint: "",
pf_p256dh: "",
pf_auth: "",
};
await dispatch(
updateUserPnconfig(data, { username: username })
);

setSubscriptionStatus("NotSubscribed");
setIsSubscribing(false);
})
.catch(function (_e) {
Error({
msg: t("unsubscribe_failed"),
});
});
})
.catch(function (_e) {
Error({ msg: t("subscription_error") });
});
})
.catch(function (_e) {
Sentry.captureException(_e);
});
};

async function subscribe() {
setIsSubscribing(true);
const response = await dispatch(getPublicKey());
const public_key = response.data.public_key;
const sw = await navigator.serviceWorker.ready;
const push = await sw.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: public_key,
});
const p256dh = btoa(
String.fromCharCode.apply(
null,
new Uint8Array(push.getKey("p256dh") as any) as any
)
);
const auth = btoa(
String.fromCharCode.apply(
null,
new Uint8Array(push.getKey("auth") as any) as any
)
);

const data = {
pf_endpoint: push.endpoint,
pf_p256dh: p256dh,
pf_auth: auth,
};

const res = await dispatch(
updateUserPnconfig(data, { username: username })
);

if (res.status >= 200 && res.status <= 300) {
setSubscriptionStatus("SubscribedOnThisDevice");
}
setIsSubscribing(false);
}

return {
subscriptionStatus,
isSubscribing,
setSubscriptionStatus,
setIsSubscribing,
handleSubscribeClick,
intialSubscriptionState,
subscribe,
unsubscribe,
};
}
Loading
Loading