Ver Fonte

fix: 修复新建空账号不能修改食谱计划的BUG

YuJing há 1 mês atrás
pai
commit
e6ea9d4239

+ 15 - 1
features/feature/src/main/ets/model/Index.ets

@@ -85,7 +85,7 @@ export class BabyInfo {
 
 // 本周食谱
 export class WeeklyPlan {
-  id?: string;
+  id?: number;
   babyId?: string;
   weekStartDate?: string;
   toDayPlan?: DayPlan;
@@ -105,6 +105,10 @@ export class DayPlan {
       this.breakfast = i.breakfast;
       this.lunch = i.lunch;
       this.dinner = i.dinner;
+    } else {
+      this.breakfast = new Cuisine();
+      this.lunch = new Cuisine();
+      this.dinner = new Cuisine();
     }
   }
 }
@@ -119,4 +123,14 @@ export class WeeklyPlanPurchaseList {
   thursday?: DayPlan
   tuesday?: DayPlan
   wednesday?: DayPlan
+
+  constructor() {
+    this.friday = new DayPlan();
+    this.monday = new DayPlan();
+    this.saturday = new DayPlan();
+    this.sunday = new DayPlan();
+    this.thursday = new DayPlan();
+    this.tuesday = new DayPlan();
+    this.wednesday = new DayPlan();
+  }
 }

+ 14 - 7
features/feature/src/main/ets/view/MainView.ets

@@ -127,16 +127,12 @@ export struct MainView {
             }
             .width('100%')
             .loop(false)
-            // .index($$this.currentIndex) // 状态变量
             .indicator(false)
             .loop(true)
             .duration(1000)
             .itemSpace(12)
             .nextMargin(15)
             .prevMargin(15)
-            // .onChange(() => {
-            //   this.loopSwitch = true;
-            // })
           }
           .width('100%')
           .padding({top: 8, bottom: 16 })
@@ -276,7 +272,10 @@ struct RecipeWidget{
 
   // 当前选中的菜品
   @Local selectCuisine: number = -1
+  // 当天的食谱
   @Local toDayPlan?: DayPlan
+  // 展开菜单时选择的是哪一餐 0 - 早餐 1 - 午餐 2 - 晚餐
+  @Local selectMeal: number = -1
 
   threeMeals: Array<BasicType<Cuisine>> = [
     {
@@ -349,23 +348,31 @@ struct RecipeWidget{
   // 展开菜单
   openMenu(text: string){
     let cuisine: Cuisine | undefined = this.getCuisine(text)
+    this.selectMeal = this.threeMeals.findIndex(item => item.text === text)
     if(cuisine) {
       this.selectCuisine = cuisine.id ?? -1
+    } else {
+      this.selectCuisine = -1
     }
   }
 
   // 选中修改菜品
   updateCuisine(id: number){
+    if(!this.toDayPlan) this.toDayPlan = new DayPlan()
+
     let origin = this.getToDayPlan(false)
     let cuisine: Cuisine | undefined = this.vm.getCuisineInfo(id)
-    if(this.toDayPlan?.breakfast?.id == this.selectCuisine) {
+    if(this.selectMeal == 0) {
       this.toDayPlan.breakfast = cuisine
+      if(!origin?.breakfast) origin!.breakfast = new Cuisine()
       origin!.breakfast = this.toDayPlan?.breakfast
-    } else if (this.toDayPlan?.lunch?.id == this.selectCuisine) {
+    } else if (this.selectMeal == 1) {
       this.toDayPlan.lunch = cuisine
+      if(!origin?.lunch) origin!.lunch = new Cuisine()
       origin!.lunch = this.toDayPlan?.lunch
-    } else if (this.toDayPlan?.dinner?.id == this.selectCuisine) {
+    } else if (this.selectMeal == 2) {
       this.toDayPlan.dinner = cuisine
+      if(!origin?.dinner) origin!.dinner = new Cuisine()
       origin!.dinner = this.toDayPlan?.dinner
     }
 

+ 4 - 1
features/feature/src/main/ets/viewModel/MainViewModel.ets

@@ -9,7 +9,7 @@ export class MainViewModel{
   @Trace safeTop: number = 0
   @Trace babyList: BabyInfo[] = []
   // 当前选择的宝宝信息
-  @Trace selectedBabyIndex: number = 0
+  @Trace selectedBabyIndex: number = -1
   // 当前的宝宝信息
   @Trace babyInfo: BabyInfo = AppStorageV2.connect<BabyInfo>(BabyInfo, () => new BabyInfo())!
   // 用户信息
@@ -61,6 +61,8 @@ export class MainViewModel{
 
   // 修改选中的宝宝信息
   async updateBabyInfo(index: number) {
+    if(index == this.selectedBabyIndex) return
+
     this.clearDataSource()
 
     BabyInfo.override(this.babyInfo, this.babyList[index])
@@ -101,6 +103,7 @@ export class MainViewModel{
         if(!startDate) startDate = YTDateUtil.formatDate(new Date(), DateFormat.UNDERLINE)
         this.allPlan = await BabyFoodApi.getWeeklyPlan(this.babyInfo.id, startDate)
         this.planList = this.allPlan.weeklyPlan
+        this.planList!.id = this.allPlan.id
       } catch (e){
         console.log(`e = ${JSON.stringify(e)}`)
       }