From 424d06e7bb77f6df3ee9dc162c938e0df6d3cf5d Mon Sep 17 00:00:00 2001 From: YanLobat Date: Fri, 21 Aug 2020 15:08:07 +0300 Subject: [PATCH] remove user via logout fetch users on mount and table calculations --- src/components/App/index.tsx | 6 ++++-- src/models/app/index.ts | 5 ++++- src/models/app/init.ts | 11 ++++++++++- src/models/auth/init.ts | 16 +++++++++++++++- src/models/init.ts | 3 ++- src/models/tables/index.ts | 30 ++++++++++++++++++++++++++++-- src/models/tables/init.ts | 4 ++-- src/models/users/index.ts | 14 ++++++++++++++ 8 files changed, 79 insertions(+), 10 deletions(-) diff --git a/src/components/App/index.tsx b/src/components/App/index.tsx index 2890e8f..a23c3db 100644 --- a/src/components/App/index.tsx +++ b/src/components/App/index.tsx @@ -1,13 +1,15 @@ import * as React from 'react' -import { useStore } from 'effector-react' +import { useStore, useGate } from 'effector-react' import './App.css' import { Theater } from '../Theater' import { Auth } from '../Auth' -import {$user} from '../../models/auth' +import { $user } from '../../models/auth' +import {AppGate} from '../../models/app' export const App = () => { + useGate(AppGate) const {email} = useStore($user) return email ? () : () } \ No newline at end of file diff --git a/src/models/app/index.ts b/src/models/app/index.ts index 0ad8c33..e73b6bf 100644 --- a/src/models/app/index.ts +++ b/src/models/app/index.ts @@ -1,5 +1,8 @@ import {createEffect} from 'effector' +import { createGate } from 'effector-react' import { Config } from './types' -export const initAppFx = createEffect() \ No newline at end of file +export const initAppFx = createEffect() + +export const AppGate = createGate() \ No newline at end of file diff --git a/src/models/app/init.ts b/src/models/app/init.ts index e2e6e26..9e28b73 100644 --- a/src/models/app/init.ts +++ b/src/models/app/init.ts @@ -1,9 +1,12 @@ import { initializeApp } from 'firebase' +import { forward } from 'effector' import { - initAppFx + initAppFx, AppGate } from './' +import { fetchUsersFx } from '../users' + import { appId, projectId, @@ -11,6 +14,7 @@ import { messagingSenderId } from '../../../config.json' + initAppFx.use(async ({ appId, projectId, @@ -33,4 +37,9 @@ initAppFx({ projectId, apiKey, messagingSenderId +}) + +forward({ + from: AppGate.open, + to: fetchUsersFx }) \ No newline at end of file diff --git a/src/models/auth/init.ts b/src/models/auth/init.ts index 73f0a87..551815a 100644 --- a/src/models/auth/init.ts +++ b/src/models/auth/init.ts @@ -15,7 +15,9 @@ import { import { addUserFx, - updateUsersTableFx + updateUsersTableFx, + deleteUserFx, + $firebaseUsers } from '../users' const gProvider = new auth.GoogleAuthProvider() @@ -77,4 +79,16 @@ forward({ } }), to: signUpViaEmailFx +}) + +sample({ + source: $firebaseUsers, + clock: logout, + //@ts-ignore + fn: (users, email) => Object.keys(users).find((id) => { + if (email === users[id].email) { + return id + } + }), + target: deleteUserFx }) \ No newline at end of file diff --git a/src/models/init.ts b/src/models/init.ts index cb7d794..8d14fc7 100644 --- a/src/models/init.ts +++ b/src/models/init.ts @@ -1,3 +1,4 @@ import './app/init' import './auth/init' -import './users/init' \ No newline at end of file +import './users/init' +import './tables/init' \ No newline at end of file diff --git a/src/models/tables/index.ts b/src/models/tables/index.ts index 958ea2a..3aa90bd 100644 --- a/src/models/tables/index.ts +++ b/src/models/tables/index.ts @@ -1,3 +1,29 @@ -import {createStore} from 'effector' +import {createStore, createEvent} from 'effector' -export const $currentConnectID = createStore('first-table') \ No newline at end of file +export const updateCurrentIDandStage = createEvent<{ ID: string, stage: number }>() + +export const $currentConnectID = createStore('first-table') + +export const $tableIDs = createStore([ + 'first-table', + 'second-table', + 'third-table', + 'fourth-table', + 'fifth-table', + 'sixth-table', + 'seventh-table', + 'eighth-table', + 'ninth-table', + 'tenth-table', + 'eleventh-table', + 'twelfth-table', + 'thirteenth-table', + 'fourteenth-table', + 'fifteenth-table', + 'left-top-table', + 'right-top-table', + 'left-bottom-table', + 'right-bottom-table' +]) + +export const $stage = createStore(2) \ No newline at end of file diff --git a/src/models/tables/init.ts b/src/models/tables/init.ts index c9b761a..bba9a9d 100644 --- a/src/models/tables/init.ts +++ b/src/models/tables/init.ts @@ -5,11 +5,11 @@ import { $currentConnectID, $tableIDs, $stage -} from './' +} from './index' import { $tableUsers -} from '../users' +} from '../users/index' sample({ source: { diff --git a/src/models/users/index.ts b/src/models/users/index.ts index 71eedbe..66850a0 100644 --- a/src/models/users/index.ts +++ b/src/models/users/index.ts @@ -25,4 +25,18 @@ export const $usersByEmail = $firebaseUsers.map((fUsers) => { ...usersByEmail } }, {}) +}) + +export const $tableUsers = $firebaseUsers.map((fUsers) => { + return Object.keys(fUsers).reduce((tableUsers, id) => { + const tableID = fUsers[id].tableID; + if (tableUsers[tableID] !== undefined) { + tableUsers[tableID].push(fUsers[id]); + return tableUsers; + } + return { + ...tableUsers, + [tableID]: [fUsers[id]] + } + }, {}) }) \ No newline at end of file