Przeglądaj źródła

Fix joinPlanner logic

Tatiana Inama 7 lat temu
rodzic
commit
3cc54f28f6

+ 21 - 18
recipes/src/containers/Planner/index.tsx

@@ -13,34 +13,17 @@ import './styles.scss';
 import { getWeekDay } from 'services/time';
 import Button from 'components/Button';
 import { ThunkDispatch } from 'redux-thunk';
-import { Action, AnyAction } from 'redux';
 
 type Actions = typeof PlannerActions
 
 interface PlannerContainerProps extends RouteComponentProps, PlannerState, Actions {
-  fetch: any
+  fetch: typeof fetchPlannerActionCreator
 }
 
 interface PlannerContainerState {
   week: [Weekday, string][]
 }
 
-const DisplayMeal = (dayPlan: DayPlan, meal: Meal, onRemove: typeof PlannerActions.removeMeal) => {
-  const dish = dayPlan ? dayPlan[meal] : dayPlan;
-  if (dish !== undefined) {
-    return (
-      <div className='meal-card'>
-        <div className='meal-card--actions'>
-          <Button icon='clear' onClick={() => onRemove(getWeekDay(dayPlan.date), meal)} small></Button>
-        </div>
-        <h5>{dish.name}</h5>
-      </div>
-    )
-  } else {
-    return null;
-  }
-};
-
 class PlannerContainer extends Component<PlannerContainerProps, PlannerContainerState> {
 
   constructor(props: PlannerContainerProps) {
@@ -54,6 +37,10 @@ class PlannerContainer extends Component<PlannerContainerProps, PlannerContainer
     this.props.fetch(this.props.week);
   }
 
+  componentDidUpdate() {
+    const { isFetching } = this.props;
+  }
+
   assignRecipe = (result: DropResult) => {
     const recipe = this.findRecipe(result.draggableId);
     if (result.destination && recipe) {
@@ -158,6 +145,22 @@ class PlannerContainer extends Component<PlannerContainerProps, PlannerContainer
   }
 }
 
+const DisplayMeal = (dayPlan: DayPlan, meal: Meal, onRemove: typeof PlannerActions.removeMeal) => {
+  const dish = dayPlan ? dayPlan[meal] : dayPlan;
+  if (dish !== undefined) {
+    return (
+      <div className='meal-card'>
+        <div className='meal-card--actions'>
+          <Button icon='clear' onClick={() => onRemove(getWeekDay(dayPlan.date), meal)} small></Button>
+        </div>
+        <h5>{dish.name}</h5>
+      </div>
+    )
+  } else {
+    return null;
+  }
+};
+
 const mapStateToProps = (state: AppState) => {
   return state.planner
 }

+ 4 - 3
recipes/src/containers/Planner/reducers.ts

@@ -34,9 +34,10 @@ const initialState: PlannerState = {
   ]
 }
 
-const joinPlanner = (old: WeekPlan, updated: DBPlanner): WeekPlan => Object.keys(old).reduce((dayplan, day) => {
-  return merge(dayplan, updated[day as Weekday])
-}, {...old});
+const joinPlanner = (old: WeekPlan, updated: DBPlanner): WeekPlan => Object.keys(old).reduce((_oldPlanner, day) => ({
+  ..._oldPlanner,
+  [day]: merge(_oldPlanner[day as Weekday], updated[day as Weekday])
+}), {...old});
 
 const PlannerReducer: Reducer<PlannerState, PlannerActions> = (
   state = initialState,