actions.ts 4.6 KB

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