From 0950f264e84a851f3e13ccf0c4905ddca8ec7a5b Mon Sep 17 00:00:00 2001 From: Alexander Wong Date: Sun, 22 Apr 2018 18:23:43 -0600 Subject: [PATCH] working page pagination controls --- src/actions/cShift/saga.actions.js | 12 +- src/api/cShift.api.js | 14 +- .../User/Client/ClientAddShiftForm.jsx | 4 - src/components/User/Client/ClientShifts.jsx | 128 +++++++++++++++--- src/constants/cShift.constants.js | 1 + src/sagas/cShift.sagas.js | 25 +++- src/sagas/index.js | 8 +- 7 files changed, 153 insertions(+), 39 deletions(-) diff --git a/src/actions/cShift/saga.actions.js b/src/actions/cShift/saga.actions.js index 7205cee..319d642 100644 --- a/src/actions/cShift/saga.actions.js +++ b/src/actions/cShift/saga.actions.js @@ -1,4 +1,7 @@ -import { CREATE_MULTIPLE_CSHIFT_REQUEST } from "../../constants/cShift.constants"; +import { + CREATE_MULTIPLE_CSHIFT_REQUEST, + GET_CSHIFTS_REQUEST +} from "../../constants/cShift.constants"; /** * Saga action for creating a list of shifts @@ -10,3 +13,10 @@ export function createMultipleCShiftRequest(postBodies) { data: postBodies }; } + +export function getCShiftsRequest(params) { + return { + type: GET_CSHIFTS_REQUEST, + data: params + }; +} diff --git a/src/api/cShift.api.js b/src/api/cShift.api.js index bf1c4c5..fbdba4a 100644 --- a/src/api/cShift.api.js +++ b/src/api/cShift.api.js @@ -1,15 +1,9 @@ -import { post } from "./baseApi"; +import { get, post } from "./baseApi"; export function createCShifts(postBodies) { - const cShiftUrl = `/cshift/`; - // return Promise.all( - // postBodies.map(postBody => post(cShiftUrl, postBody).catch(() => false)) - // ).then(postResponses => { - // Promise.resolve(postResponses); - // }); return Promise.all( postBodies.map(postBody => - post(cShiftUrl, postBody).catch(err => { + post("/cshift/", postBody).catch(err => { return { error: true, ...err }; }) ) @@ -17,3 +11,7 @@ export function createCShifts(postBodies) { return Promise.resolve(resp); }); } + +export function getCShifts(params) { + return get("/cshift/", params).then(resp => Promise.resolve(resp)); +} diff --git a/src/components/User/Client/ClientAddShiftForm.jsx b/src/components/User/Client/ClientAddShiftForm.jsx index fab0295..5ddc95a 100644 --- a/src/components/User/Client/ClientAddShiftForm.jsx +++ b/src/components/User/Client/ClientAddShiftForm.jsx @@ -132,10 +132,6 @@ class ClientAddShiftForm extends Component { return ; } - if (cShiftRequestSuccess) { - console.log(cShiftRequestSuccess); - } - const employeeChoices = selfUser.client.employees .filter(employee => !employee.deleted && !!employee.approved) .map(({ uuid, provider }) => ({ diff --git a/src/components/User/Client/ClientShifts.jsx b/src/components/User/Client/ClientShifts.jsx index 8705631..5a8194e 100644 --- a/src/components/User/Client/ClientShifts.jsx +++ b/src/components/User/Client/ClientShifts.jsx @@ -1,13 +1,62 @@ import React, { Component } from "react"; import { connect } from "react-redux"; import { Redirect, Link } from "react-router-dom"; -import { Button, Container, Header, Segment } from "semantic-ui-react"; +import { + Button, + Container, + Header, + Item, + Segment, + Pagination +} from "semantic-ui-react"; +import { getCShiftsRequest } from "../../../actions/cShift/saga.actions"; class ClientShifts extends Component { + constructor(props) { + super(props); + this.state = { + page: 1, + pageSize: 10 // client can't control this, but set here just in case + }; + } + + componentWillMount = () => { + this.props.dispatch( + getCShiftsRequest({ + page: this.state.page, + page_size: this.state.pageSize + }) + ); + }; + + handlePaginationChange = (event, { activePage }) => { + this.props.dispatch( + getCShiftsRequest({ + page: activePage, + page_size: this.state.pageSize + }) + ); + this.setState({ page: activePage }); + }; + render() { - const { selfUser } = this.props; + const { + isSendingCShiftRequest, + cShiftRequestSuccess, + selfUser + } = this.props; + const { page, pageSize } = this.state; if (selfUser.client) { - return ; + return ( + + ); } else { return ; } @@ -15,25 +64,62 @@ class ClientShifts extends Component { } function mapStateToProps(state) { - return { selfUser: state.user.selfUser }; + return { ...state.cShift, selfUser: state.user.selfUser }; } -const ClientShiftsView = ({ user }) => ( - -
Shifts
- - - -

Todo: List Shifts

-
-); +const ClientShiftsView = ({ + isSendingCShiftRequest, + cShiftRequestSuccess, + user, + page, + pageSize, + handlePaginationChange +}) => { + const { count = 0, results = [] } = cShiftRequestSuccess; + + return ( + +
Shifts
+ + + + {!isSendingCShiftRequest && + results.length > 0 && ( + + {results.map(result => ( + + + + {JSON.stringify(result, null, 2)} + + + ))} + + )} +
+ +
+
+ ); +}; export default connect(mapStateToProps)(ClientShifts); diff --git a/src/constants/cShift.constants.js b/src/constants/cShift.constants.js index ea88f65..9313361 100644 --- a/src/constants/cShift.constants.js +++ b/src/constants/cShift.constants.js @@ -14,3 +14,4 @@ export const SET_CLEAR_CSHIFT_STATE = "SET_CLEAR_CSHIFT_STATE"; // Saga CShift Action Constants export const CREATE_MULTIPLE_CSHIFT_REQUEST = "CREATE_MULTIPLE_CSHIFT_REQUEST"; +export const GET_CSHIFTS_REQUEST = "GET_CSHIFTS_REQUEST"; diff --git a/src/sagas/cShift.sagas.js b/src/sagas/cShift.sagas.js index 35f3a08..ab407f0 100644 --- a/src/sagas/cShift.sagas.js +++ b/src/sagas/cShift.sagas.js @@ -13,7 +13,7 @@ import { setFormShiftNote, setFormShiftStartTime } from "../actions/cShift/reducer.actions"; -import { createCShifts } from "../api/cShift.api"; +import { createCShifts, getCShifts } from "../api/cShift.api"; function* createCShiftsCall(postBodies) { yield effects.put(isSendingCShiftRequest(true)); @@ -36,15 +36,25 @@ function* createCShiftsCall(postBodies) { } } +function* getCShiftsCall(params) { + yield effects.put(isSendingCShiftRequest(true)); + try { + return yield effects.call(getCShifts, params); + } catch (exception) { + yield effects.put(setCShiftRequestError(exception)); + return false; + } finally { + yield effects.put(isSendingCShiftRequest(false)); + } +} + export function* createCShiftsFlow(request) { yield effects.put(clearCShiftRequestSuccess()); yield effects.put(clearCShiftRequestError()); const arrResponses = yield effects.call(createCShiftsCall, request.data); - console.log(arrResponses); if (arrResponses) { const errorResps = arrResponses.filter(resp => !!resp.error); if (errorResps.length > 0) { - console.log(arrResponses); yield effects.put(setCShiftRequestErrors(errorResps)); } const succResps = arrResponses.filter(resp => !resp.error); @@ -59,3 +69,12 @@ export function* createCShiftsFlow(request) { } } } + +export function* getCShiftsFlow(request) { + yield effects.put(clearCShiftRequestSuccess()); + yield effects.put(clearCShiftRequestError()); + const isSuccessful = yield effects.call(getCShiftsCall, request.data); + if (isSuccessful) { + yield effects.put(setCShiftRequestSuccess(isSuccessful)); + } +} diff --git a/src/sagas/index.js b/src/sagas/index.js index 379b10b..0a28cac 100644 --- a/src/sagas/index.js +++ b/src/sagas/index.js @@ -69,8 +69,11 @@ import { updatePriceFlow, deletePriceFlow } from "./price.sagas"; -import { CREATE_MULTIPLE_CSHIFT_REQUEST } from "../constants/cShift.constants"; -import { createCShiftsFlow } from "./cShift.sagas"; +import { + CREATE_MULTIPLE_CSHIFT_REQUEST, + GET_CSHIFTS_REQUEST +} from "../constants/cShift.constants"; +import { createCShiftsFlow, getCShiftsFlow } from "./cShift.sagas"; export default function* rootSaga() { yield takeLatest(SEND_REGISTER_REQUEST, registerUserFlow); @@ -99,4 +102,5 @@ export default function* rootSaga() { yield takeLatest(UPDATE_PRICE_REQUEST, updatePriceFlow); yield takeLatest(DELETE_PRICE_REQUEST, deletePriceFlow); yield takeLatest(CREATE_MULTIPLE_CSHIFT_REQUEST, createCShiftsFlow); + yield takeLatest(GET_CSHIFTS_REQUEST, getCShiftsFlow); }