-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1722 from Talej/pos
Standalone POS
- Loading branch information
Showing
23 changed files
with
2,910 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { observable, computed } from 'mobx'; | ||
import BaseModel from './BaseModel'; | ||
|
||
export enum PricedIn { | ||
Sats = 'sats', | ||
Fiat = 'fiat' | ||
} | ||
|
||
export enum ProductStatus { | ||
Active = 'active', | ||
Inactive = 'inactive' | ||
} | ||
|
||
export default class Product extends BaseModel { | ||
@observable public id: string; | ||
@observable public name: string; | ||
@observable public sku: string; | ||
@observable public pricedIn: PricedIn; | ||
@observable public price: number; | ||
@observable public category: string; | ||
@observable public status: ProductStatus; | ||
|
||
@computed public get model(): string { | ||
return 'Product'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { observable, computed } from 'mobx'; | ||
import BaseModel from './BaseModel'; | ||
|
||
export default class ProductCategory extends BaseModel { | ||
@observable public id: string; | ||
@observable public name: string; | ||
|
||
@computed public get model(): string { | ||
return 'ProductCategory'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { action, observable } from 'mobx'; | ||
import Product from '../models/Product'; | ||
import ProductCategory from '../models/ProductCategory'; | ||
import EncryptedStorage from 'react-native-encrypted-storage'; | ||
|
||
const CATEGORY_KEY = 'zeus-product-categories'; | ||
const PRODUCT_KEY = 'zeus-products'; | ||
|
||
export default class InventoryStore { | ||
@observable categories: Array<ProductCategory> = []; | ||
@observable products: Array<Product> = []; | ||
@observable public loading = false; | ||
|
||
@action | ||
public async getInventory() { | ||
this.loading = true; | ||
try { | ||
// Retrieve the categories | ||
const categories = await EncryptedStorage.getItem(CATEGORY_KEY); | ||
if (categories) { | ||
this.categories = JSON.parse(categories) || []; | ||
} | ||
// Retrieve the products | ||
const products = await EncryptedStorage.getItem(PRODUCT_KEY); | ||
if (products) { | ||
this.products = JSON.parse(products) || []; | ||
} | ||
} catch (error) { | ||
console.error('Could not load inventory', error); | ||
} finally { | ||
this.loading = false; | ||
} | ||
|
||
return { | ||
categories: this.categories, | ||
products: this.products | ||
}; | ||
} | ||
|
||
@action | ||
public async setCategories(categories: string) { | ||
this.loading = true; | ||
await EncryptedStorage.setItem(CATEGORY_KEY, categories); | ||
this.loading = false; | ||
return categories; | ||
} | ||
|
||
@action | ||
public updateCategories = async (newCategory: ProductCategory) => { | ||
const { categories: existingCategories } = await this.getInventory(); | ||
|
||
const found = existingCategories.find((c) => c.id === newCategory.id); | ||
if (found) { | ||
const { products } = await this.getInventory(); | ||
const updatedProducts = products | ||
.filter((product) => product.category === found.name) | ||
.map( | ||
(product) => | ||
({ ...product, category: newCategory.name } as Product) | ||
); | ||
if (updatedProducts.length > 0) { | ||
await this.updateProducts(updatedProducts); | ||
} | ||
|
||
found.name = newCategory.name; | ||
} else { | ||
existingCategories.push(newCategory); | ||
} | ||
|
||
await this.setCategories(JSON.stringify(existingCategories)); | ||
|
||
const { categories } = await this.getInventory(); | ||
return categories; | ||
}; | ||
|
||
@action | ||
public deleteCategory = async (categoryId: string) => { | ||
const { categories: existingCategories } = await this.getInventory(); | ||
|
||
const idx = existingCategories.findIndex((c) => c.id === categoryId); | ||
if (idx > -1) { | ||
existingCategories.splice(idx, 1); | ||
await this.setCategories(JSON.stringify(existingCategories)); | ||
|
||
const { categories } = await this.getInventory(); | ||
return categories; | ||
} | ||
}; | ||
|
||
@action | ||
public async setProducts(products: string) { | ||
this.loading = true; | ||
await EncryptedStorage.setItem(PRODUCT_KEY, products); | ||
this.loading = false; | ||
return products; | ||
} | ||
|
||
@action | ||
public updateProducts = async (newProducts: Product[]) => { | ||
const { products: existingProducts } = await this.getInventory(); | ||
|
||
newProducts.forEach((newProduct) => { | ||
const found = existingProducts.find((c) => c.id === newProduct.id); | ||
if (found) { | ||
found.name = newProduct.name; | ||
found.sku = newProduct.sku; | ||
found.price = newProduct.price; | ||
found.pricedIn = newProduct.pricedIn; | ||
found.category = newProduct.category; | ||
found.status = newProduct.status; | ||
} else { | ||
existingProducts.push(newProduct); | ||
} | ||
}); | ||
|
||
await this.setProducts(JSON.stringify(existingProducts)); | ||
// ensure we get the enhanced settings set | ||
const { products } = await this.getInventory(); | ||
return products; | ||
}; | ||
|
||
@action | ||
public deleteProduct = async (productIds: string[]) => { | ||
const { products: existingProducts } = await this.getInventory(); | ||
|
||
const updatedProducts = existingProducts.filter( | ||
(p) => !productIds.includes(p.id) | ||
); | ||
await this.setProducts(JSON.stringify(updatedProducts)); | ||
|
||
const { products } = await this.getInventory(); | ||
return products; | ||
}; | ||
} |
Oops, something went wrong.