Created
May 18, 2024 03:19
-
-
Save ribeiroevandro/eddaf36419c4efb97df0a5a5cc16c871 to your computer and use it in GitHub Desktop.
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
// Essa função getSearchParams() é usada para verificar os parâmentros na URL, ela é usada em outras funções. | |
getSearchParams(page = null) { | |
const currentUrl = new URL(window.location.href); | |
const params = {}; | |
const urlParams = new URLSearchParams(window.location.search); | |
if (this.form.tab_sales_rank_date) { | |
const {transformInitDate} = transformDate(this.form.tab_sales_rank_date) | |
if (this.form.tab_sales_rank_date) { | |
params.date = transformInitDate | |
currentUrl.searchParams.set('date', params.date); | |
} else { | |
currentUrl.searchParams.delete('date'); | |
} | |
} | |
if (this.form.tab_product_date) { | |
const {transformInitDate} = transformDate(this.form.tab_product_date) | |
if (this.form.tab_product_date) { | |
params.date = transformInitDate | |
currentUrl.searchParams.set('date', params.date); | |
} else { | |
currentUrl.searchParams.delete('date'); | |
} | |
} | |
if (this.form.business_income) { | |
params.business_income = this.form.business_income | |
currentUrl.searchParams.set('business_income', params.business_income); | |
} else { | |
currentUrl.searchParams.delete('business_income'); | |
} | |
if (page) { | |
params.page = page; | |
currentUrl.searchParams.set('page', params.page); | |
} else { | |
currentUrl.searchParams.delete('page'); | |
} | |
history.pushState(null, null, currentUrl); | |
return params; | |
} | |
// Essa função normalizeQueryParams() é executada toda vez que a tela é carregada e ela normaliza os dados e faz as requisições necessárias com esses dados | |
async normalizeQueryParams() { | |
const currentUrl = new URL(window.location.href); | |
const params = currentUrl.searchParams; | |
this.form.gateway = params.get('gateway') ?? ''; | |
const searchParams = this.getSearchParams(); | |
try { | |
await Promise.all([this.getRankTotals(searchParams), this.getProducts(searchParams)]) | |
} catch (e) { | |
console.log(e); | |
} | |
} | |
// As funções getProducts() e getRankTotals() são responsáveis por realizar as requisições de acordo com cada contexto e necessidade | |
async getProducts(params = {}) { | |
try { | |
const {data: {data}} = await axios.get("{{ route('manager.analytics.taxesPanel.productsTable') }}", { | |
params, | |
headers: {"X-CSRF-TOKEN": '{{ csrf_token() }}'} | |
}); | |
this.products = Object.entries(data).map(item => { | |
const titles = { | |
'active_products': 'Produtos cadastrado e ativos', | |
'sold_products': 'Produtos vendidos', | |
'sold_unique_products': 'Produtos diferentes com venda', | |
} | |
return { | |
title: titles[item[0]], | |
data: item[1] | |
} | |
}) | |
} catch (e) { | |
console.log(e); | |
} | |
}, | |
async getRankTotals(params = {}) { | |
try { | |
const {data: {data}} = await axios.get("{{ route('manager.analytics.salesRank.reportTotals') }}", { | |
params, | |
headers: {"X-CSRF-TOKEN": '{{ csrf_token() }}'} | |
}); | |
this.totals = Object.values(data) | |
} catch (e) { | |
console.log(e); | |
} | |
} | |
// Essa função é usada toda vez que uma data é selecionada, basicamente ela executada a `getSearchParams()` e refaz as requisições com os parâmtros retornados | |
async submitFilters() { | |
vegaLoading(true); | |
const params = this.getSearchParams(); | |
try { | |
await Promise.all([this.getRankTotals(params), this.getProducts(params)]) | |
} catch (e) { | |
console.log(e); | |
} finally { | |
vegaLoading(false); | |
} | |
} | |
async handleUpdateCurrentTab(tab) { | |
const url = new URL(window.location.href); | |
const urlParams = url.searchParams; | |
if (this.currentTab !== tab) { | |
if (urlParams.has('date')) { | |
urlParams.delete('date'); | |
history.pushState(null, null, url.toString()); | |
this.form = {} | |
} | |
} | |
this.currentTab = tab | |
return true; | |
} | |
async selectTab(tab) { | |
const params = this.getSearchParams(); | |
await this.handleUpdateCurrentTab(tab) | |
if (this.currentTab === this.TAB_ENUM.PRODUCT_PANEL) { | |
try { | |
await this.submitFilters() | |
} catch (e) { | |
console.log(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment