Skip to content

Commit

Permalink
feat(com-pwa): validate shipping form and item list before enable submit
Browse files Browse the repository at this point in the history
button
  • Loading branch information
njfamirm authored and alimd committed Mar 7, 2023
1 parent cf3554f commit 9af59d7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion core/type/src/customer-order-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export interface OrderShippingInfo extends StringifyableRecord {

// -- Schema --

export const orderLadingSchema = {
export const orderShippingInfoSchema = {
recipientName: String,
recipientNationalCode: String,
address: String,
Expand Down
2 changes: 2 additions & 0 deletions uniquely/com-pwa/src/content/l18e-fa.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
"order_status_canceled": "لغو شده",
"order_status_refunded": "بازپرداخت",

"order_shipping_info_empty_description": "ندارد.",

"order_shipping_address_title": "آدرس تحویل",
"order_shipping_recipient_name_title": "نام تحویل گیرنده",
"order_shipping_recipient_national_code_title": "کد ملی تحویل گیرنده",
Expand Down
25 changes: 23 additions & 2 deletions uniquely/com-pwa/src/ui/page/new-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
UnresolvedMixin,
} from '@alwatr/element';
import {message} from '@alwatr/i18n';
import {orderShippingInfoSchema} from '@alwatr/type/src/customer-order-management.js';
import {IconBoxContent} from '@alwatr/ui-kit/src/card/icon-box.js';
import {validator} from '@alwatr/validator';

import {buttons, pageNewOrderStateMachine} from '../../manager/controller/new-order.js';
import {AlwatrOrderDetailBase} from '../stuff/order-detail-base.js';
Expand Down Expand Up @@ -74,7 +76,6 @@ export class AlwatrPageNewOrder extends StateMachineMixin(
protected render_state_shippingForm(): unknown {
this._logger.logMethod('render_state_shippingForm');
const order = this.stateMachine.context.order;
// order.shippingInfo ??= getLocalStorageItem('shipping_form_data_x1', {});
order.shippingInfo ??= {};
return [
this.render_part_item_list(order.itemList ?? [], this.stateMachine.context.productStorage, false),
Expand Down Expand Up @@ -147,7 +148,8 @@ export class AlwatrPageNewOrder extends StateMachineMixin(
<alwatr-button
.icon=${buttons.submit.icon}
.clickSignalId=${buttons.submit.clickSignalId}
?disabled=${!pageNewOrderStateMachine.context.order.itemList?.length}
elevated
?disabled=${!this.isSubmitAble()}
>${message('page_new_order_submit')}</alwatr-button>
</div>
`;
Expand Down Expand Up @@ -189,4 +191,23 @@ export class AlwatrPageNewOrder extends StateMachineMixin(
</div>
`;
}

protected isSubmitAble(): boolean {
this._logger.logMethod('isSubmitAble');
const shippingInfo = pageNewOrderStateMachine.context.order.shippingInfo;
if (
pageNewOrderStateMachine.context.order.itemList?.length === 0 ||
shippingInfo == null
) return false;

try {
validator(orderShippingInfoSchema, shippingInfo, true);
}
catch (err) {
this._logger.error('isSubmitAble', (err as Error).message, {err});
return false;
}

return true;
}
}

0 comments on commit 9af59d7

Please sign in to comment.