Skip to content

Instantly share code, notes, and snippets.

@evertonpavan
Last active June 30, 2022 13:55
Show Gist options
  • Save evertonpavan/c3070dfda6bdb9af8823648d82e038c7 to your computer and use it in GitHub Desktop.
Save evertonpavan/c3070dfda6bdb9af8823648d82e038c7 to your computer and use it in GitHub Desktop.
Modificações - Horário de Atendimento Especial
// Modificações - Horário de Atendimento Especial
// -----------------------------------------------------------------------
// SUBSTITUIR O CÓDIGO ABAIXO:
useEffect(() => {
if (!isPlanUpdated) return;
if (!specialHours && plan.company?.cnpj) {
const filtersSpecialHoursPlans = specialHoursPlans?.map(
(specialHoursplan) => {
specialHoursplan.checked = false;
return specialHoursplan;
}
);
setSpecialHoursPlans(filtersSpecialHoursPlans);
}
// eslint-disable-next-line
}, [specialHours, plan.company?.cnpj, specialHoursPlans]);
// POR:
useEffect(() => {
if (!isPlanUpdated) return;
if (!specialHours && plan.company?.cnpj) {
const filtersSpecialHoursPlans = specialHoursPlans?.map(
(specialHoursplan) => {
specialHoursplan.checked = false;
return specialHoursplan;
}
);
const monthlyAmount = specialHoursPlans
?.map((plan) => getMoney(plan.amount))
.reduce((total, valor) => total + valor, 0);
setMonthlyAmountSpecialHoursPlans(formatReal(monthlyAmount));
setSpecialHoursPlans(filtersSpecialHoursPlans);
}
// eslint-disable-next-line
}, [specialHours, plan.company?.cnpj]);
// -----------------------------------------------------------------------
// SUBSTITUIR O CÓDIGO ABAIXO:
useEffect(() => {
// if (!isPlanUpdated) return;
const planIds = (optional?.specialHoursPlanIds || '')
?.split(',')
?.map((planId) => Number(planId));
api
.get(`special-hours/terminals/${requirements.terminalNumber}`)
.then((response) => {
const selectedPlans: ISpecialHoursPlan[] = response.data?.map(
(plan: any) => {
plan.checked = false;
const isEqual = planIds.some((planId) => plan.id === planId);
if (isEqual) plan.checked = true;
return plan;
}
);
setSpecialHoursPlans(selectedPlans);
setSpecialHoursPlansIds(selectedPlans.map((plan) => plan.id));
})
.catch((error) => {});
}, [isPlanUpdated, requirements.terminalNumber, optional]);
// POR:
useEffect(() => {
const planIds = (optional?.specialHoursPlanIds || '')
?.split(',')
?.map((planId) => Number(planId));
api
.get(`special-hours/terminals/${requirements.terminalNumber}`)
.then((response) => {
const selectedPlans: ISpecialHoursPlan[] = response.data?.map(
(plan: any) => {
plan.checked = false;
const isEqual = planIds.some((planId) => plan.id === planId);
if (isEqual) plan.checked = true;
return plan;
}
);
const monthlyAmount = selectedPlans
?.map((plan) => getMoney(plan.amount))
.reduce((total, valor) => total + valor, 0);
setMonthlyAmountSpecialHoursPlans(formatReal(monthlyAmount));
setSpecialHoursPlans(selectedPlans);
setSpecialHoursPlansIds(selectedPlans.map((plan) => plan.id));
})
.catch((error) => {});
}, [isPlanUpdated, requirements.terminalNumber, optional]);
// -----------------------------------------------------------------------
// SUBSTITUIR O CÓDIGO ABAIXO:
async function getSpecialHoursPlansCheckeds(
specialHoursPlans: ISpecialHoursPlan[]
) {
const selectedPlans = specialHoursPlans?.filter(
(plan: ISpecialHoursPlan) => plan.checked === true
);
if (selectedPlans?.length === 0) {
setSpecialHours(false);
} else {
setSpecialHours(true);
}
const monthlyAmount = selectedPlans
?.map((plan) => getMoney(plan.amount))
.reduce((total, valor) => total + valor, 0);
const selectedPlansIds: number[] = selectedPlans?.map(
(item: ISpecialHoursPlan) => item.id
);
setSpecialHoursPlansIds(selectedPlansIds);
setMonthlyAmountSpecialHoursPlans(formatReal(monthlyAmount));
}
// POR:
async function getSpecialHoursPlansCheckeds(
specialHoursPlans: ISpecialHoursPlan[]
) {
const selectedPlans = specialHoursPlans?.filter(
(plan: ISpecialHoursPlan) => plan.checked === true
);
if (selectedPlans?.length === 0) {
setSpecialHours(false);
} else {
setSpecialHours(true);
const monthlyAmount = selectedPlans
?.map((plan) => getMoney(plan.amount))
.reduce((total, valor) => total + valor, 0);
setMonthlyAmountSpecialHoursPlans(formatReal(monthlyAmount));
}
}
// -----------------------------------------------------------------------
// SUBSTITUIR O CÓDIGO ABAIXO:
{`Horário Especial de Atendimento ${specialHours !== false
? `(+ R$ ${monthlyAmountSpecialHoursPlans})`
: ''
}`}
// POR
{`Horário Especial de Atendimento (+ R$ ${monthlyAmountSpecialHoursPlans})`}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment