All files / src/store actionCreators.ts

20.68% Statements 6/29
0% Branches 0/2
0% Functions 0/18
20.68% Lines 6/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                              1x                             1x                               1x                               1x                           1x                         1x          
import {Activity, ActivityControllerService, LoginControllerService} from "../generated/api";
import {
    ADD_ACTIVITY,
    ADD_ACTIVITY_RQ,
    EDIT_ACTIVITY,
    EDIT_ACTIVITY_RQ,
    FETCH_ACTIVITY_RQ,
    LOGIN,
    LOGIN_RQ,
    LOGIN_USERNAME,
    REMOVE_ACTIVITY,
    REMOVE_ACTIVITY_RQ,
    SET_ACTIVITIES
} from "../type";
import {Dispatch} from "react";
 
/**
 * add activity action creator. It creates a thunk and dispatches two actions for informing the store about the starting
 * of the request and the final response or error
 *
 * @param activity the {@link Activity} to be add
 * @param dispatch the thunk
 */
export const addActivity = (activity: Activity, dispatch: Dispatch<any>) => {
    dispatch({type: ADD_ACTIVITY_RQ})
    ActivityControllerService.add(activity)
        .then((value: Activity) => dispatch({type: ADD_ACTIVITY, payload: value}))
        .catch(reason => dispatch({type: "ERROR", payload: reason}))
}
 
 
/**
 * fetch activities action creator: retrieves all {@link Activity}s from the server
 * It creates a thunk and dispatches two actions for informing the store about the starting
 * of the request and the final response or error
 *
 * @param dispatch the thunk
 */
export const fetchActivities = (dispatch: Dispatch<any>) => {
    dispatch({type: FETCH_ACTIVITY_RQ})
    ActivityControllerService.activities()
        // value can be undefined: the rest return 204 (no content)
        .then((value: Activity[]) => dispatch({type: SET_ACTIVITIES, payload: !value ? [] : value  }))
        .catch(reason => dispatch({type: "ERROR", payload: reason}))
}
 
/**
 * edit activity action creator: put on the server an edited {@link Activity}
 * It creates a thunk and dispatches two actions for informing the store about the starting
 * of the request and the final response or error
 *
 * @param activity the {@link Activity} to be send to the server
 * @param dispatch the thunk
 */
export const editActivity = (activity: Activity, dispatch: Dispatch<any>) => {
    dispatch({type: EDIT_ACTIVITY_RQ, payload: activity})
    ActivityControllerService.edit(activity)
        .then((value: Activity) => dispatch({type: EDIT_ACTIVITY, payload: value}))
        .catch(reason => dispatch({type: "ERROR", payload: reason}))
}
 
/**
 * remove activity action creator: deletes from the server an {@link Activity}
 * It creates a thunk and dispatches two actions for informing the store about the starting
 * of the request and the final response or error
 *
 * @param activity the {@link Activity} to be send to the server
 * @param dispatch the thunk
 */
 
export const removeActivity = (activity: Activity, dispatch: Dispatch<any>) => {
    dispatch({type: REMOVE_ACTIVITY_RQ, payload: activity})
    ActivityControllerService.remove(activity.id!)
        .then((value: Activity) => dispatch({type: REMOVE_ACTIVITY, payload: value}))
        .catch(reason => dispatch({type: "ERROR", payload: reason}))
}
 
 
/**
 * execute a simple basic authenticated login (only for demostration)
 *
 * @param dispatch the thunk
 */
 
export const login = (dispatch: Dispatch<any>) => {
    dispatch({type: LOGIN_RQ})
    LoginControllerService.simpleBasicLogin()
        .then((value: string) => dispatch({type: LOGIN, payload: value}))
        .catch(reason => dispatch({type: "ERROR", payload: reason}))
}
 
/**
 * execute a simple basic authenticated login (only for demostration)
 *
 * @param dispatch the thunk
 */
 
export const loginUserName = (dispatch: Dispatch<any>) => {
    LoginControllerService.loggedUserName()
        .then((value: string) => dispatch({type: LOGIN_USERNAME, payload: value}))
        .catch(reason => dispatch({type: "ERROR", payload: reason}))
}