|
1 |
| -import {createEffect, createStore} from 'effector' |
2 |
| -import {persist} from 'effector-storage/session' |
3 |
| -import {AuthRequest} from '../model/AuthRequest.ts' |
4 |
| -import {AuthResponse} from '../model/AuthResponse.ts' |
| 1 | +import { createEffect, createStore } from 'effector' |
| 2 | +import { persist } from 'effector-storage/session' |
| 3 | +import { AuthRequest } from '../model/AuthRequest.ts' |
| 4 | +import { AuthResponse } from '../model/AuthResponse.ts' |
5 | 5 | import axios from 'axios'
|
6 |
| -import {AuthState, initialAuthState} from '@/entities' |
7 |
| -import {backendAuthikUrl} from '@/shared/api' |
| 6 | +import { AuthState, initialAuthState } from '@/entities' |
| 7 | +import { backendAuthikUrl } from '@/shared/api' |
8 | 8 |
|
9 | 9 | export const $authStore = createStore<AuthState>(initialAuthState)
|
10 |
| -export const $userIdStore = createStore<number | null>(null); |
| 10 | +export const $userIdStore = createStore<number | null>(null) |
11 | 11 |
|
12 |
| -export const $userRegisteredStore = createStore<boolean>(false); |
| 12 | +export const $userRegisteredStore = createStore<boolean>(false) |
13 | 13 |
|
14 |
| - |
15 |
| -persist({store: $authStore, key: 'authStore'}) |
16 |
| -persist({store: $userIdStore, key: 'userIdStore'}) |
17 |
| -persist({store: $userRegisteredStore, key: 'userRegisteredStore'}) |
| 14 | +persist({ store: $authStore, key: 'authStore' }) |
| 15 | +persist({ store: $userIdStore, key: 'userIdStore' }) |
| 16 | +persist({ store: $userRegisteredStore, key: 'userRegisteredStore' }) |
18 | 17 |
|
19 | 18 | export const authFx = createEffect<AuthRequest, AuthState, Error>({
|
20 |
| - handler: async (authReq) => { |
21 |
| - try { |
22 |
| - const response = await axios.put<AuthResponse>( |
23 |
| - `${backendAuthikUrl}/api/auth/telegram/web-app`, |
24 |
| - JSON.stringify(authReq), |
25 |
| - { |
26 |
| - method: 'PUT', |
27 |
| - headers: { |
28 |
| - 'Content-Type': 'application/json', |
29 |
| - }, |
30 |
| - }, |
31 |
| - ) |
32 |
| - return fillAuthState(response.data) |
33 |
| - } catch (error) { |
34 |
| - console.error(error) |
35 |
| - throw new Error('Auth failed') |
36 |
| - } |
37 |
| - }, |
| 19 | + handler: async (authReq) => { |
| 20 | + try { |
| 21 | + const response = await axios.put<AuthResponse>( |
| 22 | + `${backendAuthikUrl}/api/auth/telegram/web-app`, |
| 23 | + JSON.stringify(authReq), |
| 24 | + { |
| 25 | + method: 'PUT', |
| 26 | + headers: { |
| 27 | + 'Content-Type': 'application/json', |
| 28 | + }, |
| 29 | + }, |
| 30 | + ) |
| 31 | + return fillAuthState(response.data) |
| 32 | + } catch (error) { |
| 33 | + console.error(error) |
| 34 | + throw new Error('Auth failed') |
| 35 | + } |
| 36 | + }, |
38 | 37 | })
|
39 | 38 |
|
40 | 39 | const fillAuthState = (response: AuthResponse): AuthState => {
|
41 |
| - return { |
42 |
| - token: response.access, |
43 |
| - loading: false, |
44 |
| - tgId: '-1', |
45 |
| - isAuthenticated: true, |
46 |
| - error: null |
47 |
| - }; |
| 40 | + return { |
| 41 | + token: response.access, |
| 42 | + loading: false, |
| 43 | + tgId: '-1', |
| 44 | + isAuthenticated: true, |
| 45 | + error: null, |
| 46 | + } |
48 | 47 | }
|
49 | 48 |
|
50 |
| -export const $isAuthenticated = $authStore.map(state => state.isAuthenticated) |
| 49 | +export const $isAuthenticated = $authStore.map((state) => state.isAuthenticated) |
51 | 50 |
|
52 |
| -$authStore.on(authFx.doneData, (_, result) => result); |
53 |
| -$authStore.on(authFx.failData, (_, error) => ({...initialAuthState, loading: false, error: error.message})); |
| 51 | +$authStore.on(authFx.doneData, (_, result) => result) |
| 52 | +$authStore.on(authFx.failData, (_, error) => ({ |
| 53 | + ...initialAuthState, |
| 54 | + loading: false, |
| 55 | + error: error.message, |
| 56 | +})) |
54 | 57 |
|
55 | 58 | export const userIdFx = createEffect<number | null, number | null, Error>({
|
56 |
| - handler: async (x) => { |
57 |
| - if (x == null) |
58 |
| - throw Error("x is null") |
59 |
| - return x |
60 |
| - } |
61 |
| - } |
62 |
| -) |
| 59 | + handler: async (x) => { |
| 60 | + if (x == null) throw Error('x is null') |
| 61 | + return x |
| 62 | + }, |
| 63 | +}) |
63 | 64 |
|
64 | 65 | export const userRegisteredFx = createEffect<boolean, boolean, Error>({
|
65 |
| - handler: async (x) => { |
66 |
| - if (!x) |
67 |
| - throw Error("user not registered") |
68 |
| - return x |
69 |
| - } |
70 |
| - } |
71 |
| -) |
| 66 | + handler: async (x) => { |
| 67 | + if (!x) throw Error('user not registered') |
| 68 | + return x |
| 69 | + }, |
| 70 | +}) |
72 | 71 |
|
73 | 72 | $userIdStore.on(userIdFx.doneData, (_, result) => result)
|
74 | 73 | $userIdStore.on(userIdFx.failData, (_, __) => {
|
75 |
| - return null |
| 74 | + return null |
76 | 75 | })
|
77 | 76 | $userRegisteredStore.on(userRegisteredFx.doneData, (_, result) => result)
|
78 | 77 | $userRegisteredStore.on(userRegisteredFx.failData, (_, __) => {
|
79 |
| - return false |
| 78 | + return false |
80 | 79 | })
|
0 commit comments