actions.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. import { async } from 'q';
  7. export const ADD_TO_BACKLOG = 'ADD_TO_BACKLOG';
  8. export const REMOVE_FROM_BACKLOG = 'REMOVE_FROM_BACKLOG';
  9. export const ASSIGN_TO_DAY = 'ASSIGN_TO_DAY';
  10. export const REMOVE_MEAL = 'REMOVE_MEAL';
  11. export const REQUEST_PLANNER = 'REQUEST_PLANNER';
  12. export const RECEIVE_PLANNER = 'RECEIVE_PLANNER';
  13. export const CHANGE_PLANNER_MODE = 'CHANGE_PLANNER_MODE';
  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 PendingSavePlannerAction extends Action<'PENDING_SAVE_PLANNER'> {
  93. planner: WeekPlan
  94. }
  95. export const pendingSavePlanner = (planner: WeekPlan): PendingSavePlannerAction => ({
  96. type: PENDING_SAVE_PLANNER,
  97. planner,
  98. })
  99. export interface ConfirmSavePlannerAction extends Action<'CONFIRM_SAVE_PLANNER'> {
  100. result: DBDayPlan[]
  101. }
  102. export const confirmSavePlanner = (result: DBDayPlan[]): ConfirmSavePlannerAction => ({
  103. type: CONFIRM_SAVE_PLANNER,
  104. result
  105. })
  106. export interface RejectSavePlannerAction extends Action<'REJECT_SAVE_PLANNER'> {
  107. error: Error
  108. }
  109. export const rejectSavePlanner = (error: Error): RejectSavePlannerAction => ({
  110. type: REJECT_SAVE_PLANNER,
  111. error,
  112. })
  113. export const savePlannerActionCreator: ActionCreator<
  114. ThunkAction<
  115. Promise<ConfirmSavePlannerAction|RejectSavePlannerAction>,
  116. DBDayPlan[],
  117. WeekPlan,
  118. ConfirmSavePlannerAction|RejectSavePlannerAction
  119. >
  120. > = (weekplan: WeekPlan) => {
  121. return async (dispatch: Dispatch) => {
  122. dispatch(pendingSavePlanner(weekplan))
  123. try {
  124. const result = await savePlanner(weekplan);
  125. return dispatch(confirmSavePlanner(result))
  126. } catch(error) {
  127. return dispatch(rejectSavePlanner(error))
  128. }
  129. }
  130. }
  131. export type PlannerActions =
  132. AddToBacklogAction |
  133. RemoveFromBacklogAction |
  134. AssignToDayAction |
  135. RemoveMealAction |
  136. RequestPlannerAction |
  137. ReceivePlannerAction |
  138. ChangePlannerModeAction |
  139. PendingSavePlannerAction |
  140. ConfirmSavePlannerAction |
  141. RejectSavePlannerAction;
  142. export default {
  143. addToBacklog,
  144. removeFromBacklog,
  145. assignToDay,
  146. removeMeal,
  147. changePlannerMode,
  148. }