Skip to content

Commit

Permalink
feat(com-pwa): user context consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm authored and AliMD committed May 1, 2023
1 parent c6b2ce1 commit a8e1f87
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
11 changes: 10 additions & 1 deletion uniquely/com-pwa/src/manager/context-provider/sign-in.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {contextProvider, serverContextConsumer} from '@alwatr/context';
import {simpleHashNumber} from '@alwatr/math';

import {config} from '../../config.js';

Expand All @@ -10,9 +11,17 @@ export const signInContextConsumer = serverContextConsumer<AlwatrServiceResponse
config.fetchContextOptions,
);

signInContextConsumer.subscribe(() => {
if (signInContextConsumer.getState().target === 'complete') {
const user = signInContextConsumer.getResponse()?.data;
if (user != null) {
contextProvider.setValue<ComUser>('user_context', user);
}
}
});

export const signIn = (phoneNumber: number, token: string): void => {
signInContextConsumer.request({
url: `${config.api}/auth/${phoneNumber}-${token}`,
url: `${config.api}/storage/${simpleHashNumber(phoneNumber)}-${token}`,
});
};
23 changes: 6 additions & 17 deletions uniquely/com-pwa/src/manager/context-provider/user.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import {serverContextConsumer} from '@alwatr/context';
import {simpleHashNumber} from '@alwatr/math';
import {contextProvider} from '@alwatr/context';
import {ComUser} from '@alwatr/type/customer-order-management.js';

import {config} from '../../config.js';

import type {AlwatrDocumentStorage} from '@alwatr/type';
import type {ComUser} from '@alwatr/type/customer-order-management.js';

export const userStorageContextConsumer = serverContextConsumer<AlwatrDocumentStorage<ComUser>>(
'user_storage_context',
config.fetchContextOptions,
);

export const signIn = (phoneNumber: number, token: string): void => {
userStorageContextConsumer.request({
url: `${config.api}/storage/${simpleHashNumber(phoneNumber)}-${token}`,
});
};
const user = localStorage.getItem('user-context');
if (user != null) {
contextProvider.setValue<ComUser>('user_context', JSON.parse(user));
}
12 changes: 10 additions & 2 deletions uniquely/com-pwa/src/ui/alwatr-pwa.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {contextConsumer} from '@alwatr/context';
import {html, customElement, nothing} from '@alwatr/element';
import '@alwatr/font/vazirmatn.css';
import {AlwatrPwaElement} from '@alwatr/pwa-helper/pwa-element.js';
Expand All @@ -9,6 +10,8 @@ import '@alwatr/ui-kit/style/theme/palette-270.css';
import './stuff/app-footer.js';
import {topAppBarContextProvider} from '../manager/context.js';

import type {ComUser} from '@alwatr/type/customer-order-management.js';

declare global {
interface HTMLElementTagNameMap {
'alwatr-pwa': AlwatrPwa;
Expand Down Expand Up @@ -61,7 +64,7 @@ class AlwatrPwa extends AlwatrPwaElement {
protected _renderPageOrder(routeContext: RouteContext): unknown {
import('./page/order.js');
topAppBarContextProvider.setValue({headlineKey: 'loading'});
const orderId = routeContext.sectionList[1] || 'new';
const orderId = routeContext.sectionList[1] ?? 'new';
return html`<alwatr-page-order .orderId=${orderId} unresolved>...</alwatr-page-order>`;
}

Expand All @@ -87,7 +90,12 @@ class AlwatrPwa extends AlwatrPwaElement {
protected _checkSignedIn(routeContext: RouteContext): void {
const routeId = this._routesConfig.routeId(routeContext);
this._logger.logMethodArgs?.('_checkSignedIn', {routeId});
if (localStorage.getItem('user-token') == null && routeId !== 'sign-in' && routeId !== 's' && routeId !== '') {
if (
contextConsumer<ComUser>('user_context').getValue() == null &&
routeId !== 'sign-in' &&
routeId !== 's' &&
routeId !== ''
) {
redirect({sectionList: ['sign-in']});
}
}
Expand Down

0 comments on commit a8e1f87

Please sign in to comment.