Skip to content

Commit

Permalink
operators. Fixed favourite operators migration
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyco97 committed Sep 30, 2024
1 parent e740647 commit cc38046
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
8 changes: 4 additions & 4 deletions components/layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ export const Layout: FC<LayoutProps> = ({ children }) => {
store.dispatch.operators.setErrorMessage('')

try {
retrieveUserEndpoints()
const userEndpoints = await retrieveUserEndpoints()
retrieveGroups()
retrieveExtensions()
retrieveAvatars(authStore)
retrieveFavoriteOperators(authStore, operatorsStore?.operators)
retrieveFavoriteOperators(authStore, userEndpoints)
} catch (e) {
store.dispatch.operators.setErrorMessage('Cannot retrieve operators')
store.dispatch.operators.setOperatorsLoaded(true)
Expand All @@ -227,7 +227,7 @@ export const Layout: FC<LayoutProps> = ({ children }) => {
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
operatorsStore.isOperatorsLoaded,
operatorsStore?.isOperatorsLoaded,
firstRenderOperators,
profile?.macro_permissions?.presence_panel?.value,
])
Expand Down Expand Up @@ -488,7 +488,7 @@ export const Layout: FC<LayoutProps> = ({ children }) => {

store.dispatch.operators.updateExten(data[opName]?.number, extensionInformation)
})

useEventListener('phone-island-conversations', (data) => {
const opName = Object.keys(data)[0]
const conversations = data[opName].conversations
Expand Down
25 changes: 20 additions & 5 deletions lib/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,13 @@ export const retrieveUserEndpoints = async () => {
const endpoints = await getUserEndpointsAll()
store.dispatch.operators.setUserEndpoints(endpoints)
store.dispatch.operators.setUserEndpointsLoaded(true)
return endpoints
} catch (e) {
console.error(e)
store.dispatch.operators.setErrorMessage('Cannot retrieve user endpoints')
store.dispatch.operators.setOperatorsLoaded(true)
store.dispatch.operators.setLoading(false)
return null
}
}

Expand Down Expand Up @@ -276,13 +278,26 @@ export const retrieveFavoriteOperators = async (authStore: any, operatorObject:
const favoriteOperatorsLocalStorage =
loadPreference('favoriteOperators', authStore.username) || []

//check if the favoriteOperatorsLocalStorage is not empty
if (favoriteOperatorsLocalStorage.length > 0) {
// Cycle through all the favorite operators in local storage
for (const operatorName of favoriteOperatorsLocalStorage) {
const operatorSpeedDial = speedDials.find((speedDial: any) => speedDial.name === operatorName)
const mainExtension = operatorSpeedDial?.speeddial_num
if (mainExtension) {
let operatorRealName = operatorObject[operatorName]?.name
await addOperatorToFavorites(operatorName, mainExtension, operatorRealName)
// Check if the operator already exists in the speed dials
const operatorAlreadyExists = speedDials.some(
(speedDial: any) => speedDial?.name === operatorName,
)

// If the operator does not exist in the speed dials, add it
if (!operatorAlreadyExists) {
const operatorSpeedDial = operatorObject[operatorName]
if (operatorSpeedDial) {
const mainExtension = operatorSpeedDial?.endpoints?.mainextension[0]?.id
const operatorRealName = operatorSpeedDial?.name

if (mainExtension && operatorRealName) {
await addOperatorToFavorites(operatorName, mainExtension, operatorRealName)
}
}
}
}

Expand Down

0 comments on commit cc38046

Please sign in to comment.