Type user store
This commit is contained in:
parent
f5ebc6b249
commit
ad21b2e1fd
|
@ -61,7 +61,8 @@ export type Store = BaseStore &
|
||||||
ViewV2.Store &
|
ViewV2.Store &
|
||||||
NonPlus.Store &
|
NonPlus.Store &
|
||||||
Datasource.Store &
|
Datasource.Store &
|
||||||
Validation.Store & {
|
Validation.Store &
|
||||||
|
Users.Store & {
|
||||||
// TODO while typing the rest of stores
|
// TODO while typing the rest of stores
|
||||||
fetch: Writable<any>
|
fetch: Writable<any>
|
||||||
filter: Writable<any>
|
filter: Writable<any>
|
||||||
|
|
|
@ -1,8 +1,29 @@
|
||||||
import { writable, get, derived } from "svelte/store"
|
import { writable, get, derived, Writable, Readable } from "svelte/store"
|
||||||
import { helpers } from "@budibase/shared-core"
|
import { helpers } from "@budibase/shared-core"
|
||||||
|
import { Store as StoreContext } from "."
|
||||||
|
import { UIUser } from "@budibase/types"
|
||||||
|
|
||||||
export const createStores = () => {
|
interface UsersStore {
|
||||||
const users = writable([])
|
users: Writable<UIUser[]>
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DerivedUsersStore {
|
||||||
|
userCellMap: Readable<Record<string, UIUser>>
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ActionUserStore {
|
||||||
|
users: UsersStore["users"] & {
|
||||||
|
actions: {
|
||||||
|
updateUser: (user: UIUser) => void
|
||||||
|
removeUser: (sessionId: string) => void
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Store = UsersStore & DerivedUsersStore
|
||||||
|
|
||||||
|
export const createStores = (): UsersStore => {
|
||||||
|
const users = writable<UIUser[]>([])
|
||||||
|
|
||||||
const enrichedUsers = derived(users, $users => {
|
const enrichedUsers = derived(users, $users => {
|
||||||
return $users.map(user => ({
|
return $users.map(user => ({
|
||||||
|
@ -20,7 +41,7 @@ export const createStores = () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const deriveStores = context => {
|
export const deriveStores = (context: StoreContext): DerivedUsersStore => {
|
||||||
const { users, focusedCellId } = context
|
const { users, focusedCellId } = context
|
||||||
|
|
||||||
// Generate a lookup map of cell ID to the user that has it selected, to make
|
// Generate a lookup map of cell ID to the user that has it selected, to make
|
||||||
|
@ -28,7 +49,7 @@ export const deriveStores = context => {
|
||||||
const userCellMap = derived(
|
const userCellMap = derived(
|
||||||
[users, focusedCellId],
|
[users, focusedCellId],
|
||||||
([$users, $focusedCellId]) => {
|
([$users, $focusedCellId]) => {
|
||||||
let map = {}
|
let map: Record<string, UIUser> = {}
|
||||||
$users.forEach(user => {
|
$users.forEach(user => {
|
||||||
const cellId = user.gridMetadata?.focusedCellId
|
const cellId = user.gridMetadata?.focusedCellId
|
||||||
if (cellId && cellId !== $focusedCellId) {
|
if (cellId && cellId !== $focusedCellId) {
|
||||||
|
@ -44,10 +65,10 @@ export const deriveStores = context => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createActions = context => {
|
export const createActions = (context: StoreContext): ActionUserStore => {
|
||||||
const { users } = context
|
const { users } = context
|
||||||
|
|
||||||
const updateUser = user => {
|
const updateUser = (user: UIUser) => {
|
||||||
const $users = get(users)
|
const $users = get(users)
|
||||||
if (!$users.some(x => x.sessionId === user.sessionId)) {
|
if (!$users.some(x => x.sessionId === user.sessionId)) {
|
||||||
users.set([...$users, user])
|
users.set([...$users, user])
|
||||||
|
@ -60,7 +81,7 @@ export const createActions = context => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const removeUser = sessionId => {
|
const removeUser = (sessionId: string) => {
|
||||||
users.update(state => {
|
users.update(state => {
|
||||||
return state.filter(x => x.sessionId !== sessionId)
|
return state.filter(x => x.sessionId !== sessionId)
|
||||||
})
|
})
|
|
@ -2,3 +2,4 @@ export * from "./columns"
|
||||||
export * from "./datasource"
|
export * from "./datasource"
|
||||||
export * from "./table"
|
export * from "./table"
|
||||||
export * from "./view"
|
export * from "./view"
|
||||||
|
export * from "./user"
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
import { User } from "@budibase/types"
|
||||||
|
|
||||||
|
export interface UIUser extends User {
|
||||||
|
sessionId: string
|
||||||
|
gridMetadata?: { focusedCellId?: string }
|
||||||
|
}
|
Loading…
Reference in New Issue