Преглед изворни кода

Only update planner when editions were made

Tatiana Inama пре 7 година
родитељ
комит
1fe5a46e13

+ 3 - 3
ktchn/src/mongo.ts

@@ -1,4 +1,4 @@
-import { Db, Collection, ObjectID, FilterQuery, ObjectId, WriteOpResult, FindOneOptions, UpdateWriteOpResult } from 'mongodb';
+import { Db, Collection, ObjectID, FilterQuery, ObjectId, WriteOpResult, FindOneOptions } from 'mongodb';
 
 export interface IDDocument {
   _id: ObjectID
@@ -13,7 +13,7 @@ export interface IMongoService {
   findOneById<T>(idParam: string): Promise<IDBDocument<T>|null>,
   find<T>(query: FilterQuery<T>, optionalQuery?: FindOneOptions): Promise<IDBDocument<T>[]>,
   update<T>(id: string, data: T): Promise<WriteOpResult>,
-  updateOne<T>(filter: FilterQuery<T>, data: T, options?: {upsert?: boolean}): Promise<UpdateWriteOpResult>,
+  updateOne<T>(filter: FilterQuery<T>, data: T, options?: {upsert?: boolean}): Promise<WriteOpResult>,
   aggregate<T>(pipeline: Object[]): Promise<T[]>
 }
 
@@ -27,7 +27,7 @@ export const mongoService = (db: Db) => (col: string): IMongoService => {
     findOneById: (idParam: string) => collection.findOne({ _id: new ObjectId(idParam) }),
     find: (query, optionalQuery) => collection.find(query, optionalQuery).toArray(),
     update: (id, data) => collection.update({_id: new ObjectId(id)}, data),
-    updateOne: (filter, data, options) => collection.updateOne(filter, data, options), 
+    updateOne: (filter, data, options) => collection.update(filter, data, options), 
     aggregate: pipeline => collection.aggregate(pipeline).toArray()
   }
 };

+ 2 - 2
ktchn/src/planner/controller.ts

@@ -1,4 +1,4 @@
-import { WriteOpResult, UpdateWriteOpResult } from 'mongodb';
+import { WriteOpResult } from 'mongodb';
 import { IMongoService } from '../mongo';
 import { ChainPController } from '../promise-all-middleware';
 import PlanDB, { WeeklyPlanner, CompletePlanDB, Weekday, Plan, CompactWeeklyPlanner } from './model';
@@ -110,7 +110,7 @@ const savePlan: Controller<void, PlanDB> = db => req => prevResult => {
   return validPlan(plan).then(db.insertOne)
 }
 
-const saveManyPlans: Controller<void, UpdateWriteOpResult[]> = db => req => async () => {
+const saveManyPlans: Controller<void, WriteOpResult[]> = db => req => async () => {
   const mbPlanner = req.body;
   if (Array.isArray(mbPlanner)) {
     const plans = await Promise.all(mbPlanner.map(validPlan));

+ 9 - 1
recipes/src/containers/Planner/actions.ts

@@ -14,6 +14,7 @@ export const REQUEST_PLANNER = 'REQUEST_PLANNER';
 export const RECEIVE_PLANNER = 'RECEIVE_PLANNER';
 
 export const CHANGE_PLANNER_MODE = 'CHANGE_PLANNER_MODE';
+export const EDIT_PLANNER = 'EDIT_PLANNER';
 
 export const PENDING_SAVE_PLANNER = 'PENDING_SAVE_PLANNER';
 export const CONFIRM_SAVE_PLANNER = 'CONFIRM_SAVE_PLANNER';
@@ -102,6 +103,12 @@ export const changePlannerMode = (mode: PlannerMode): ChangePlannerModeAction =>
   mode,
 });
 
+export interface EditPlannerAction extends Action<'EDIT_PLANNER'> {
+}
+export const editPlanner = (): EditPlannerAction => ({
+  type: EDIT_PLANNER
+});
+
 export interface PendingSavePlannerAction extends Action<'PENDING_SAVE_PLANNER'> {
   planner: WeekPlan
 }
@@ -154,6 +161,7 @@ export type PlannerActions =
   RequestPlannerAction |
   ReceivePlannerAction |
   ChangePlannerModeAction |
+  EditPlannerAction |
   PendingSavePlannerAction |
   ConfirmSavePlannerAction |
   RejectSavePlannerAction;
@@ -164,5 +172,5 @@ export default {
   assignToDay,
   removeMeal,
   changePlannerMode,
-  
+  editPlanner  
 }

+ 10 - 22
recipes/src/containers/Planner/index.tsx

@@ -29,7 +29,6 @@ class PlannerContainer extends Component<PlannerContainerProps, PlannerContainer
 
   constructor(props: PlannerContainerProps) {
     super(props);
-    console.log(props)
     this.state = {
       week: Object.keys(props.planner).map((weekday) => ([ weekday as Weekday, moment(props.planner[weekday as Weekday].date).format()]))
     }
@@ -49,9 +48,15 @@ class PlannerContainer extends Component<PlannerContainerProps, PlannerContainer
       const [idx, day, meal] = result.destination.droppableId.split('-');
       this.props.assignToDay(recipe, day as Weekday, meal as Meal);
       this.props.removeFromBacklog(recipe);
+      this.props.editPlanner();
     }
   }
 
