Repository URL to install this package:
|
Version:
0.0.11 ▾
|
import * as tslib_1 from "tslib";
import { isArray, isObj, isSafe } from 'exotic';
import { action, observable, computed } from 'xmobx/mobx';
import { ObservableContainer } from '@skava/packages/libraries/observable-container';
/**
* @todo @@packages move
*/
// import { approvalContainer } from '@skava/packages/pages/Orders/ApprovalSection/state'
// local
import { shippingContainer } from './container.shipping';
import { paymentsContainer } from './container.payments';
import { privacySectionContainer } from './container.privacy';
import { toSecurityQuestion } from './transform';
import { fetchUserLiteProfileBinding } from './bindings';
/**
* @see src/uxui/templates/Address/Item/Add/typings
*/
/**
* @todo @fixme @@graphql @@haircut address changes
*/
const isShippingAddress = (address) => address.addressType === 'shippingAddress';
class UserContainer extends ObservableContainer {
constructor() {
super(...arguments);
/**
* @alias cardList
*/
this.cards = [];
/**
* @alias addresses
*/
this.addressList = [];
/**
* @todo should probably remove this nesting, flatten out
*/
this.userinfo = {
// seeing about this
userName: '',
firstName: '',
lastName: '',
userId: '',
email: '',
dateOfBirth: '',
gender: '',
userPhoto: '',
preferences: [],
// @deprecated use telephone
phoneNumber: '',
telephone: '',
};
this.security = {
verificationDetails: {},
};
this.accounts = {
teams: [],
};
this.customparams = {
sendWelcomeEmail: false,
};
}
async updateCards(response) {
this.cards = response.cards;
}
async updateAddresses(response) {
if (!response) {
console.warn('[updateAddresses] missing data');
}
else {
this.addressList = response.addressinfo;
}
}
async fetchUserLiteProfile() {
const response = await fetchUserLiteProfileBinding();
// const { paymentsContainer } = require('./container.payments')
// const cardResponse = await paymentsContainer.fetchCards()
// const addressResponse = await shippingContainer.fetchAddresses()
return response;
}
async updateAccounts(response) {
this.accounts = response.accounts;
}
async updateFrom(profile) {
this.userinfo = profile;
// await paymentsContainer.fetchCards()
// await shippingContainer.fetchAddresses()
// @todo fetch as a separate call...?
this.security = toSecurityQuestion(profile);
// await accountsContainer.fetchAccounts()
}
/**
* @todo @@haircut @graphql
* @todo @see RegisterContainer
*/
setNotifications(shouldNotify) {
this.customparams.sendWelcomeEmail = shouldNotify ? true : false;
}
// @todo - should be able to do `makeDefault` and works on payment + address
makeAddressDefault(addressItem) {
// addressItem.isDefault = true
shippingContainer.changeDefaultAddress(addressItem);
this.persistAddress();
}
/**
* @see /src/packages/pages/MyAccount/MyAccountWidget/AddressSection/index.tsx
* @todo fix these args, wrong
*/
async addAddress(addresses) {
// addressItem = serialized , address = address
// await shippingContainer.addAddress(addresses.serialized, addresses.address, addresses.onClose)
await shippingContainer.addAddress(addresses.serialized);
this.persistAddress();
}
/**
* @see /src/packages/pages/MyAccount/MyAccountWidget/AddressSection/index.tsx
* @todo fix these args, wrong
*/
async updateAddress(address) {
await shippingContainer.updateAddress(address.serialized, address.address);
this.persistAddress();
}
/**
* @see /src/packages/pages/MyAccount/MyAccountWidget/AddressSection/index.tsx
*/
removeAddress(address) {
shippingContainer.removeAddress(address);
this.persistAddress();
this.persist();
}
// // Payment
// @action.bound
// setPayment(userinfo: unknown) {
// this.cards = toAddressInfo(userinfo)
// // this.persist()
// }
// @todo - should be able to do `makeDefault` and works on payment + address
makePaymentDefault(paymentItem) {
paymentsContainer.changeDefaultCard(paymentItem);
}
updateCard(paymentItem) {
paymentsContainer.updateCardPayment(paymentItem);
}
// there is no update payment...
// updatePayment(paymentItem) {},
addPayment(card) {
this.persistPayments();
paymentsContainer.addCard(card);
}
removePayment(payment) {
paymentsContainer.deleteCard(payment.cardId);
this.persistPayments();
this.persist();
}
// @todo @@packages move
// @action.bound
// approveOrders(params: { [key: string]: any }) {
// approvalContainer.approveOrders(params)
// this.persistPayments()
// this.persist()
// }
// profile - user details and passwords
setEmail(email) {
this.userinfo.email = email;
}
setTelephone(phone) {
this.userinfo.telephone = phone;
}
setFirstName(name) {
this.userinfo.firstName = name;
}
setLastName(name) {
this.userinfo.lastName = name;
}
// addressOrPaymentItem
deleteUser() {
return privacySectionContainer.deleteUserAccount();
}
// ========= getters =========
getTeamId() {
const { teams } = this.accounts;
if (isArray(teams)) {
const teamId = isObj(teams[0]) && teams[0].id ? teams[0].id : '';
return teamId;
}
return '';
}
get isSocialLogin() {
// since list is not available in get profile response isSocialLogin is true for all the scenarios
// hence making isSocialLogin false
// const loginType = list => {
// if (list.name === 'skava' && this.userinfo.phoneNumber) {
// return true
// } else {
// return false
// }
// }
// const type = this.userinfo.list && this.userinfo.list.find(loginType)
// return !(type && type.name)
return false;
}
get hasLoaded() {
return this.email !== '' || this.telephone !== '' || !!this.userId;
}
get profile() {
return {
userId: this.userId,
firstName: this.firstName,
lastName: this.lastName,
email: this.email,
telephone: this.telephone,
};
}
get telephone() {
return this.userinfo.telephone;
}
get firstName() {
return this.userinfo.firstName;
}
get lastName() {
return this.userinfo.lastName;
}
get userId() {
return this.userinfo.userId;
}
get accountId() {
return this.accounts.accountid;
}
get teamId() {
return this.getTeamId();
}
get userName() {
return this.userinfo.userName;
}
/**
* @deprecated
*/
get addressinfo() {
return this.addressList;
}
get email() {
return this.userinfo.email || '';
}
get shippingAddresses() {
return this.addressinfo.filter(isShippingAddress);
}
get cardList() {
return this.cards;
}
get defaultAddress() {
if (isArray(this.addressinfo)) {
const address = this.addressinfo.find(key => key.isDefault);
// @todo no write on read
if (isObj(address)) {
if (isSafe(address.email) === false) {
address.email = this.email;
}
return address;
}
else {
console.warn('[user] did not have default address');
return {};
}
}
else {
console.warn('[user] did not have userinfo.addressinfo');
return {};
}
}
// ========= persist =========
persist() {
// this should be able to be done automatically in omnistore with the mappings
this.persistPayments();
this.persistAddress();
this.persistUserInfo();
this.persistSecurityInfo();
}
get storageKeys() {
return {
cardList: 'user_card_list',
addressList: 'user_address_list',
personal: 'user_personal',
security: 'user_security',
};
}
// any payments that are not cards?
persistPayments() {
console.warn('[core/user/container] persist not working');
// oneStorage.set(this.storageKeys.cardList, this.cards)
}
persistAddress() {
console.warn('[core/user/container] persist not working');
// oneStorage.set(this.storageKeys.addressList, this.addressinfo)
}
// this is profileinfo/personal?
persistUserInfo() {
console.warn('[core/user/container] persist not working');
// oneStorage.set(this.storageKeys.personal, this.userinfo)
}
persistSecurityInfo() {
console.warn('[core/user/container] persist not working');
// oneStorage.set(this.storageKeys.security, this.security)
}
}
UserContainer.debugName = 'User';
tslib_1.__decorate([
observable
], UserContainer.prototype, "cards", void 0);
tslib_1.__decorate([
observable
], UserContainer.prototype, "addressList", void 0);
tslib_1.__decorate([
observable
], UserContainer.prototype, "userinfo", void 0);
tslib_1.__decorate([
action
], UserContainer.prototype, "updateCards", null);
tslib_1.__decorate([
action
], UserContainer.prototype, "updateAddresses", null);
tslib_1.__decorate([
action
], UserContainer.prototype, "fetchUserLiteProfile", null);
tslib_1.__decorate([
action
], UserContainer.prototype, "updateAccounts", null);
tslib_1.__decorate([
action
], UserContainer.prototype, "updateFrom", null);
tslib_1.__decorate([
action.bound
], UserContainer.prototype, "setNotifications", null);
tslib_1.__decorate([
action.bound
], UserContainer.prototype, "makeAddressDefault", null);
tslib_1.__decorate([
action.bound
], UserContainer.prototype, "addAddress", null);
tslib_1.__decorate([
action.bound
], UserContainer.prototype, "updateAddress", null);
tslib_1.__decorate([
action.bound
], UserContainer.prototype, "removeAddress", null);
tslib_1.__decorate([
action.bound
], UserContainer.prototype, "makePaymentDefault", null);
tslib_1.__decorate([
action.bound
], UserContainer.prototype, "updateCard", null);
tslib_1.__decorate([
action.bound
], UserContainer.prototype, "addPayment", null);
tslib_1.__decorate([
action.bound
], UserContainer.prototype, "removePayment", null);
tslib_1.__decorate([
action.bound
], UserContainer.prototype, "setEmail", null);
tslib_1.__decorate([
action.bound
], UserContainer.prototype, "setTelephone", null);
tslib_1.__decorate([
action.bound
], UserContainer.prototype, "setFirstName", null);
tslib_1.__decorate([
action.bound
], UserContainer.prototype, "setLastName", null);
tslib_1.__decorate([
action.bound
], UserContainer.prototype, "deleteUser", null);
tslib_1.__decorate([
computed
], UserContainer.prototype, "hasLoaded", null);
tslib_1.__decorate([
computed
], UserContainer.prototype, "profile", null);
tslib_1.__decorate([
computed
], UserContainer.prototype, "telephone", null);
tslib_1.__decorate([
computed
], UserContainer.prototype, "firstName", null);
tslib_1.__decorate([
computed
], UserContainer.prototype, "lastName", null);
tslib_1.__decorate([
computed
], UserContainer.prototype, "userId", null);
tslib_1.__decorate([
computed
], UserContainer.prototype, "userName", null);
tslib_1.__decorate([
computed
], UserContainer.prototype, "addressinfo", null);
tslib_1.__decorate([
computed
], UserContainer.prototype, "email", null);
tslib_1.__decorate([
computed
], UserContainer.prototype, "shippingAddresses", null);
tslib_1.__decorate([
computed
], UserContainer.prototype, "cardList", null);
tslib_1.__decorate([
computed
], UserContainer.prototype, "defaultAddress", null);
tslib_1.__decorate([
action
], UserContainer.prototype, "persist", null);
const userContainer = new UserContainer();
export { userContainer };
export default userContainer;
//# sourceMappingURL=container.js.map