Skip to content

Commit

Permalink
Merge pull request #499 from entur/improve-custom-url-backend
Browse files Browse the repository at this point in the history
Improve custom url backend
  • Loading branch information
magnusrand authored Nov 3, 2021
2 parents d5f47fc + 89d4fc3 commit 3e33631
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const scheduledDeleteOfDocumentsSetToBeDeleted = region('us-central1')
const batch = firestore().batch()
firestore()
.collection('settings')
.where('delete', '==', true)
.where('isScheduledForDelete', '==', true)
.get()
.then((querySnapshot) => {
querySnapshot.forEach((doc) => {
Expand Down
9 changes: 8 additions & 1 deletion src/containers/MyBoards/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ function sortBoard(boards: BoardProps[]): BoardProps[] {
})
}

const filterBoards = (boards: BoardProps[]): BoardProps[] =>
boards.filter((board) => !board.data.isScheduledForDelete)

const MyBoards = ({ history }: Props): JSX.Element | null => {
const [boards, setBoards] = useState<DocumentData>()
const user = useUser()
Expand All @@ -51,7 +54,11 @@ const MyBoards = ({ history }: Props): JSX.Element | null => {
id: docSnapshot.id,
} as BoardProps),
)
setBoards(updatedBoards.length ? sortBoard(updatedBoards) : [])
setBoards(
updatedBoards.length
? sortBoard(filterBoards(updatedBoards))
: [],
)
},
error: () => setBoards([]),
})
Expand Down
34 changes: 19 additions & 15 deletions src/services/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,31 @@ export const uploadLogo = async (
)
}

export const copySettingsToNewId = (
export const copySettingsToNewId = async (
newDocId: string,
settings: Settings | null,
): Promise<boolean> => {
if (!settings) return false

const newDocRef: DocumentReference = getSettings(newDocId)

return getDoc(newDocRef)
.then((document) => {
if (document.exists()) {
return false
} else {
if (settings) {
createSettingsWithId(settings, newDocId)
return true
} else {
return false
}
try {
const document = await getDoc(newDocRef)
if (document.exists()) {
if (document.data().isScheduledForDelete) {
await deleteDoc(newDocRef)
await createSettingsWithId(settings, newDocId)
return true
}
})
.catch(() => false)
return false
} else {
await createSettingsWithId(settings, newDocId)
return true
}
} catch {
return false
}
}

export const setIdToBeDeleted = (docId: string): Promise<void> =>
updateSingleSettingsField(docId, 'delete', true)
updateSingleSettingsField(docId, 'isScheduledForDelete', true)
1 change: 1 addition & 0 deletions src/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface Settings {
hideWalkInfo?: boolean
hideRealtimeData?: boolean
hiddenRealtimeDataLineRefs: string[]
isScheduledForDelete?: boolean
}

type Setter = (settings: Partial<Settings>) => void
Expand Down

0 comments on commit 3e33631

Please sign in to comment.