+  removeMeal = (day: Weekday, meal: Meal) => {
+    this.props.editPlanner();
+    return this.props.removeMeal(day, meal);
+  }
+
   findRecipe = (recipeId: string) => this.props.backlog.find(recipe => recipe._id === recipeId);
 
   changeMode = (mode: PlannerMode) => this.props.changePlannerMode(mode);
@@ -67,10 +72,9 @@ class PlannerContainer extends Component<PlannerContainerProps, PlannerContainer
               <Button outlined raised onClick={() => this.changeMode('edit')}>Edit</Button>
               ) : (
               <Button outlined raised onClick={() => {
-                console.log('====================================');
-                console.log(this.props.planner);
-                console.log('====================================');
-                this.props.save(this.props.planner)
+                if (this.props.edit) {
+                  this.props.save(this.props.planner)
+                }
                 this.changeMode('view')
               }}>Save</Button>
             )
@@ -83,29 +87,13 @@ class PlannerContainer extends Component<PlannerContainerProps, PlannerContainer
           onDragEnd={this.assignRecipe}
           backlog={this.props.backlog}
           planner={this.props.planner}
-          removeMeal={this.props.removeMeal}
+          removeMeal={this.removeMeal}
         />
       </div>
     );
   }
 }
 
-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 DisplayMealWithActions: React.SFC<{
   recipe?: RecipePlan,
   children?: React.ReactElement

+ 6 - 0
recipes/src/containers/Planner/reducers.ts

@@ -9,6 +9,7 @@ const initialState: PlannerState = {
   mode: 'view',
   isFetching: false,
   saving: false,
+  edit: false,
   from: mkWeekDay(1),
   to: mkWeekDay(7),
   week: getWeekNumber(),
@@ -101,6 +102,11 @@ const PlannerReducer: Reducer<PlannerState, PlannerActions> = (
         ...state,
         mode: action.mode
       }
+    case 'EDIT_PLANNER':
+      return {
+        ...state,
+        edit: true,
+      }
     case 'PENDING_SAVE_PLANNER':
       return {
         ...state,

+ 0 - 1
recipes/src/containers/Planner/services.ts

@@ -8,7 +8,6 @@ const PLANNER: string = process.env.REACT_APP_API_PLANNER;
 const toDBPlan = (weekplan: WeekPlan) => {
   const planner = Object.keys(weekplan).map((weekday) => weekplan[weekday as Weekday]);
   const mkPlan = (day: DayPlan, meal: Meal): Array<DBDayPlan> => {
-    console.log(day);
     const dish = day[meal];
     const date = moment(day.date);
     return dish ? [{

+ 2 - 1
recipes/src/types/planner.ts

@@ -37,7 +37,8 @@ export interface PlannerState {
   from: Moment,
   to: Moment,
   planner: WeekPlan,
-  backlog: RecipePlan[]
+  backlog: RecipePlan[],
+  edit: boolean,
 }
 
 export interface DBDayPlan {