Skip to content

Commit 080ba08

Browse files
authored
Merge pull request #125 from enBloc-org/#104/event-interval
#104/event interval
2 parents 599b46d + bfdd629 commit 080ba08

File tree

6 files changed

+36
-25
lines changed

6 files changed

+36
-25
lines changed

src/background/messages/triggerPopup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
99
const { data } = await fetchStrapiContent<EventData>(
1010
`api/events/${req.body.id}?${eventPopupQueryString}`
1111
)
12-
backgroundPopupCreate(data.pop_ups)
12+
backgroundPopupCreate(data.pop_ups, data.time_delay)
1313
res.send("success!")
1414
}
1515

src/types/eventTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface EventData {
77
title: string
88
createdAt: string
99
updatedAt: string
10+
time_delay: number
1011
pop_ups: Popup[]
1112
}
1213

src/utils/eventAlarmListener.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ export default async function eventAlarmListener(alarm) {
2323
)
2424
if (error) console.error(error)
2525

26-
const { pop_ups, numberOfEvents } =
26+
const { popUps, numberOfEvents, timeDelay } =
2727
user.project_id === 0
2828
? await getCurrentProjectPopups(user.current_index)
2929
: await getProjectPopups(user.project_id, user.current_index)
3030

31-
await backgroundPopupCreate(pop_ups)
31+
await backgroundPopupCreate(popUps, timeDelay)
3232
const newIndex = iterateIndex(numberOfEvents, user.current_index)
3333

3434
const response = await fetchStrapiContent<User>(
@@ -46,9 +46,9 @@ export default async function eventAlarmListener(alarm) {
4646
"arebyte-public-index"
4747
)
4848

49-
const { pop_ups, numberOfEvents } =
49+
const { popUps, numberOfEvents, timeDelay } =
5050
await getCurrentProjectPopups(publicIndex)
51-
await backgroundPopupCreate(pop_ups)
51+
await backgroundPopupCreate(popUps, timeDelay)
5252

5353
const newPublicIndex = iterateIndex(numberOfEvents, publicIndex)
5454
await storage.set("arebyte-public-index", newPublicIndex)

src/utils/getCurrentProjectPopups.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { fetchStrapiContent } from "./fetchStrapiContent"
88
/**
99
*
1010
* @description fetches an array of pop ups from the unique type Current_Project
11+
* @returns an object with the array of pop_ups, the total number of events in the project and the time_delay value set for the event
12+
* @example const { popUps, numberOfEvents, timeDelay } = getCurrentProjectPopups(currentIndex)
1113
*/
1214
export default async function getCurrentProjectPopups(
1315
currentIndex: number
@@ -17,13 +19,12 @@ export default async function getCurrentProjectPopups(
1719
)
1820
const currentEventId =
1921
currentProject.data.project.sequence[currentIndex].id
20-
const {
21-
data: { pop_ups }
22-
} = await fetchStrapiContent<EventData>(
22+
const { data } = await fetchStrapiContent<EventData>(
2323
`api/events/${currentEventId}?${eventPopupQueryString}`
2424
)
2525
return {
26-
pop_ups: pop_ups,
27-
numberOfEvents: currentProject.data.project.sequence.length
26+
popUps: data.pop_ups,
27+
numberOfEvents: currentProject.data.project.sequence.length,
28+
timeDelay: data.time_delay
2829
}
2930
}

src/utils/getProjectPopups.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { fetchStrapiContent } from "./fetchStrapiContent"
88
/**
99
*
1010
* @description fetches an array of popups from the project with the given Id
11+
* @returns an object with the full array of popups, the total number of events in the project and the time_delay set for the event
12+
* @example const { popUps, numberOfEvents, timeDelay } = getProjectPopups(projectId, currentIndex)
1113
*/
1214
export default async function getProjectPopups(
1315
projectId: number,
@@ -17,14 +19,13 @@ export default async function getProjectPopups(
1719
`api/projects/${projectId}?${projectQueryString}`
1820
)
1921
const currentEventId = currentProject.data.sequence[currentIndex].id
20-
const {
21-
data: { pop_ups }
22-
} = await fetchStrapiContent<EventData>(
22+
const { data } = await fetchStrapiContent<EventData>(
2323
`api/events/${currentEventId}?${eventPopupQueryString}`
2424
)
2525

2626
return {
27-
pop_ups: pop_ups,
28-
numberOfEvents: currentProject.data.sequence.length
27+
popUps: data.pop_ups,
28+
numberOfEvents: currentProject.data.sequence.length,
29+
timeDelay: data.time_delay
2930
}
3031
}

src/utils/popup-utils/backgroundPopCreate.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import determineFormat from "./determineFormat"
1212
import parseImageSize from "./parseImageSize"
1313
import parseWindowSize from "./parseWindowSize"
1414

15-
const backgroundPopupCreate = async (popups: Popup[]) => {
15+
const backgroundPopupCreate = async (
16+
popups: Popup[],
17+
timeDelay: number
18+
) => {
1619
// Get system widow size
1720
const screenDimensions = await Browser.tabs
1821
.query({ active: true, currentWindow: true })
@@ -145,15 +148,20 @@ const backgroundPopupCreate = async (popups: Popup[]) => {
145148
await Browser.storage.session.set({ arebytePopups: slimPopups })
146149

147150
// Create windows
148-
slimPopups.forEach(async popup => {
149-
await createWindow(
150-
popup.index,
151-
popup.width,
152-
popup.height,
153-
popup.top,
154-
popup.left
155-
)
156-
})
151+
for (const popup of slimPopups) {
152+
await new Promise(resolve => {
153+
setTimeout(async () => {
154+
const window = await createWindow(
155+
popup.index,
156+
popup.width,
157+
popup.height,
158+
popup.top,
159+
popup.left
160+
)
161+
resolve(window)
162+
}, timeDelay * 100)
163+
})
164+
}
157165
}
158166

159167
export default backgroundPopupCreate

0 commit comments

Comments
 (0)