From a62315641acf130c65bb9e080556b638fddcab9b Mon Sep 17 00:00:00 2001 From: caturbgs Date: Thu, 16 Dec 2021 22:40:21 +0700 Subject: [PATCH] feat: adding try catch --- src/store/authentication.js | 1 - src/store/category.js | 73 +++++++++++++++++++---------- src/store/commission.js | 38 +++++++++------ src/store/membership.js | 93 +++++++++++++++++++++---------------- src/store/partner.js | 58 +++++++++++++++-------- src/store/product.js | 78 ++++++++++++++++++++----------- src/store/role.js | 28 ++++++++--- src/store/subcategory.js | 52 ++++++++++++++------- src/store/supplier.js | 61 ++++++++++++++++-------- src/store/transaction.js | 91 +++++++++++++++++++++++++----------- src/store/user.js | 6 ++- 11 files changed, 385 insertions(+), 194 deletions(-) diff --git a/src/store/authentication.js b/src/store/authentication.js index eb541b0..9a98b01 100644 --- a/src/store/authentication.js +++ b/src/store/authentication.js @@ -56,7 +56,6 @@ export class Authentication { this.profileData = response.body; } catch (e) { console.error(e); - throw e; } } diff --git a/src/store/category.js b/src/store/category.js index 8176b46..facc4d2 100644 --- a/src/store/category.js +++ b/src/store/category.js @@ -20,55 +20,78 @@ export class Category { } async getData() { - const response = await http.get(`/product/categories?page=${this.page}&pageSize=${this.pageSize}`); - this.data = response.body.data.map((item, idx) => { + try { + const response = await http.get(`/product/categories?page=${this.page}&pageSize=${this.pageSize}`); + this.data = response.body.data .map((item, idx) => { item.key = idx; return item }) ?? [] - - this.total_data = response.body.total_data ?? 0 + this.total_data = response.body.total_data ?? 0 + } catch (e) { + console.log(e); + } } async getDataSubCategories() { - const response = await http.get(`/product/sub-categories?page=${this.pageSubCategories}&pageSize=${this.pageSizeSubCategories}`); - this.dataSubCategories = response.body.data.map((item, idx) => { + try { + const response = await http.get(`/product/sub-categories?page=${this.pageSubCategories}&pageSize=${this.pageSizeSubCategories}`); + this.dataSubCategories = response.body.data.map((item, idx) => { item.key = idx; return item }) ?? [] - this.total_dataSubCategories = response.body.count ?? 0 + this.total_dataSubCategories = response.body.count ?? 0 + } catch (e) { + console.log(e); + } } async getDataCategories() { - const response = await http.get(`/product/categories?page=${this.pageCategories}&pageSize=${this.pageSizeCategories}`); + try { + const response = await http.get(`/product/categories?page=${this.pageCategories}&pageSize=${this.pageSizeCategories}`); - this.dataCategories = response.body.data.map((item, idx) => { - item.key = idx; - return item - }) ?? [] - - this.total_dataCategories = response.body.total_data ?? 0 - if (this.dataCategories.length > 0) { - this.filterCategory = this.dataCategories[0].id + this.dataCategories = response.body.data.map((item, idx) => { + item.key = idx; + return item + }) ?? [] + + this.total_dataCategories = response.body.total_data ?? 0 + if (this.dataCategories.length > 0) { + this.filterCategory = this.dataCategories[0].id + } + } catch (e) { + console.error(e); } } async create(data) { - const response = await http.post('/product/categories').send(data); - await this.getData(); - return response; + try { + const response = await http.post('/product/categories').send(data); + await this.getData(); + return response; + } catch (e) { + console.log(e); + } } async update(id, data) { - const response = await http.put(`/product/categories/${id}`).send(data); - await this.getData(); - return response; + try { + const response = await http.put(`/product/categories/${id}`).send(data); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } async delete(id) { - const response = await http.del(`/product/${id}`); - await this.getData(); - return response; + try { + const response = await http.del(`/product/${id}`); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } } diff --git a/src/store/commission.js b/src/store/commission.js index 7fca342..a613c2f 100644 --- a/src/store/commission.js +++ b/src/store/commission.js @@ -25,25 +25,37 @@ export class Commission { } async getData() { - const response = await http.get(`/config/commission?page=${this.page}&pageSize=${this.pageSize}`); - this.data = response.body.data.map((item, idx) => { - item.key = idx; - return item - }) ?? [] - - this.total_data = response.body.total_data ?? 0 + try { + const response = await http.get(`/config/commission?page=${this.page}&pageSize=${this.pageSize}`); + this.data = response.body.data.map((item, idx) => { + item.key = idx; + return item + }) ?? [] + + this.total_data = response.body.total_data ?? 0 + } catch (e) { + console.error(e); + } } async update(id, data) { - const response = await http.put(`/config/commission/${id}`).send(data); - await this.getData(); - return response; + try { + const response = await http.put(`/config/commission/${id}`).send(data); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } async delete(id) { - const response = await http.del(`/product/${id}`); - await this.getData(); - return response; + try { + const response = await http.del(`/product/${id}`); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } } diff --git a/src/store/membership.js b/src/store/membership.js index ad56942..b68f75b 100644 --- a/src/store/membership.js +++ b/src/store/membership.js @@ -1,4 +1,4 @@ -import {action, makeAutoObservable} from "mobx"; +import {makeAutoObservable} from "mobx"; import {http} from "../utils/http"; export class Membership { @@ -13,60 +13,75 @@ export class Membership { } async getData() { - const response = await http.get(`/users?page=${this.page}&pageSize=${this.pageSize}`); - - this.data = response.body.data.map((item, idx) => { - item.key = idx; - item.name = item?.user_detail?.name; - item.phone_number = item?.user_detail?.phone_number; - item.roleId = item?.roles.id; - item.roleName = item?.roles.name; - return item - }) ?? [] + try { + const response = await http.get(`/users?page=${this.page}&pageSize=${this.pageSize}`); - console.log(JSON.stringify(this.data), "DATA"); - - this.total_data = response.body.total_data ?? 0 + this.data = response.body.data.map((item, idx) => { + item.key = idx; + item.name = item?.user_detail?.name; + item.phone_number = item?.user_detail?.phone_number;item.roleId = item?.roles.id; + item.roleName = item?.roles.name; + return item + }) ?? [] + + this.total_data = response.body.total_data ?? 0 + } catch (e) { + console.error(e); + } } async getDataBySuperior() { - const response = await http.get(`/users/find-by-supperior?page=${this.page}&pageSize=${this.pageSize}`); - this.data = response.body.data.map((item, idx) => { - item.key = idx; - item.name = item?.user_detail?.name; - item.phone_number = item?.user_detail?.phone_number; - item.roleId = item?.roles.id; - item.roleName = item?.roles?.name; - return item - }) ?? [] - - this.total_data = response.body.total_data ?? 0 + try { + const response = await http.get(`/users/find-by-supperior?page=${this.page}&pageSize=${this.pageSize}`); + this.data = response.body.data.map((item, idx) => { + item.key = idx; + item.name = item?.user_detail?.name; + item.phone_number = item?.user_detail?.phone_number;item.roleId = item?.roles.id; + item.roleName = item?.roles?.name; + return item + }) ?? [] + + this.total_data = response.body.total_data ?? 0 + } catch (e) { + console.error(e); + } } async create(data) { - return await http.post('/users').send(data) + try { + return await http.post('/users').send(data) + } catch (e) { + console.error(e); + } } async update(id, data) { - // console.log(data) - // console.log(id) - const response = await http.put('/users/' + id).send(data); - console.log(response, 'Data user') - console.log(JSON.stringify(response.body.data), 'Data') + try { + const response = await http.put('/users/' + id).send(data); + console.log(response, 'Data user') + console.log(JSON.stringify(response.body.data), 'Data') + } catch (e) { + console.error(e); + } } async delete(id) { - console.log(id) - return await http.del('/users/' + id); - + try { + return await http.del('/users/' + id); + } catch (e) { + console.error(e); + } + } async changeStatus(id, status) { - console.log(`/users/${id}/${status}`); - - const response = await http.get(`/users/${id}/${status}`); - await this.getData(); - return response; + try { + const response = await http.get(`/users/${id}/${status}`); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } } diff --git a/src/store/partner.js b/src/store/partner.js index 0e94a36..f804b6e 100644 --- a/src/store/partner.js +++ b/src/store/partner.js @@ -25,26 +25,38 @@ export class Partner { } async getData() { - const response = await http.get(`/users/partner?page=${this.page}&pageSize=${this.pageSize}`); - - this.data = response.body.data.map((item, idx) => { - item.key = idx; - return item - }) ?? [] + try { + const response = await http.get(`/users/partner?page=${this.page}&pageSize=${this.pageSize}`); - this.total_data = response.body.count ?? 0 + this.data = response.body.data.map((item, idx) => { + item.key = idx; + return item + }) ?? [] + + this.total_data = response.body.count ?? 0 + } catch (e) { + console.error(e); + } } async create(data) { - const response = await http.post('/users/partner').send(data); - await this.getData(); - return response; + try { + const response = await http.post('/users/partner').send(data); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } async update(id, data) { - const response = await http.put(`/users/partner/${id}`).send(data); - await this.getData(); - return response; + try { + const response = await http.put(`/users/partner/${id}`).send(data); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } // async updateStatus(id, data) { @@ -54,15 +66,23 @@ export class Partner { // } async delete(id) { - const response = await http.del(`/product/${id}`); - await this.getData(); - return response; + try { + const response = await http.del(`/product/${id}`); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } async changeStatus(id, status) { - const response = await http.get(`/users/partner/${id}/${status}`); - await this.getData(); - return response; + try { + const response = await http.get(`/users/partner/${id}/${status}`); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } } diff --git a/src/store/product.js b/src/store/product.js index 2c377a3..690560c 100644 --- a/src/store/product.js +++ b/src/store/product.js @@ -29,48 +29,72 @@ export class Product { } async getData() { - const response = await http.get(`/product/all?supplier=${this.filterSupplier}&categories=${this.filterCategory}&categories=${this.filterSubCategory}&page=${this.page}&pageSize=${this.pageSize}`); - this.data = response.body.data.map((item, idx) => { - item.key = idx; - return item - }) ?? [] - - this.total_data = response.body.total_data ?? 0 + try { + const response = await http.get(`/product/all?supplier=${this.filterSupplier}&categories=${this.filterCategory}&categories=${this.filterSubCategory}&page=${this.page}&pageSize=${this.pageSize}`); + this.data = response.body.data.map((item, idx) => { + item.key = idx; + return item + }) ?? [] + + this.total_data = response.body.total_data ?? 0 + } catch (e) { + console.error(e); + } } async getDataSubCategories() { - const response = await http.get(`/product/sub-categories?page=${this.pageSubCategories}&pageSize=${this.pageSizeSubCategories}`); - this.dataSubCategories = response.body.data.map((item, idx) => { - item.key = idx; - return item - }) ?? [] - this.total_dataSubCategories = response.body.count ?? 0 + try { + const response = await http.get(`/product/sub-categories?page=${this.pageSubCategories}&pageSize=${this.pageSizeSubCategories}`); + this.dataSubCategories = response.body.data.map((item, idx) => { + item.key = idx; + return item + }) ?? [] + this.total_dataSubCategories = response.body.count ?? 0 + } catch (e) { + console.error(e); + } } async getDataCategories() { - const response = await http.get(`/product/categories?page=${this.pageCategories}&pageSize=${this.pageSizeCategories}`); - this.dataCategories = response.body.data.map((item, idx) => { - item.key = idx; - return item - }) ?? [] + try { + const response = await http.get(`/product/categories?page=${this.pageCategories}&pageSize=${this.pageSizeCategories}`); + this.dataCategories = response.body.data.map((item, idx) => { + item.key = idx; + return item + }) ?? [] + } catch (e) { + console.error(e); + } } async create(data) { - const response = await http.post('/product').send(data); - await this.getData(); - return response; + try { + const response = await http.post('/product').send(data); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } async update(id, data) { - const response = await http.put(`/product/${id}`).send(data); - await this.getData(); - return response; + try { + const response = await http.put(`/product/${id}`).send(data); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } async delete(id) { - const response = await http.del(`/product/${id}`); - await this.getData(); - return response; + try { + const response = await http.del(`/product/${id}`); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } } diff --git a/src/store/role.js b/src/store/role.js index 4352187..51bd959 100644 --- a/src/store/role.js +++ b/src/store/role.js @@ -13,21 +13,37 @@ export class Role { } async getData() { - const response = await http.get(`/config/roles?page=${this.page}&pageSize=${this.pageSize}`); - this.data = response.body.data ?? [] - this.total_data = response.body.total_data ?? 0 + try { + const response = await http.get(`/config/roles?page=${this.page}&pageSize=${this.pageSize}`); + this.data = response.body.data ?? [] + this.total_data = response.body.total_data ?? 0 + } catch (e) { + console.error(e); + } } async create(data) { - return await http.post('/users').send(data) + try { + return await http.post('/users').send(data) + } catch (e) { + console.error(e); + } } async update(id, data) { - return await http.put('/users/' + id).send(data); + try { + return await http.put('/users/' + id).send(data); + } catch (e) { + console.error(e); + } } async delete(id) { - return await http.del('/users/' + id); + try { + return await http.del('/users/' + id); + } catch (e) { + console.error(e); + } } } diff --git a/src/store/subcategory.js b/src/store/subcategory.js index 282dc39..8268179 100644 --- a/src/store/subcategory.js +++ b/src/store/subcategory.js @@ -25,42 +25,62 @@ export class Subcategory { } async getData() { - const response = await http.get(`/product/sub-categories?page=${this.page}&pageSize=${this.pageSize}`); - this.data = response.body.data.map((item, idx) => { + try { + const response = await http.get(`/product/sub-categories?page=${this.page}&pageSize=${this.pageSize}`); + this.data = response.body.data.map((item, idx) => { item.key = idx; item.categoryName = item.category.name; return item }) ?? [] - this.total_data = response.body.total_data ?? 0 + this.total_data = response.body.total_data ?? 0 + } catch (e) { + console.error(e); + } } async getDataSubCategories() { - const response = await http.get(`/product/sub-categories?page=${this.pageSubCategories}&pageSize=${this.pageSizeSubCategories}`); - this.dataSubCategories = response.body.data.map((item, idx) => { + try { + const response = await http.get(`/product/sub-categories?page=${this.pageSubCategories}&pageSize=${this.pageSizeSubCategories}`); + this.dataSubCategories = response.body.data.map((item, idx) => { item.key = idx; return item }) ?? [] - - this.total_dataSubCategories = response.body.count ?? 0 + + this.total_dataSubCategories = response.body.count ?? 0 + } catch (e) { + console.error(e); + } } async create(data) { - const response = await http.post('/product/sub-categories').send(data); - await this.getData(); - return response; + try { + const response = await http.post('/product/sub-categories').send(data); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } async update(id, data) { - const response = await http.put(`/product/sub-categories/${id}`).send(data); - await this.getData(); - return response; + try { + const response = await http.put(`/product/sub-categories/${id}`).send(data); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } async delete(id) { - const response = await http.del(`/product/${id}`); - await this.getData(); - return response; + try { + const response = await http.del(`/product/${id}`); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } } diff --git a/src/store/supplier.js b/src/store/supplier.js index a8a22d7..0684349 100644 --- a/src/store/supplier.js +++ b/src/store/supplier.js @@ -27,39 +27,62 @@ export class Supplier { } async getData() { - const response = await http.get(`/users/supplier?page=${this.page}&pageSize=${this.pageSize}`); - //console.log(response) - this.data = response.body.data ?? [] - this.total_data = response.body.count ?? 0 + try { + const response = await http.get(`/users/supplier?page=${this.page}&pageSize=${this.pageSize}`); + this.data = response.body.data ?? [] + this.total_data = response.body.count ?? 0 + } catch (e) { + console.error(e); + } } async create(data) { - const response = await http.post('/users/supplier').send(data); - await this.getData(); - return response; + try { + const response = await http.post('/users/supplier').send(data); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } async createTransaction(data) { - const response = await http.post('/transaction/add-saldo-supplier').send(data); - await this.getData(); - return response; + try { + const response = await http.post('/transaction/add-saldo-supplier').send(data); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } async update(id, data) { - const response = await http.put(`/users/supplier/${id}`).send(data); - await this.getData(); - return response; + try { + const response = await http.put(`/users/supplier/${id}`).send(data); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } async delete(id) { - const response = await http.del(`/product/${id}`); - await this.getData(); - return response; + try { + const response = await http.del(`/product/${id}`); + await this.getData(); + return response; + } catch (e) { + console.error(e) + } } async changeStatus(id, status) { - const response = await http.get(`/users/supplier/${id}/${status}`); - await this.getData(); - return response; + try { + const response = await http.get(`/users/supplier/${id}/${status}`); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } } diff --git a/src/store/transaction.js b/src/store/transaction.js index 916dc21..9372be4 100644 --- a/src/store/transaction.js +++ b/src/store/transaction.js @@ -30,61 +30,96 @@ export class Transaction { } async getData() { - const response = await http.get(`/product/by-categories?categories=${this.filterCategory}&page=${this.page}&pageSize=${this.pageSize}`); - - this.data = response.body.data ?? [] - this.total_data = response.body.total_data ?? 0 + try { + const response = await http.get(`/product/by-categories?categories=${this.filterCategory}&page=${this.page}&pageSize=${this.pageSize}`); + this.data = response.body.data ?? [] + this.total_data = response.body.total_data ?? 0 + } catch (e) { + console.error(e); + } } async getDataSubCategories() { - const response = await http.get(`/product/sub-categories?page=${this.pageSubCategories}&pageSize=${this.pageSizeSubCategories}`); - this.dataSubCategories = response.body.data ?? [] - this.total_dataSubCategories = response.body.count ?? 0 + try { + const response = await http.get(`/product/sub-categories?page=${this.pageSubCategories}&pageSize=${this.pageSizeSubCategories}`); + this.dataSubCategories = response.body.data ?? [] + this.total_dataSubCategories = response.body.count ?? 0 + } catch (e) { + console.error(e); + } } async getDataCategories() { - const response = await http.get(`/product/categories?page=${this.pageCategories}&pageSize=${this.pageSizeCategories}`); + try { + const response = await http.get(`/product/categories?page=${this.pageCategories}&pageSize=${this.pageSizeCategories}`); - this.dataCategories = response.body.data ?? [] - this.total_dataCategories = response.body.total_data ?? 0 - if (this.dataCategories.length > 0) { - this.filterCategory = this.dataCategories[0].id + this.dataCategories = response.body.data ?? [] + this.total_dataCategories = response.body.total_data ?? 0 + if (this.dataCategories.length > 0) { + this.filterCategory = this.dataCategories[0].id + } + } catch (e) { + console.error(e); } } async getDataHistoryTransaction() { - const response = await http.get(`/transaction/history?page=${this.pageHistoryTransaction}`); + try { + const response = await http.get(`/transaction/history?page=${this.pageHistoryTransaction}`); - this.dataHistoryTransaction = response.body.data ?? [] - this.total_dataHistoryTransaction = response.body.total_data ?? 0 + this.dataHistoryTransaction = response.body.data ?? [] + this.total_dataHistoryTransaction = response.body.total_data ?? 0 + } catch (e) { + console.error(e); + } } async create(data) { - const response = await http.post('/product').send(data); - await this.getData(); - return response; + try { + const response = await http.post('/product').send(data); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } async update(id, data) { - const response = await http.put(`/product/${id}`).send(data); - await this.getData(); - return response; + try { + const response = await http.put(`/product/${id}`).send(data); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } async delete(id) { - const response = await http.del(`/product/${id}`); - await this.getData(); - return response; + try { + const response = await http.del(`/product/${id}`); + await this.getData(); + return response; + } catch (e) { + console.error(e); + } } async distribute(data) { - const response = await http.post('/transaction/distribute').send(data); - return response; + try { + const response = await http.post('/transaction/distribute').send(data); + return response; + } catch (e) { + console.error(e); + } } async distributeAdmin(data) { - const response = await http.post('/transaction/distribute-admin').send(data); - return response; + try { + const response = await http.post('/transaction/distribute-admin').send(data); + return response; + } catch (e) { + console.error(e); + } } } diff --git a/src/store/user.js b/src/store/user.js index 9e08229..f341af4 100644 --- a/src/store/user.js +++ b/src/store/user.js @@ -11,7 +11,11 @@ export class User { @action async getData() { - this.data = (await http.get('/user')).body.data; + try { + this.data = (await http.get('/user')).body.data; + } catch (e) { + console.error(e); + } } }