Skip to content

Instantly share code, notes, and snippets.

@matheusnascgomes
Created February 9, 2019 18:42
Show Gist options
  • Select an option

  • Save matheusnascgomes/1fb936812f171a72eb1743f58150cdd8 to your computer and use it in GitHub Desktop.

Select an option

Save matheusnascgomes/1fb936812f171a72eb1743f58150cdd8 to your computer and use it in GitHub Desktop.
Estruta Redux + Redux Saga
export const Types = {
GET_REQUEST: 'ride/GET_REQUEST',
GET_SUCCESS: 'ride/GET_SUCCESS',
};
const INITIAL_STATE = {
data: null,
};
export default function rideDetails(state = INITIAL_STATE, action) {
switch (action.type) {
case Types.GET_REQUEST:
return { ...state };
case Types.GET_SUCCESS:
return { ...state, data: action.payload.data };
default:
return state;
}
}
export const Creators = {
getRideDetailsRequest: rideId => ({
type: Types.GET_REQUEST,
payload: { rideId }
}),
getRideDetailsSuccess: data => ({
type: Types.GET_SUCCESS,
payload: { data },
}),
};
import { call, put } from 'redux-saga/effects';
import api from '../../../../services/api';
import { Creators as RideDetailsActions } from '../ducks/rideDetails';
export function* getRideDetails(action) {
try {
const { rideId } = action.payload;
if(!rideId)
return false;
const response = yield call(api.get, `trip/scheduled/list/${ rideId }`);
const { arrPayload } = response.data;
yield put(RideDetailsActions.getRideDetailsSuccess(arrPayload));
} catch (err) {
console.error(err);
}
}
export const Types = {
GET_REQUEST: 'ride/GET_REQUEST',
GET_SUCCESS: 'ride/GET_SUCCESS',
};
const INITIAL_STATE = {
data: null,
};
export default function rides(state = INITIAL_STATE, action) {
switch (action.type) {
case Types.GET_SUCCESS:
return { ...state, data: action.payload.data };
default:
return state;
}
}
export const Creators = {
getRidesRequest: driverId => ({
type: Types.GET_REQUEST,
payload: { driverId }
}),
getRidesSuccess: data => ({
type: Types.GET_SUCCESS,
payload: { data },
}),
};
import { call, put } from 'redux-saga/effects';
import api from '../../../../services/api';
import { Creators as Rides } from '../ducks/rides';
export function* getRides(action) {
try {
const response = yield call(api.get, `driver/invitations/${ action.payload.driverId }`);
const { arrPayload } = response.data;
yield put(Rides.getRidesSuccess(arrPayload));
} catch (err) {
console.error(err);
}
}
import { all, takeLatest } from 'redux-saga/effects';
import { Types as RidesTypes } from '../../OffDriver/Rides/redux/ducks/rides';
import { Types as RideDetailsTypes } from '../../OffDriver/Rides/redux/ducks/rideDetails';
import { getRides } from '../../OffDriver/Rides/redux/sagas/rides';
import { getRideDetails } from '../../OffDriver/Rides/redux/sagas/rideDetails';
export default function* rootSaga() {
yield all([
takeLatest(RidesTypes.GET_REQUEST, getRides),
takeLatest(RideDetailsTypes.GET_REQUEST, getRideDetails),
])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment