Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
98c366b
style: changes day to sequnce
nichgalzin Nov 7, 2024
6ee76a2
refactor: refactors event alarm to fetch and update user data from db…
nichgalzin Nov 7, 2024
91d0a30
fix: removes awaits causing lint error
nichgalzin Nov 7, 2024
2e06d4f
refactor: adds loading state for app
nichgalzin Nov 7, 2024
b0291d6
feat: Loading spinner
nichgalzin Nov 7, 2024
2da9587
refactor: makes button update user store
nichgalzin Nov 7, 2024
97cc459
refactor: User info updated in store after database
nichgalzin Nov 7, 2024
cf1c1d7
fix: intitialises project_id to undefined to prevent data fetch glitch
nichgalzin Nov 7, 2024
661a27e
refactor: ditches loading spinner
nichgalzin Nov 7, 2024
7ce68e5
refactor: removes storage update that is not required
nichgalzin Nov 8, 2024
03da0af
fix: returns updated values
nichgalzin Nov 8, 2024
5459f35
refactor: modifies user session information
nichgalzin Nov 8, 2024
20765d6
refactor: modifies user session information
nichgalzin Nov 8, 2024
b65cccf
refactor: modifies user session information
nichgalzin Nov 8, 2024
023765a
fix: defines default project
nichgalzin Nov 8, 2024
2bbefbb
deletes loading spinner
nichgalzin Nov 8, 2024
a131bdf
fix: adds await clause to storage set
nichgalzin Nov 8, 2024
87d199f
refactor: adds and implements logged in method
nichgalzin Nov 8, 2024
aa9f022
refactor:adds early return and tweaks session check logic
nichgalzin Nov 8, 2024
710ffd0
Merge remote-tracking branch 'origin' into 113/hotfix-homepage
nichgalzin Nov 8, 2024
8dba68d
fix: removes commented out code
nichgalzin Nov 8, 2024
1f17791
refactor: replace useStorage hook with a call to the storage api with…
camelPhonso Nov 8, 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
2 changes: 1 addition & 1 deletion src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (process.env.NODE_ENV === "production") {

Browser.runtime.onInstalled.addListener(async () => {
const storage = newStorage()
storage.set("arebyte-public-index", 0)
await storage.set("arebyte-public-index", 0)

const currentProject =
await fetchStrapiContent<CurrentProjectData>(
Expand Down
5 changes: 1 addition & 4 deletions src/background/messages/selectNewActiveProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { PlasmoMessaging } from "@plasmohq/messaging"
import { User, UserSession } from "~types/userTypes"
import { fetchStrapiContent } from "~utils/fetchStrapiContent"
import newStorage from "~utils/newStorage"
import updateStorage from "~utils/updateStorage"

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const storage = newStorage()
Expand All @@ -28,12 +27,10 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
res.send(false)
}

const newSession = updateStorage(userSession, {
res.send({
project_id: data.project_id,
current_index: data.current_index
})
await storage.set("arebyte-audience-session", newSession)
res.send(true)
}

export default handler
3 changes: 0 additions & 3 deletions src/background/messages/updateUserDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ const handler: PlasmoMessaging.MessageHandler = async (req, res) => {

const newSession = updateStorage(userSession, {
id: response.data.id,
event_time: response.data.event_time,
project_id: response.data.project_id,
current_index: response.data.current_index
})
await storage.set("arebyte-audience-session", newSession)
res.send(response)
Expand Down
6 changes: 3 additions & 3 deletions src/components/HomePage/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function HomePage() {
const { showBoundary } = useErrorBoundary()

useEffect(() => {
const getUserSession = async () => {
const fetchProjectData = async () => {
if (project_id === 0) {
const {
data,
Expand All @@ -48,7 +48,7 @@ export default function HomePage() {
updateCurrentProject(data)
}
}
getUserSession()
fetchProjectData()
}, [project_id, current_index])

return (
Expand All @@ -75,7 +75,7 @@ export default function HomePage() {
<td>{event_time.slice(0, -4)}</td>
</tr>
<tr>
<td>Day:</td>
<td>Sequence:</td>
<td>
{current_index + 1} of{" "}
{currentProject.sequence.length}
Expand Down
3 changes: 2 additions & 1 deletion src/components/SelectProjectButton/SelectProjectButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default function SelectProjectButton() {
const { project_id: currentProjectId } = useStore.use.user()
const isLoggedIn = useStore.use.isLoggedIn()
const navigateTo = useStore.use.navigateTo()
const updateUser = useStore.use.updateUser()
const exploreProjectId = useStore.use.exploreProjectId()
const [isShowingAlarm, setIsShowingAlarm] = useState<boolean>(false)
const [isLoading, setIsLoading] = useState<boolean>(false)
Expand All @@ -22,7 +23,7 @@ export default function SelectProjectButton() {
name: "selectNewActiveProject",
body: { selectedProjectId: exploreProjectId }
})

updateUser({ ...result})
if (result) setIsLoading(false)
}

Expand Down
5 changes: 2 additions & 3 deletions src/components/page-components/LoginPage/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { AuthData, UserSession } from "~types/userTypes"

export default function LoginPage() {
const navigateTo = useStore.use.navigateTo()
const logInUser = useStore.use.logInUser()
const [errorMessage, setErrorMessage] = useState("")
const [isLoading, setIsLoading] = useState(false)
const [, setUserSession] = useStorage("arebyte-audience-session")
Expand Down Expand Up @@ -59,12 +60,10 @@ export default function LoginPage() {

const userSession: UserSession = {
id: data.user.id,
project_id: data.user.project_id,
event_time: data.user.event_time,
current_index: data.user.current_index,
jwt: data.jwt
}
setUserSession(userSession)
logInUser()
navigateTo("home")
return actions.setSubmitting(false)
}}
Expand Down
9 changes: 8 additions & 1 deletion src/components/page-components/ProfilePage/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function ProfilePage() {
const navigateTo = useStore.use.navigateTo()
const resetStore = useStore.use.resetStore()
const userInfo = useStore.use.user()
const updateUser = useStore.use.updateUser()
const { is_paused: isPaused } = useStore.use.user()
const updatedIsPaused = useStore.use.updateIsPaused()
const [isOpen, setIsOpen] = useState<boolean>(false)
Expand Down Expand Up @@ -128,7 +129,13 @@ export default function ProfilePage() {
})
if (alarmError) setErrorMessage(alarmError)
}

updateUser({
username: data.username,
email: data.email,
birth_date: data.birth_date,
location: data.location,
event_time: data.event_time
})
setIsLoading(false)
setIsOpen(false)
return actions.setSubmitting(false)
Expand Down
5 changes: 2 additions & 3 deletions src/components/page-components/SignUpPage/SignUpPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { AuthData, UserSession } from "~types/userTypes"

const SignUpPage = () => {
const navigateTo = useStore.use.navigateTo()
const logInUser = useStore.use.logInUser()
const [errorMessage, setErrorMessage] = useState("")
const [isLoading, setIsLoading] = useState(false)
const [, setUserSession] = useStorage("arebyte-audience-session")
Expand Down Expand Up @@ -91,14 +92,12 @@ const SignUpPage = () => {

const userSession: UserSession = {
id: data.user.id,
project_id: data.user.project_id,
event_time: data.user.event_time,
current_index: data.user.current_index,
jwt: data.jwt
}

setUserSession(userSession)
actions.setSubmitting(false)
logInUser()
setIsLoading(false)
navigateTo("home")
}}
Expand Down
50 changes: 30 additions & 20 deletions src/popup.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "./components/normalize.css"
import "~components/globals.css"

import { useEffect } from "react"
import { useEffect, useState } from "react"
import { ErrorBoundary } from "react-error-boundary"
import { CSSTransition } from "react-transition-group"

Expand All @@ -25,34 +25,44 @@ import newStorage from "~utils/newStorage"

function IndexPopup() {
const currentPage = useStore.use.currentPage()
const logInUser = useStore.use.logInUser()
const isLoggedIn = useStore.use.isLoggedIn()
const updateUser = useStore.use.updateUser()
const updateCurrentIndex = useStore.use.updateCurrentIndex()

const [userSession] = useStorage<UserSession>({
key: "arebyte-audience-session",
instance: newStorage()
})
const [publicIndex] = useStorage<number>({
key: "arebyte-public-index",
instance: newStorage()
})
const [isLoading, setIsLoading] = useState<boolean>(false)
const storage = newStorage()

useEffect(() => {
const fetchUserProfile = async () => {
if (userSession) {
const { data, error }: { data: User; error: string | null } =
await sendToBackground({
name: "fetchUserProfile",
body: { jwt: userSession.jwt, id: userSession.id }
})
if (error) console.error(error)
updateUser(data)
setIsLoading(true)
const userSession: UserSession = await storage.get(
"arebyte-audience-session"
)

if (!userSession) {
const publicIndex: number = await storage.get(
"arebyte-public-index"
)
updateUser({
current_index: publicIndex,
project_id: 0
})
return setIsLoading(false)
}

const { data, error }: { data: User; error: string | null } =
await sendToBackground({
name: "fetchUserProfile",
body: { jwt: userSession.jwt, id: userSession.id }
})
if (error) console.error(error)
updateUser(data)
logInUser()
setIsLoading(false)
}
fetchUserProfile()
}, [userSession, publicIndex])
}, [isLoggedIn])

if (isLoading) return
return (
<ErrorBoundary FallbackComponent={ErrorFallback}>
<Layout theme={isLoggedIn ? "logged-in" : "logged-out"}>
Expand Down
11 changes: 8 additions & 3 deletions src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface Actions {
updateExploreProjectId: (id: number) => void
updateIsPaused: (newStatus: boolean) => void
updateCurrentIndex: (newIndex: number) => void
logInUser: () => void
}

const initialState: State = {
Expand All @@ -41,7 +42,7 @@ const initialState: State = {
birth_date: undefined,
location: undefined,
is_paused: false,
project_id: 0,
project_id: undefined,
current_index: 0,
event_time: "12:00:00.000"
},
Expand Down Expand Up @@ -75,8 +76,7 @@ const baseStore = create<State & Actions>(set => {
user: {
...state.user,
...newUser
},
isLoggedIn: true
}
}))
},
resetStore: () =>
Expand Down Expand Up @@ -104,6 +104,11 @@ const baseStore = create<State & Actions>(set => {
...state.user,
current_index: newIndex
}
})),
logInUser: () =>
set(state => ({
...state,
isLoggedIn: true
}))
}
})
Expand Down
6 changes: 1 addition & 5 deletions src/types/userTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ export interface AuthData {
jwt: string
}

export interface UserSession
extends Pick<
User,
"event_time" | "project_id" | "current_index" | "id"
> {
export interface UserSession extends Pick<User, "id"> {
jwt: string
}

Expand Down
37 changes: 24 additions & 13 deletions src/utils/eventAlarmListener.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { UserSession } from "~types/userTypes"
import { userQueryString } from "~queries/userQuery"
import type { User, UserSession } from "~types/userTypes"

import { fetchStrapiContent } from "./fetchStrapiContent"
import getCurrentProjectPopups from "./getCurrentProjectPopups"
import getProjectPopups from "./getProjectPopups"
import iterateIndex from "./iterateIndex"
import newStorage from "./newStorage"
import backgroundPopupCreate from "./popup-utils/backgroundPopCreate"
import updateStorage from "./updateStorage"

export default async function eventAlarmListener(alarm) {
if (alarm.name !== "sequence-alarm") return
Expand All @@ -15,21 +16,31 @@ export default async function eventAlarmListener(alarm) {
)

if (userSession) {
const projectId = userSession.project_id
const currentIndex = userSession.current_index
const { data: user, error } = await fetchStrapiContent<User>(
`api/users/${userSession.id}?${userQueryString}`,
"GET",
userSession.jwt
)
if (error) console.error(error)

const { pop_ups, numberOfEvents } =
projectId === 0
? await getCurrentProjectPopups(currentIndex)
: await getProjectPopups(projectId, currentIndex)
user.project_id === 0
? await getCurrentProjectPopups(user.current_index)
: await getProjectPopups(user.project_id, user.current_index)

await backgroundPopupCreate(pop_ups)
const newIndex = iterateIndex(numberOfEvents, currentIndex)
const updatedSession = updateStorage(userSession, {
current_index: newIndex,
...(newIndex === 0 && { project_id: 0 })
})
await storage.set("arebyte-audience-session", updatedSession)
const newIndex = iterateIndex(numberOfEvents, user.current_index)

const response = await fetchStrapiContent<User>(
`api/users/${userSession.id}`,
"PUT",
userSession.jwt,
JSON.stringify({
current_index: newIndex,
...(newIndex === 0 && { project_id: 0 })
})
)
if (response.error) console.error(response.error)
} else {
const publicIndex: number = await storage.get(
"arebyte-public-index"
Expand Down
6 changes: 3 additions & 3 deletions src/utils/setEventAlarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ export default async function setEventAlarm(
eventMinute: number
) {
await browser.alarms.clear("sequence-alarm")
await browser.alarms.onAlarm.removeListener(eventAlarmListener)
browser.alarms.onAlarm.removeListener(eventAlarmListener)

await browser.alarms.create("sequence-alarm", {
browser.alarms.create("sequence-alarm", {
periodInMinutes: 1440,
when: calculateCountDown(eventHour, eventMinute)
})
await browser.alarms.onAlarm.addListener(eventAlarmListener)
browser.alarms.onAlarm.addListener(eventAlarmListener)
try {
const newAlarm = await browser.alarms.get("sequence-alarm")

Expand Down
Loading