Skip to content

Commit

Permalink
fix: coupon api (#218)
Browse files Browse the repository at this point in the history
* chor: update the outdate period to 24hrs

Signed-off-by: Aman <[email protected]>

* rfac: update the coupon api

Signed-off-by: Aman <[email protected]>

* feat: Add dialog before updating status of coupon. (#217)

* feat: add dialog for coupon number

Signed-off-by: Aman <[email protected]>

---------

Signed-off-by: Aman <[email protected]>
Co-authored-by: Ayush Chaudhary <[email protected]>
  • Loading branch information
aman-singh7 and Ayush0Chaudhary authored Feb 15, 2023
1 parent 915f873 commit 8b5f64b
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 25 deletions.
8 changes: 8 additions & 0 deletions lib/models/menu/week_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class Meal {
String? secretCode;
bool isOutdated;
bool isLeaveToggleOutdated;
bool isCouponOutdated;
DateTime startDateTime;
DateTime endDateTime;

Expand All @@ -200,6 +201,7 @@ class Meal {
this.secretCode,
required this.isOutdated,
required this.isLeaveToggleOutdated,
required this.isCouponOutdated,
required this.startDateTime,
required this.endDateTime,
required this.couponStatus,
Expand Down Expand Up @@ -227,6 +229,10 @@ class Meal {
!DateTimeUtils.getDateTimeFromDateAndTime(date, json['start_time'])
.subtract(outdatedTime)
.isAfter(DateTime.now()),
isCouponOutdated:
!DateTimeUtils.getDateTimeFromDateAndTime(date, json['start_time'])
.subtract(outdatedTime * 2)
.isAfter(DateTime.now()),
startDateTime:
DateTimeUtils.getDateTimeFromDateAndTime(date, json['start_time']),
endDateTime:
Expand Down Expand Up @@ -264,6 +270,7 @@ class Meal {
String? secretCode,
bool? isOutdated,
bool? isLeaveToggleOutdated,
bool? isCouponOutdated,
DateTime? startDateTime,
DateTime? endDateTime,
}) {
Expand All @@ -284,6 +291,7 @@ class Meal {
isLeaveToggleOutdated:
isLeaveToggleOutdated ?? this.isLeaveToggleOutdated,
isOutdated: isOutdated ?? this.isOutdated,
isCouponOutdated: isCouponOutdated ?? this.isCouponOutdated,
startDateTime: startDateTime ?? this.startDateTime,
endDateTime: endDateTime ?? this.endDateTime,
);
Expand Down
6 changes: 3 additions & 3 deletions lib/services/api/coupon_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class CouponApi {

try {
await ApiUtils.addTokenToHeaders(headers);
var jsonResponse =
await ApiUtils.post(uri, headers: headers, body: {'meal': mealId});
var jsonResponse = await ApiUtils.post(uri,
headers: headers, body: {'meal': mealId, 'is_active': true});

return CouponStatus(
status: CouponStatusEnum.A,
Expand All @@ -34,7 +34,7 @@ class CouponApi {

try {
await ApiUtils.addTokenToHeaders(headers);
await ApiUtils.delete(uri, headers: headers);
await ApiUtils.patch(uri, headers: headers, body: {'is_active': false});

return CouponStatus(status: CouponStatusEnum.N);
} on Exception catch (e) {
Expand Down
70 changes: 52 additions & 18 deletions lib/ui/menu/components/your_meal_menu_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,49 @@ class _YourMealsMenuCardState extends State<YourMealsMenuCard> {

Widget _buildCouponCodeComponent() {
if (_model.meal!.couponStatus.status == CouponStatusEnum.A &&
_model.meal!.isOutdated) {
return Container(
decoration: BoxDecoration(
color: AppTheme.secondary,
borderRadius: BorderRadius.circular(24),
),
padding: EdgeInsets.symmetric(
horizontal: 8.r,
vertical: 4.r,
),
child: Text(
'CPN - ${_model.meal!.couponStatus.id}',
style: AppTheme.bodyText2.copyWith(color: AppTheme.white),
_model.meal!.isCouponOutdated) {
return GestureDetector(
onTap: () {
Get.dialog(
SimpleDialog(
contentPadding: EdgeInsets.symmetric(vertical: 10),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.r),
side: BorderSide(color: AppTheme.grey),
),
children: [
Center(
child: Text(
'Coupon',
style: AppTheme.headline6,
),
),
Center(
child: Text(
'${_model.meal!.couponStatus.id}',
style: AppTheme.headline1.copyWith(
fontSize: 30.sp,
color: AppTheme.tertiary,
),
),
),
],
),
);
},
child: Container(
decoration: BoxDecoration(
color: AppTheme.secondary,
borderRadius: BorderRadius.circular(24),
),
padding: EdgeInsets.symmetric(
horizontal: 8.r,
vertical: 4.r,
),
child: Text(
'CPN - ${_model.meal!.couponStatus.id}',
style: AppTheme.bodyText2.copyWith(color: AppTheme.white),
),
),
);
}
Expand Down Expand Up @@ -202,11 +232,15 @@ class _YourMealsMenuCardState extends State<YourMealsMenuCard> {
_model.onLeaveChanged(value).then((_) {
context.read<YourMenuViewModel>().updateMeal =
_model.meal!.copyWith(
leaveStatus: LeaveStatus(
status: _model.mealLeaveStatus
? LeaveStatusEnum.N
: LeaveStatusEnum.P,
));
leaveStatus: LeaveStatus(
status: _model.mealLeaveStatus
? LeaveStatusEnum.N
: LeaveStatusEnum.P,
),
couponStatus: !_model.mealLeaveStatus
? CouponStatus(status: CouponStatusEnum.N)
: _model.meal!.couponStatus,
);
});
},
),
Expand Down
7 changes: 7 additions & 0 deletions lib/ui/menu/your_menu_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import 'package:appetizer/ui/components/appetizer_error_widget.dart';
import 'package:appetizer/ui/components/appetizer_progress_widget.dart';
import 'package:appetizer/ui/menu/your_day_menu_view.dart';
import 'package:appetizer/utils/date_time_utils.dart';
import 'package:appetizer/viewmodels/home_viewmodel.dart';
import 'package:appetizer/viewmodels/menu/your_menu_viewmodel.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class YourMenuView extends StatefulWidget {
final DateTime selectedDateTime;
Expand Down Expand Up @@ -38,6 +40,11 @@ class _YourMenuViewState extends State<YourMenuView> {
return BaseView<YourMenuViewModel>(
onModelReady: (model) {
_model = model;
context.read<HomeViewModel>().addListener(() {
_model.fetchSelectedWeekMenu(
DateTimeUtils.getWeekNumber(widget.selectedDateTime),
);
});
_model.fetchInitialCheckedStatus();
_model.selectedDateTime = widget.selectedDateTime;
_model.fetchSelectedWeekMenu(
Expand Down
6 changes: 5 additions & 1 deletion lib/utils/menu_ui_utils.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:math' as math;

import 'package:appetizer/app_theme.dart';
import 'package:appetizer/globals.dart';
import 'package:appetizer/models/menu/week_menu.dart';
import 'package:appetizer/ui/components/appetizer_outline_button.dart';
import 'package:appetizer/ui/components/appetizer_primary_button.dart';
Expand Down Expand Up @@ -82,7 +83,10 @@ class MenuUIUtils {
),
),
),
if (!meal.isOutdated && item.type == MealItemType.CPN)
if (!meal.isCouponOutdated &&
item.type == MealItemType.CPN &&
meal.leaveStatus.status == LeaveStatusEnum.N &&
!isCheckedOut)
SizedBox(
height: 20.r,
child: meal.couponStatus.status == CouponStatusEnum.N
Expand Down
13 changes: 11 additions & 2 deletions lib/viewmodels/menu/your_menu_card_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ class YourMenuCardViewModel extends BaseModel {
if (_meal != null) {
if (_meal!.couponStatus.status == CouponStatusEnum.A) {
try {
return await _couponApi.cancelCoupon(_meal!.couponStatus.id!);
var _dialogResponse = await _dialogService.showConfirmationDialog(
title: 'Coupon',
description: 'Do you want to cancel your coupon?');
if (_dialogResponse.confirmed) {
return await _couponApi.cancelCoupon(_meal!.couponStatus.id!);
}
} on Failure catch (f) {
print(f.toString());
setErrorMessage(f.message);
Expand All @@ -126,7 +131,11 @@ class YourMenuCardViewModel extends BaseModel {
}
} else {
try {
return await _couponApi.applyForCoupon(_meal!.id);
var _dialogResponse = await _dialogService.showConfirmationDialog(
title: 'Coupon', description: 'Do you want to apply for coupon?');
if (_dialogResponse.confirmed) {
return await _couponApi.applyForCoupon(_meal!.id);
}
} on Failure catch (f) {
print(f.toString());
setErrorMessage(f.message);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -966,5 +966,5 @@ packages:
source: hosted
version: "3.1.1"
sdks:
dart: ">=2.19.0-0 <4.0.0"
dart: ">=2.19.0-0 <3.0.0"
flutter: ">=3.7.0-0"

0 comments on commit 8b5f64b

Please sign in to comment.