actions.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { DBRecipe } from 'types/recipes';
  2. import { Meal, RecipePlan, Weekday, DBPlanner, PlannerMode, WeekPlan, DBDayPlan } from 'types/planner';
  3. import { Dispatch, ActionCreator, Action } from 'redux';
  4. import { getPlanner, savePlanner } from './services'
  5. import { ThunkAction } from 'redux-thunk';
  6. export const ADD_TO_BACKLOG = 'ADD_TO_BACKLOG';
  7. export const REMOVE_FROM_BACKLOG = 'REMOVE_FROM_BACKLOG';
  8. export const ASSIGN_TO_DAY = 'ASSIGN_TO_DAY';
  9. export const REMOVE_MEAL = 'REMOVE_MEAL';
  10. export const REQUEST_PLANNER = 'REQUEST_PLANNER';
  11. export const RECEIVE_PLANNER = 'RECEIVE_PLANNER';
  12. export const CHANGE_PLANNER_MODE = 'CHANGE_PLANNER_MODE';
  13. export const EDIT_PLANNER = 'EDIT_PLANNER';
  14. export const PENDING_SAVE_PLANNER = 'PENDING_SAVE_PLANNER';
  15. export const CONFIRM_SAVE_PLANNER = 'CONFIRM_SAVE_PLANNER';
  16. export const REJECT_SAVE_PLANNER = 'REJECT_SAVE_PLANNER';
  17. export interface AddToBacklogAction extends Action<'ADD_TO_BACKLOG'> {
  18. recipe: {
  19. _id: string,
  20. name: string
  21. }
  22. }
  23. const addToBacklog = (recipe: DBRecipe): AddToBacklogAction => ({
  24. recipe: {
  25. _id: recipe._id,
  26. name: recipe.name
  27. },
  28. type: ADD_TO_BACKLOG,
  29. });
  30. export interface RemoveFromBacklogAction extends Action<'REMOVE_FROM_BACKLOG'> {
  31. recipe: RecipePlan
  32. }
  33. const removeFromBacklog = (recipe: RecipePlan): RemoveFromBacklogAction => ({
  34. type: REMOVE_FROM_BACKLOG,
  35. recipe,
  36. });
  37. export interface AssignToDayAction extends Action<'ASSIGN_TO_DAY'> {
  38. recipe: RecipePlan,
  39. day: Weekday,
  40. meal: Meal
  41. }
  42. const assignToDay = (recipe: RecipePlan, day: Weekday, meal: Meal): AssignToDayAction => ({
  43. type: ASSIGN_TO_DAY,
  44. recipe,
  45. day,
  46. meal,
  47. });
  48. export interface RemoveMealAction extends Action<'REMOVE_MEAL'> {
  49. day: Weekday,
  50. meal: Meal
  51. }
  52. const removeMeal = (day: Weekday, meal: Meal): RemoveMealAction => ({
  53. type: REMOVE_MEAL,
  54. day,
  55. meal
  56. });
  57. export interface RequestPlannerAction extends Action<'REQUEST_PLANNER'> {
  58. week: number
  59. }
  60. const requestPlanner = (week: number): RequestPlannerAction => ({
  61. type: REQUEST_PLANNER,
  62. week
  63. })
  64. export interface ReceivePlannerAction extends Action<'RECEIVE_PLANNER'>{
  65. payload: DBPlanner,
  66. }
  67. const receivePlanner = (json: DBPlanner): ReceivePlannerAction => ({
  68. type: RECEIVE_PLANNER,
  69. payload: json
  70. })
  71. export const fetchPlannerActionCreator: ActionCreator<
  72. ThunkAction<
  73. Promise<ReceivePlannerAction>,
  74. DBPlanner,
  75. number,
  76. ReceivePlannerAction
  77. >
  78. > = (week: number) => {
  79. return async (dispatch: Dispatch) => {
  80. dispatch(requestPlanner(week));
  81. const planner = await getPlanner(week);
  82. return dispatch(receivePlanner(planner))
  83. }
  84. }
  85. export interface ChangePlannerModeAction extends Action<'CHANGE_PLANNER_MODE'> {
  86. mode: PlannerMode
  87. }
  88. export const changePlannerMode = (mode: PlannerMode): ChangePlannerModeAction => ({
  89. type: CHANGE_PLANNER_MODE,
  90. mode,
  91. });
  92. export interface EditPlannerAction extends Action<'EDIT_PLANNER'> {
  93. }
  94. export const editPlanner = (): EditPlannerAction => ({
  95. type: EDIT_PLANNER
  96. });
  97. export interface PendingSavePlannerAction extends Action<'PENDING_SAVE_PLANNER'> {
  98. planner: WeekPlan,
  99. from: Date,
  100. to: Date
  101. }
  102. export const pendingSavePlanner = (planner: WeekPlan, from: Date, to: Date): PendingSavePlannerAction => ({
  103. type: PENDING_SAVE_PLANNER,
  104. planner,
  105. from,
  106. to,
  107. })
  108. export interface ConfirmSavePlannerAction extends Action<'CONFIRM_SAVE_PLANNER'> {
  109. result: DBDayPlan[]
  110. }
  111. export const confirmSavePlanner = (result: DBDayPlan[]): ConfirmSavePlannerAction => ({
  112. type: CONFIRM_SAVE_PLANNER,
  113. result
  114. })
  115. export interface RejectSavePlannerAction extends Action<'REJECT_SAVE_PLANNER'> {
  116. error: Error
  117. }
  118. export const rejectSavePlanner = (error: Error): RejectSavePlannerAction => ({
  119. type: REJECT_SAVE_PLANNER,
  120. error,
  121. })
  122. export const savePlannerActionCreator: ActionCreator<
  123. ThunkAction<
  124. Promise<ConfirmSavePlannerAction|RejectSavePlannerAction>,
  125. DBDayPlan[],
  126. WeekPlan,
  127. ConfirmSavePlannerAction|RejectSavePlannerAction
  128. >
  129. > = (weekplan: WeekPlan, from: Date, to: Date) => {
  130. return async (dispatch: Dispatch) => {
  131. dispatch(pendingSavePlanner(weekplan, from, to))
  132. try {
  133. const result = await savePlanner(weekplan, from, to);
  134. return dispatch(confirmSavePlanner(result))
  135. } catch(error) {
  136. return dispatch(rejectSavePlanner(error))
  137. }
  138. }
  139. }
  140. export type PlannerActions =
  141. AddToBacklogAction |
  142. RemoveFromBacklogAction |
  143. AssignToDayAction |
  144. RemoveMealAction |
  145. RequestPlannerAction |
  146. ReceivePlannerAction |
  147. ChangePlannerModeAction |
  148. EditPlannerAction |
  149. PendingSavePlannerAction |
  150. ConfirmSavePlannerAction |
  151. RejectSavePlannerAction;
  152. export default {
  153. addToBacklog,
  154. removeFromBacklog,
  155. assignToDay,
  156. removeMeal,
  157. changePlannerMode,
  158. editPlanner
  159. }