Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion src/models/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Config } from './types'

export const initAppFx = createEffect<Config, unknown>()

export const showErrorFx = createEffect<string, unknown>()

export const AppGate = createGate()

export const Route = createGate<{name: string}>()
export const Route = createGate<{ name: string }>()
6 changes: 5 additions & 1 deletion src/models/app/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { initializeApp } from 'firebase'
import { forward } from 'effector'

import {
initAppFx, AppGate, Route
initAppFx, showErrorFx, AppGate, Route
} from './'

import { fetchUsersFx } from '../users'
Expand Down Expand Up @@ -34,6 +34,10 @@ initAppFx.use(async ({
})
})

showErrorFx.use((text) => {
alert(text)
})

initAppFx({
appId,
projectId,
Expand Down
67 changes: 48 additions & 19 deletions src/models/auth/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { forward, sample, guard } from 'effector'
import { forward, sample, guard, merge } from 'effector'
import { auth } from 'firebase'

import {
Expand All @@ -19,12 +19,20 @@ import {

import {
addUserFx,
updateUsersTableFx,
deleteUserFx,
$firebaseUsers,
$usersByEmail
$usersByEmail,
$usersCount
} from '../users'

import {
$tableCapacity,
$tablesCount
} from '../tables'

import {
showErrorFx
} from '../app'

import { User } from './types'

const gProvider = new auth.GoogleAuthProvider()
Expand Down Expand Up @@ -80,14 +88,33 @@ $signInForm
[fieldName]: value
}))

forward({
from: gSignIn,
to: manageGmailProviderFx
const $canUserEnter = sample({
source: {
count: $tablesCount,
capacity: $tableCapacity
},
clock: $usersCount,
fn: ({ count, capacity }, usersCount) => count * capacity > usersCount
})

forward({
from: signIn,
to: manageEmailProviderFx
const $cantUserEnter = $canUserEnter.map((can) => !can)

guard({
source: gSignIn,
filter: $canUserEnter,
target: manageGmailProviderFx
})

guard({
source: signIn,
filter: $canUserEnter,
target: manageEmailProviderFx
})

guard({
source: merge([signIn, gSignIn]).map(() => 'Theater is full! Try to login later.'),
filter: $cantUserEnter,
target: showErrorFx
})

forward({
Expand All @@ -100,17 +127,19 @@ forward({
to: signUpViaEmailFx
})

const foundPrevLogInUser = sample({
source: $usersByEmail,
clock: [
manageGmailProviderFx.doneData,
signUpViaEmailFx.doneData,
manageEmailProviderFx.doneData
],
fn: (users, user) => users[user.email]
})

sample({
source: guard({
source: sample({
source: $usersByEmail,
clock: [
manageGmailProviderFx.doneData,
signUpViaEmailFx.doneData,
manageEmailProviderFx.doneData
],
fn: (users, user) => users[user.email]
}),
source: foundPrevLogInUser,
filter: Boolean
}),
fn: (user) => user.id!,
Expand Down
6 changes: 6 additions & 0 deletions src/models/tables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {createStore, createEvent} from 'effector'

export const updateCurrentIDandStage = createEvent<{ ID: string, stage: number }>()

export const updateStage = updateCurrentIDandStage.map(({ stage }) => stage)

export const $currentConnectID = createStore<string>('first-table')

export const $tableIDs = createStore<string[]>([
Expand All @@ -26,4 +28,8 @@ export const $tableIDs = createStore<string[]>([
'right-bottom-table'
])

export const $tablesCount = $tableIDs.map((ids) => ids.length)

export const $tableCapacity = createStore(6)

export const $stage = createStore(2)
21 changes: 14 additions & 7 deletions src/models/tables/init.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import {forward, sample} from 'effector'
import {forward, sample, guard} from 'effector'

import {
updateCurrentIDandStage,
updateStage,
$currentConnectID,
$tableIDs,
$stage
} from './index'
$stage,
$tableCapacity
} from './'

import {
$tableUsers
} from '../users/index'
} from '../users'

sample({
source: {
Expand Down Expand Up @@ -37,7 +39,12 @@ forward({
to: $currentConnectID
})

forward({
from: updateCurrentIDandStage.map(({stage}) => stage),
to: $stage
guard({
source: updateStage,
filter: sample({
source: $tableCapacity,
clock: updateStage,
fn: (capacity, stage) => stage <= capacity
}),
target: $stage
})
6 changes: 5 additions & 1 deletion src/models/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ export const $tableUsers = $firebaseUsers.map((fUsers) => {
[tableID]: [fUsers[id]]
}
}, {})
})
})

export const $usersCount = $firebaseUsers.map((fUsers) =>
Object.keys(fUsers).length
)
49 changes: 40 additions & 9 deletions src/models/users/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
fetchUsersFx,
deleteUserFx,
updateUsersTableFx,
$firebaseUsers
$firebaseUsers,
$usersByEmail,
$tableUsers
} from './'

import {
Expand All @@ -20,9 +22,14 @@ import {
} from '../auth'

import {
$currentConnectID
$currentConnectID,
$tableCapacity
} from '../tables'

import {
showErrorFx
} from '../app'

const usersRef = database().ref('users/');
usersRef.on('value', (snapshot) => updateUsers(snapshot.val() === null ? {} : snapshot.val()))

Expand Down Expand Up @@ -65,16 +72,40 @@ sample({
target: addUserFx
})

sample({
source: $user,
forward({
from: addUserFx.doneData,
to: fetchUsersFx
})

const userChangeTable = sample({
source: {
user: $user,
usersByEmail: $usersByEmail,
tableUsers: $tableUsers,
tableCapacity: $tableCapacity
},
clock: changeUserTable,
fn: ({id}, tableID) => ({
id, tableID
fn: ({user, usersByEmail, tableUsers, tableCapacity}, tableID) => {
const id = usersByEmail[user.email!].id!
const canBeUpdated = tableUsers[tableID] === undefined || tableUsers[tableID].length < tableCapacity
return {id, tableID, canBeUpdated}
}
})

forward({
from: userChangeTable.filterMap(({ id, tableID, canBeUpdated }) => {
if (canBeUpdated) {
return { id, tableID }
}
}),
target: updateUsersTableFx
to: updateUsersTableFx
})

forward({
from: addUserFx.doneData,
to: fetchUsersFx
from: userChangeTable.filterMap(({ canBeUpdated }) => {
if (!canBeUpdated) {
return 'Table is full. You shall not pass!'
}
}),
to: showErrorFx
})