Skip to content

Commit

Permalink
fix: Fix order price.
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanTkachev committed May 27, 2021
1 parent 4ca0875 commit 1424b2f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 24 deletions.
17 changes: 15 additions & 2 deletions src/modules/catalog/product/ProductCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import {
GetCategoriesShortDocument,
useCreateProductMutation,
ProductCategoryCreateInput,
TenantPriceDisplay,
} from '../../../generated/graphql';
import * as DineroHelper from '../../../helpers';
import { DineroObject } from 'dinero.js';
import { useTenant } from '../../../context/TenantContext';

const ProductCreate: React.FC = () => {
const { currentTenant } = useTenant();
const navigate = useNavigate();
const [createProduct, { error }] = useCreateProductMutation({
refetchQueries: [
Expand All @@ -27,13 +31,22 @@ const ProductCreate: React.FC = () => {
const onSubmitBasic = useCallback(
async (values) => {
const input: ProductCreateInput = {
tenantId: 1,
tenantId: currentTenant?.id,
...values,
price: {
grossAmount: DineroHelper.moneyFromString(values.price),
grossAmount:
currentTenant?.priceDisplay === TenantPriceDisplay.ExlVat
? DineroHelper.moneyFromString(values.price)
: DineroHelper.moneyFromString(
DineroHelper.priceWithoutRate(
currentTenant?.vatRate?.value as DineroObject,
values.price
)
),
},
stockControl: true,
inStockNum: 0,
vatRateId: currentTenant?.vatRate?.id,
};

if (values.categories) {
Expand Down
28 changes: 21 additions & 7 deletions src/modules/catalog/product/ProductUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ import {
useGetProductQuery,
useUpdateProductMutation,
ProductUpdateInput,
TenantPriceDisplay,
} from '../../../generated/graphql';
import * as DineroHelper from '../../../helpers';
import { DineroObject } from 'dinero.js';
import { useTenant } from '../../../context/TenantContext';

const ProductUpdate: React.FC = () => {
const { currentTenant } = useTenant();
const navigate = useNavigate();
const params = useParams();
const productId = Number(params.productId);
Expand All @@ -49,16 +53,26 @@ const ProductUpdate: React.FC = () => {

const onSubmit = useCallback(
async (values) => {
const input = _.omit({ tenantId: 1, ...data?.product, ...values }, [
'id',
'__typename',
'price',
'categories',
]) as ProductUpdateInput;
const input = _.omit(
{
tenantId: currentTenant?.id,
...data?.product,
...values,
},
['id', '__typename', 'price', 'categories']
) as ProductUpdateInput;

if (values.price) {
input.price = {
grossAmount: DineroHelper.moneyFromString(values.price),
grossAmount:
currentTenant?.priceDisplay === TenantPriceDisplay.ExlVat
? DineroHelper.moneyFromString(values.price)
: DineroHelper.moneyFromString(
DineroHelper.priceWithoutRate(
currentTenant?.vatRate?.value as DineroObject,
values.price
)
),
};
}
if (values.categories) {
Expand Down
28 changes: 19 additions & 9 deletions src/modules/catalog/product/components/ProductBasicOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
GetSignedUrlQuery,
GetSignedUrlQueryVariables,
ProductStatus,
TenantPriceDisplay,
useGetCategoriesShortQuery,
} from '../../../../generated/graphql';
import { useTenant } from '../../../../context/TenantContext';
Expand Down Expand Up @@ -86,7 +87,10 @@ const ProductBasicOptions: React.FC<ProductBasicOptionsProps> = ({
initialValues: {
...defaultValues,
...product,
price: DineroHelper.formatPrice(product?.price),
price: DineroHelper.formatPrice(
product?.price,
currentTenant?.priceDisplay === TenantPriceDisplay.IncVat
),
categories: product?.categories.map((category) => ({
id: category?.id,
name: category?.title,
Expand Down Expand Up @@ -195,7 +199,11 @@ const ProductBasicOptions: React.FC<ProductBasicOptionsProps> = ({
</div>
<div>
<PrefilledInput
label="Pris inkl. MVA"
label={
currentTenant?.priceDisplay === TenantPriceDisplay.IncVat
? 'Pris inkl. MVA'
: 'Pris'
}
prefilledText="NOK"
prefilledTextPosition={TextPosition.RIGHT}
placeholder="258,00"
Expand All @@ -208,13 +216,15 @@ const ProductBasicOptions: React.FC<ProductBasicOptionsProps> = ({
{form.touched.price && form.errors.price && (
<Error message={form.errors.price} />
)}
<Label sx={{ color: 'gray', ml: 158 }}>
{'Pris eks. MVA: ' +
DineroHelper.priceWithoutRate(
currentTenant?.vatRate?.value as DineroObject,
form.values.price
)}
</Label>
{currentTenant?.priceDisplay === TenantPriceDisplay.IncVat && (
<Label sx={{ color: 'gray', ml: 158 }}>
{'Pris eks. MVA: ' +
DineroHelper.priceWithoutRate(
currentTenant?.vatRate?.value as DineroObject,
form.values.price
)}
</Label>
)}
</div>
<div>
<Field
Expand Down
5 changes: 5 additions & 0 deletions src/modules/settings/ShippingSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
useGetDeliveryMethodsQuery,
} from '../../generated/graphql';
import { TenantItem } from '../../context/TenantContext';
import * as DineroHelper from '../../helpers';

export type DeliveryMethodItem =
GetDeliveryMethodsQuery['deliveryMethods']['items'][0];
Expand Down Expand Up @@ -86,6 +87,10 @@ const Shipping: React.FC<ShippingSettingsProps> = ({ tenant }) => {
{
Header: 'Pris',
accessor: 'price.amount',
Cell: ({
row: { original: deliveryMethod },
}: CellProps<DeliveryMethodItem>) =>
DineroHelper.formatMoney(deliveryMethod?.price),
},
{
Header: 'Status',
Expand Down
10 changes: 4 additions & 6 deletions src/modules/settings/ShippingSettingsModalContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ export const ShippingSettingsModalContent: React.FC<{
tenantId: number | null | undefined;
onRequestClose: () => void;
}> = ({ currentDeliveryMethod, onRequestClose, tenantId }) => {
const [updateDeliveryMethod, { error: updateError }] =
useUpdateDeliveryMethodMutation();
const [createDeliveryMethod, { error: createError }] =
useCreateDeliveryMethodMutation();
const [updateDeliveryMethod] = useUpdateDeliveryMethodMutation();
const [createDeliveryMethod] = useCreateDeliveryMethodMutation();

const update = async (
currentDeliveryMethod: DeliveryMethodItem,
Expand Down Expand Up @@ -68,14 +66,14 @@ export const ShippingSettingsModalContent: React.FC<{
<Formik
initialValues={{
name: currentDeliveryMethod?.name || '',
price: currentDeliveryMethod?.price.amount || '',
price: DineroHelper.formatMoney(currentDeliveryMethod?.price) || '',
status:
currentDeliveryMethod?.status ||
DeliveryMethodStatus.DeliveryMethodStatusUnspecified,
digitalDelivery: currentDeliveryMethod?.digitalDelivery || false,
}}
validationSchema={DeliveryMethodSchema}
onSubmit={async (values, { setSubmitting }) => {
onSubmit={async (values) => {
const input = {
name: values.name,
price: DineroHelper.moneyFromString(String(values.price)),
Expand Down

0 comments on commit 1424b2f

Please sign in to comment.