PR feedback

This commit is contained in:
Dean 2024-03-15 09:09:44 +00:00
parent 9755d40203
commit 4949ea1bcd
9 changed files with 44 additions and 53 deletions

View File

@ -18,7 +18,7 @@
auth,
groups,
licensing,
enriched as enrichedApps,
enrichedApps,
} from "stores/portal"
import { goto } from "@roxi/routify"
import { AppStatus } from "constants"

View File

@ -21,8 +21,22 @@
{size}
on:click={async e => {
e.stopPropagation()
const userAppFavourites = new Set([...($auth.user.appFavourites || [])])
let processedAppIds = []
if ($auth.user.appFavourites && app?.appId) {
if (userAppFavourites.has(app.appId)) {
userAppFavourites.delete(app.appId)
} else {
userAppFavourites.add(app.appId)
}
processedAppIds = [...userAppFavourites]
} else {
processedAppIds = [app.appId]
}
await auth.updateSelf({
appFavourites: [app?.appId],
appFavourites: processedAppIds,
})
}}
disabled={!app}

View File

@ -1,10 +1,6 @@
<script>
import { params, goto } from "@roxi/routify"
import {
auth,
sideBarCollapsed,
enriched as enrichedApps,
} from "stores/portal"
import { auth, sideBarCollapsed, enrichedApps } from "stores/portal"
import AppRowContext from "components/start/AppRowContext.svelte"
import FavouriteAppButton from "../FavouriteAppButton.svelte"
import {

View File

@ -1,9 +1,5 @@
<script>
import {
sideBarCollapsed,
enriched as enrichedApps,
auth,
} from "stores/portal"
import { sideBarCollapsed, enrichedApps, auth } from "stores/portal"
import { params, goto } from "@roxi/routify"
import NavItem from "components/common/NavItem.svelte"
import NavHeader from "components/common/NavHeader.svelte"

View File

@ -25,7 +25,7 @@
admin,
licensing,
environment,
enriched as enrichedApps,
enrichedApps,
} from "stores/portal"
import { goto } from "@roxi/routify"
import AppRow from "components/start/AppRow.svelte"

View File

@ -141,7 +141,7 @@ export class AppsStore extends BudiStore {
export const appsStore = new AppsStore()
// Centralise any logic that enriches the apps list
export const enriched = derived([appsStore, auth], ([$store, $auth]) => {
export const enrichedApps = derived([appsStore, auth], ([$store, $auth]) => {
const enrichedApps = $store.apps
? $store.apps.map(app => ({
...app,

View File

@ -3,7 +3,7 @@ import { writable } from "svelte/store"
export { organisation } from "./organisation"
export { users } from "./users"
export { admin } from "./admin"
export { appsStore, enriched } from "./apps"
export { appsStore, enrichedApps } from "./apps"
export { email } from "./email"
export { auth } from "./auth"
export { oidc } from "./oidc"

View File

@ -59,7 +59,6 @@ import sdk from "../../sdk"
import { builderSocket } from "../../websockets"
import { sdk as sharedCoreSDK } from "@budibase/shared-core"
import * as appMigrations from "../../appMigrations"
import { cloneDeep } from "lodash"
// utility function, need to do away with this
async function getLayouts() {
@ -684,7 +683,7 @@ export async function duplicateApp(
const createRequest = {
roleId: ctx.roleId,
user: {
...cloneDeep(ctx.user),
...ctx.user,
_id: dbCore.getGlobalIDFromUserMetadataID(ctx.user._id || ""),
},
request: {

View File

@ -111,6 +111,9 @@ export async function getSelf(ctx: any) {
}
export const syncAppFavourites = async (processedAppIds: string[]) => {
if (processedAppIds.length === 0) {
return []
}
const apps = await fetchAppsByIds(processedAppIds)
return apps?.reduce((acc: string[], app) => {
const id = app.appId.replace(dbCore.APP_DEV_PREFIX, "")
@ -127,35 +130,27 @@ export const fetchAppsByIds = async (processedAppIds: string[]) => {
)
}
const processUserAppFavourites = (
const processUserAppFavourites = async (
user: User,
appIdRequest: string[] | undefined
update: UpdateSelfRequest
) => {
const userAppFavourites = user.appFavourites || []
const appFavouritesRequest = appIdRequest || []
const appFavouritesUpdated = [
...new Set(userAppFavourites.concat(appFavouritesRequest)),
]
if (!("appFavourites" in update)) {
// Ignore requests without an explicit update to favourites.
return
}
// Process state
let processedAppIds: string[]
if (userAppFavourites.length) {
const req = new Set(appFavouritesRequest)
const updated = new Set(userAppFavourites)
for (const element of req) {
if (updated.has(element)) {
// Subtract/Toggle any existing ones
updated.delete(element)
} else {
// Add new appIds
updated.add(element)
const userAppFavourites = user.appFavourites || []
const requestAppFavourites = new Set(update.appFavourites || [])
const containsAll = userAppFavourites.every(v => requestAppFavourites.has(v))
if (containsAll && requestAppFavourites.size === userAppFavourites.length) {
// Ignore request if the outcome will have no change
return
}
}
processedAppIds = [...updated]
} else {
processedAppIds = [...appFavouritesUpdated]
}
return processedAppIds
// Clean up the request by purging apps that no longer exist.
const syncedAppFavourites = await syncAppFavourites([...requestAppFavourites])
return syncedAppFavourites
}
export async function updateSelf(
@ -164,16 +159,7 @@ export async function updateSelf(
const update = ctx.request.body
let user = await userSdk.db.getUser(ctx.user._id!)
let requestAppFavourites: string[] = [...(update.appFavourites || [])]
let updatedAppFavourites: string[] | undefined
if ("appFavourites" in update) {
const appIds: string[] = processUserAppFavourites(
user,
requestAppFavourites
)
updatedAppFavourites = await syncAppFavourites(appIds)
}
const updatedAppFavourites = await processUserAppFavourites(user, update)
user = {
...user,