functionality for client users to add providers

master
Alexander Wong 6 years ago
parent 95d86161ec
commit bc0628bcb7
No known key found for this signature in database
GPG Key ID: EBFE6371FA6A79DC
  1. 73
      src/actions/employee/reducer.actions.js
  2. 26
      src/actions/employee/saga.actions.js
  3. 40
      src/api/employee.api.js
  4. 10
      src/components/App.jsx
  5. 10
      src/components/App.test.js
  6. 20
      src/components/Navbar.jsx
  7. 11
      src/components/Shared/Error.jsx
  8. 125
      src/components/User/Client/ClientAddProviderForm.jsx
  9. 92
      src/components/User/Client/ClientProviders.jsx
  10. 15
      src/constants/employee.constants.js
  11. 73
      src/reducers/employeeReducer.js
  12. 4
      src/reducers/index.js
  13. 100
      src/sagas/employee.sagas.js
  14. 13
      src/sagas/index.js

@ -0,0 +1,73 @@
import {
IS_SENDING_EMPLOYEE_REQUEST,
SET_EMPLOYEE_REQUEST_ERROR,
CLEAR_EMPLOYEE_REQUEST_ERROR,
SET_EMPLOYEE_REQUEST_SUCCESS,
CLEAR_EMPLOYEE_REQUEST_SUCCESS,
SET_EMPLOYEE_UUID,
SET_FORM_EMPLOYEE_EMAIL,
SET_FORM_EMPLOYEE_NOTE,
SET_CLEAR_EMPLOYEE_STATE
} from "../../constants/employee.constants";
import { parseError } from "../common.actions";
export function isSendingEmployeeRequest(sendingRequest) {
return {
type: IS_SENDING_EMPLOYEE_REQUEST,
data: sendingRequest
};
}
export function setEmployeeRequestError(exception) {
let error = parseError(exception);
return {
type: SET_EMPLOYEE_REQUEST_ERROR,
data: error
};
}
export function clearEmployeeRequestError() {
return {
type: CLEAR_EMPLOYEE_REQUEST_ERROR
};
}
export function setEmployeeRequestSuccess(response) {
return {
type: SET_EMPLOYEE_REQUEST_SUCCESS,
data: response.detail || response
};
}
export function clearEmployeeRequestSuccess() {
return {
type: CLEAR_EMPLOYEE_REQUEST_SUCCESS
};
}
export function setEmployeeUUID(uuid) {
return {
type: SET_EMPLOYEE_UUID,
data: uuid
}
}
export function setFormEmployeeEmail(email) {
return {
type: SET_FORM_EMPLOYEE_EMAIL,
data: email
};
}
export function setFormEmployeeNote(note) {
return {
type: SET_FORM_EMPLOYEE_NOTE,
data: note
};
}
export function setClearEmployeeState() {
return {
type: SET_CLEAR_EMPLOYEE_STATE
};
}

@ -0,0 +1,26 @@
import {
CREATE_EMPLOYEE_REQUEST,
READ_EMPLOYEE_REQUEST,
DELETE_EMPLOYEE_REQUEST
} from "../../constants/employee.constants";
export function createEmployeeRequest(postBody) {
return {
type: CREATE_EMPLOYEE_REQUEST,
data: postBody
};
}
export function readEmployeeRequest(payload) {
return {
type: READ_EMPLOYEE_REQUEST,
data: payload
};
}
export function deleteEmployeeRequest(payload) {
return {
type: DELETE_EMPLOYEE_REQUEST,
data: payload
};
}

@ -0,0 +1,40 @@
import { post, get, patch, del } from "./baseApi";
/**
* Function wrapping POST request for adding a provider
* @param {string} provider_email - email of provider
* @param {string?} note - optional note
*/
export function addEmployee(provider_email, note) {
return post("/employee/", { provider_email, note }).then(resp =>
Promise.resolve(resp)
);
}
/**
* Function wrapping GET request for one employee detail
* @param {string} uuid - employee UUID
*/
export function getEmployee(uuid) {
return get(`/employee/${uuid}/`).then(resp => Promise.resolve(resp));
}
/**
* Function wrapping PATCH request for updating a provider
* @param {string} uuid - employee UUID
* @param {string} provider_email - provider's email
* @param {string?} note - optional note
*/
export function updateEmployee(uuid, provider_email, note) {
return patch(`/employee/${uuid}`, { provider_email, note }).then(resp =>
Promise.resolve(resp)
);
}
/**
* Function wrapping DELETE request for removing an employee
* @param {string} uuid - employee UUID
*/
export function deleteEmployee(uuid) {
return del(`/employee/${uuid}/`).then(resp => Promise.resolve(resp));
}

@ -10,6 +10,8 @@ import VerifyEmail from "./Auth/VerifyEmail";
import CreateWorkTypeForm from "./Worktype/CreateWorkTypeForm";
import UpdateWorkTypeForm from "./Worktype/UpdateWorkTypeForm";
import Worktypes from "./Worktype/Worktypes";
import ClientProviders from "./User/Client/ClientProviders";
import ClientAddProviderForm from "./User/Client/ClientAddProviderForm";
import CompleteRegistration from "./User/CompleteRegistration";
import EditProfile from "./User/EditProfile";
import Profile from "./User/Profile";
@ -61,6 +63,14 @@ class App extends Component {
path="/user/profile/client/create-worktype"
component={CreateWorkTypeForm}
/>
<PrivateRoute
path="/user/profile/client/providers"
component={ClientProviders}
/>
<PrivateRoute
path="/user/profile/client/add-provider"
component={ClientAddProviderForm}
/>
<PrivateRoute path="/user/profile/edit" component={EditProfile} />
<PrivateRoute path="/user/profile" component={Profile} />
<Route path="/user/complete-registration" component={Profile} />

@ -1,8 +1,4 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
it("does nothing", async done => {
expect(true).toBe(true);
done();
});

@ -46,7 +46,7 @@ const NavbarView = ({ isAuthenticated, dispatchLogoutRequest, selfUser }) => (
<Menu.Item as={Link} to="/about">
About
</Menu.Item>
{!isAuthenticated &&
{!isAuthenticated && (
<Menu.Menu position="right">
<Menu.Item as={Link} to="/auth/login">
Login
@ -54,18 +54,25 @@ const NavbarView = ({ isAuthenticated, dispatchLogoutRequest, selfUser }) => (
<Menu.Item as={Link} to="/auth/register">
Register
</Menu.Item>
</Menu.Menu>}
{!!isAuthenticated &&
</Menu.Menu>
)}
{!!isAuthenticated && (
<Menu.Menu position="right">
<Dropdown item text="Account">
<Dropdown.Menu>
<Dropdown.Item as={Link} to="/user/profile">
My Profile
</Dropdown.Item>
{selfUser.client &&
{selfUser.client && (
<Dropdown.Item as={Link} to="/user/profile/client/worktypes">
Work Types
</Dropdown.Item>}
</Dropdown.Item>
)}
{selfUser.client && (
<Dropdown.Item as={Link} to="/user/profile/client/providers">
Providers
</Dropdown.Item>
)}
<Dropdown.Item as={Link} to="/auth/settings">
Settings
</Dropdown.Item>
@ -75,7 +82,8 @@ const NavbarView = ({ isAuthenticated, dispatchLogoutRequest, selfUser }) => (
</Dropdown.Item>
</Dropdown.Menu>
</Dropdown>
</Menu.Menu>}
</Menu.Menu>
)}
</Menu>
);

@ -3,7 +3,11 @@ import React from "react";
import { Message } from "semantic-ui-react";
const propTypes = {
error: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
error: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
PropTypes.array
]),
header: PropTypes.string
};
@ -26,7 +30,10 @@ const Error = ({ error, header }) => {
error={hasError}
header={header}
list={Object.keys(error).map(p => (
<Message.Item key={p}> {p}: {error[p]}</Message.Item>
<Message.Item key={p}>
{" "}
{p}: {error[p]}
</Message.Item>
))}
/>
);

@ -0,0 +1,125 @@
import React, { Component } from "react";
import { connect } from "react-redux";
import { Redirect } from "react-router-dom";
import {
Container,
Form,
Header,
Input,
Message,
TextArea
} from "semantic-ui-react";
import {
clearEmployeeRequestError,
clearEmployeeRequestSuccess,
setFormEmployeeEmail,
setFormEmployeeNote
} from "../../../actions/employee/reducer.actions";
import { createEmployeeRequest } from "../../../actions/employee/saga.actions";
import Error from "../../Shared/Error";
class ClientAddProviderForm extends Component {
componentWillMount = () => {
this.props.dispatch(clearEmployeeRequestError());
this.props.dispatch(clearEmployeeRequestSuccess());
this.props.dispatch(setFormEmployeeEmail(""));
this.props.dispatch(setFormEmployeeNote(""));
};
changeProviderEmail = event => {
this.props.dispatch(setFormEmployeeEmail(event.target.value));
};
changeEmployeeNote = event => {
this.props.dispatch(setFormEmployeeNote(event.target.value));
};
onSubmitEmployee = event => {
event.preventDefault();
const { email, note } = this.props;
this.props.dispatch(createEmployeeRequest({ provider_email: email, note }));
};
render() {
const {
isSendingEmployeeRequest,
employeeRequestError,
employeeRequestSuccess,
email,
note,
selfUser
} = this.props;
if (!selfUser.client) {
return <Redirect to="/" />;
}
return (
<ClientAddProviderFormView
isSendingEmployeeRequest={isSendingEmployeeRequest}
employeeRequestError={employeeRequestError}
employeeRequestSuccess={employeeRequestSuccess}
email={email}
note={note}
changeProviderEmail={this.changeProviderEmail}
changeEmployeeNote={this.changeEmployeeNote}
onSubmitEmployee={this.onSubmitEmployee}
/>
);
}
}
function mapStateToProps(state) {
return { ...state.employee, selfUser: state.user.selfUser };
}
const ClientAddProviderFormView = ({
isSendingEmployeeRequest,
employeeRequestError,
employeeRequestSuccess,
email,
note,
changeProviderEmail,
changeEmployeeNote,
onSubmitEmployee
}) => (
<Container>
<Header>Add a Provider</Header>
<Form
loading={isSendingEmployeeRequest}
onSubmit={onSubmitEmployee}
error={!!employeeRequestError}
success={!!employeeRequestSuccess}
>
<Form.Field>
<label>Email Address</label>
<Input
placeholder="provider@caremyway.ca"
type="email"
value={email || ""}
onChange={changeProviderEmail}
/>
</Form.Field>
<Form.Field>
<label>Note</label>
<TextArea
placeholder="Employee notes"
value={note}
onChange={changeEmployeeNote}
/>
</Form.Field>
<Error header="Add Provider failed!" error={employeeRequestError} />
<Message success>
<Message.Header>Create Worktype successful!</Message.Header>
<p>Worktype successfully created.</p>
{!!employeeRequestSuccess && (
<Redirect to="/user/profile/client/providers" />
)}
</Message>
<Form.Button>Submit Worktype</Form.Button>
</Form>
</Container>
);
export default connect(mapStateToProps)(ClientAddProviderForm);

@ -0,0 +1,92 @@
import React, { Component } from "react";
import { connect } from "react-redux";
import { Redirect, Link } from "react-router-dom";
import {
Button,
Card,
Container,
Header,
Popup,
Segment
} from "semantic-ui-react";
import { deleteEmployeeRequest } from "../../../actions/employee/saga.actions";
class ClientProviders extends Component {
deleteEmployee = uuid => {
this.props.dispatch(deleteEmployeeRequest(uuid));
};
render() {
const { selfUser } = this.props;
if (selfUser.client) {
return (
<ClientProvidersView
user={selfUser}
deleteEmployee={this.deleteEmployee}
/>
);
} else {
return <Redirect to="/" />;
}
}
}
function mapStateToProps(state) {
return { selfUser: state.user.selfUser };
}
const ClientProvidersView = ({ user, deleteEmployee }) => (
<Container>
<Header>Providers</Header>
<Segment>
<Button
basic
color="green"
size="small"
as={Link}
to="/user/profile/client/add-provider"
>
Add a Provider
</Button>
</Segment>
{(user.client.employees || []).filter(employee => !employee.deleted)
.length > 0 && (
<Card.Group>
{user.client.employees
.filter(employee => !employee.deleted)
.map((employee, index) => (
<Card key={index}>
<Card.Content>
<Card.Header as="h4">{employee.provider}</Card.Header>
<Card.Description>{employee.note}</Card.Description>
<Popup
content={
<div>
Are you sure you want to delete this employee?<br />
<Button
basic
color="red"
size="small"
onClick={() => deleteEmployee(employee.uuid)}
>
Confirm Deletion
</Button>
</div>
}
trigger={
<Button basic color="red" size="small">
Delete
</Button>
}
on="click"
position="top right"
/>
</Card.Content>
</Card>
))}
</Card.Group>
)}
</Container>
);
export default connect(mapStateToProps)(ClientProviders);

@ -0,0 +1,15 @@
// Reducer Employee Action Constants
export const IS_SENDING_EMPLOYEE_REQUEST = "IS_SENDING_EMPLOYEE_REQUEST";
export const SET_EMPLOYEE_REQUEST_ERROR = "SET_EMPLOYEE_REQUEST_ERROR";
export const CLEAR_EMPLOYEE_REQUEST_ERROR = "CLEAR_EMPLOYEE_REQUEST_ERROR";
export const SET_EMPLOYEE_REQUEST_SUCCESS = "SET_EMPLOYEE_REQUEST_SUCCESS";
export const CLEAR_EMPLOYEE_REQUEST_SUCCESS = "CLEAR_EMPLOYEE_REQUEST_SUCCESS";
export const SET_EMPLOYEE_UUID = "SET_EMPLOYEE_UUID";
export const SET_FORM_EMPLOYEE_EMAIL = "SET_FORM_EMPLOYEE_EMAIL";
export const SET_FORM_EMPLOYEE_NOTE = "SET_FORM_EMPLOYEE_NOTE";
export const SET_CLEAR_EMPLOYEE_STATE = "SET_CLEAR_EMPLOYEE_STATE";
// Saga Worktype Action Constants
export const CREATE_EMPLOYEE_REQUEST = "CREATE_EMPLOYEE_REQUEST";
export const READ_EMPLOYEE_REQUEST = "READ_EMPLOYEE_REQUEST";
export const DELETE_EMPLOYEE_REQUEST = "DELETE_EMPLOYEE_REQUEST";

@ -0,0 +1,73 @@
import {
IS_SENDING_EMPLOYEE_REQUEST,
SET_EMPLOYEE_REQUEST_ERROR,
CLEAR_EMPLOYEE_REQUEST_ERROR,
SET_EMPLOYEE_REQUEST_SUCCESS,
CLEAR_EMPLOYEE_REQUEST_SUCCESS,
SET_EMPLOYEE_UUID,
SET_FORM_EMPLOYEE_EMAIL,
SET_FORM_EMPLOYEE_NOTE,
SET_CLEAR_EMPLOYEE_STATE
} from "../constants/employee.constants";
const initialState = {
isSendingEmployeeRequest: false,
employeeRequestError: "",
employeeRequestSuccess: "",
uuid: "",
email: "",
note: ""
};
function employeeReducer(state = initialState, action) {
switch (action.type) {
case IS_SENDING_EMPLOYEE_REQUEST:
return {
...state,
isSendingEmployeeRequest: action.data
};
case SET_EMPLOYEE_REQUEST_ERROR:
return {
...state,
employeeRequestError: action.data
};
case CLEAR_EMPLOYEE_REQUEST_ERROR:
return {
...state,
employeeRequestError: ""
};
case SET_EMPLOYEE_REQUEST_SUCCESS:
return {
...state,
employeeRequestSuccess: action.data
};
case CLEAR_EMPLOYEE_REQUEST_SUCCESS:
return {
...state,
employeeRequestSuccess: ""
};
case SET_EMPLOYEE_UUID:
return {
...state,
uuid: action.data
};
case SET_FORM_EMPLOYEE_EMAIL:
return {
...state,
email: action.data
};
case SET_FORM_EMPLOYEE_NOTE:
return {
...state,
note: action.data
};
case SET_CLEAR_EMPLOYEE_STATE:
return {
...initialState
};
default:
return state;
}
}
export default employeeReducer;

@ -2,11 +2,13 @@ import { combineReducers } from "redux";
import authReducer from "./authReducer";
import userReducer from "./userReducer";
import worktypeReducer from "./worktypeReducer";
import employeeReducer from "./employeeReducer";
const reducer = combineReducers({
auth: authReducer,
user: userReducer,
worktype: worktypeReducer
worktype: worktypeReducer,
employee: employeeReducer
});
export default reducer;

@ -0,0 +1,100 @@
import { effects } from "redux-saga";
import {
isSendingEmployeeRequest,
setEmployeeRequestError,
setEmployeeRequestSuccess,
clearEmployeeRequestError,
clearEmployeeRequestSuccess,
setEmployeeUUID,
setFormEmployeeEmail,
setFormEmployeeNote
} from "../actions/employee/reducer.actions";
import { getSelfUserRequest } from "../actions/user/saga.actions";
import { addEmployee, getEmployee, deleteEmployee, updateEmployee } from "../api/employee.api";
function* addEmployeeCall(postBody) {
yield effects.put(isSendingEmployeeRequest(true));
const { provider_email, note } = postBody;
try {
return yield effects.call(addEmployee, provider_email, note);
} catch (exception) {
yield effects.put(setEmployeeRequestError(exception));
return false;
} finally {
yield effects.put(isSendingEmployeeRequest(false));
}
}
function* readEmployeeCall(uuid) {
yield effects.put(isSendingEmployeeRequest(true));
try {
return yield effects.call(getEmployee, uuid);
} catch (exception) {
yield effects.put(setEmployeeRequestError(exception));
return false;
} finally {
yield effects.put(isSendingEmployeeRequest(false));
}
}
function* updateEmployeeCall(payload) {
yield effects.put(isSendingEmployeeRequest(true));
const { uuid, provider_email, note } = payload;
try {
return yield effects.call(updateEmployee, uuid, provider_email, note);
} catch (exception) {
yield effects.put(setEmployeeRequestError(exception));
return false;
} finally {
yield effects.put(isSendingEmployeeRequest(false))
}
}
function* deleteEmployeeCall(uuid) {
yield effects.put(isSendingEmployeeRequest(true));
try {
return yield effects.call(deleteEmployee, uuid);
} catch (exception) {
yield effects.put(setEmployeeRequestError(exception));
return false;
} finally {
yield effects.put(isSendingEmployeeRequest(false));
}
}
export function* addEmployeeFlow(request) {
yield effects.put(clearEmployeeRequestSuccess());
yield effects.put(clearEmployeeRequestError());
const wasSuccessful = yield effects.call(addEmployeeCall, request.data);
if (wasSuccessful) {
yield effects.put(getSelfUserRequest());
yield effects.put(setEmployeeRequestSuccess(wasSuccessful));
yield effects.put(setFormEmployeeEmail(""));
yield effects.put(setFormEmployeeNote(""));
yield effects.put(clearEmployeeRequestError());
}
}
export function* readEmployeeFlow(request) {
const wasSuccessful = yield effects.call(readEmployeeCall, request.data);
if (wasSuccessful) {
yield effects.put(setEmployeeUUID(wasSuccessful.uuid))
yield effects.put(setFormEmployeeEmail(wasSuccessful.provider_email));
yield effects.put(setFormEmployeeNote(wasSuccessful.note))
}
}
export function* updateEmployeeFlow(request) {
yield effects.put(clearEmployeeRequestSuccess());
yield effects.put(clearEmployeeRequestError());
const wasSuccessful = yield effects.call(updateEmployeeCall, request.data);
if (wasSuccessful) {
yield effects.put(getSelfUserRequest());
yield effects.put(setEmployeeRequestSuccess(wasSuccessful));
}
}
export function* deleteEmployeeFlow(request) {
yield effects.call(deleteEmployeeCall, request.data);
yield effects.put(getSelfUserRequest());
}

@ -47,6 +47,16 @@ import {
updateWorktypeFlow,
deleteWorktypeFlow
} from "./worktype.sagas";
import {
CREATE_EMPLOYEE_REQUEST,
READ_EMPLOYEE_REQUEST,
DELETE_EMPLOYEE_REQUEST
} from "../constants/employee.constants";
import {
addEmployeeFlow,
readEmployeeFlow,
deleteEmployeeFlow
} from "./employee.sagas";
export default function* rootSaga() {
yield takeLatest(SEND_REGISTER_REQUEST, registerUserFlow);
@ -67,4 +77,7 @@ export default function* rootSaga() {
yield takeLatest(READ_WORKTYPE_REQUEST, readWorktypeFlow);
yield takeLatest(UPDATE_WORKTYPE_REQUEST, updateWorktypeFlow);
yield takeLatest(DELETE_WORKTYPE_REQUEST, deleteWorktypeFlow);
yield takeLatest(CREATE_EMPLOYEE_REQUEST, addEmployeeFlow);
yield takeLatest(READ_EMPLOYEE_REQUEST, readEmployeeFlow);
yield takeLatest(DELETE_EMPLOYEE_REQUEST, deleteEmployeeFlow);
}

Loading…
Cancel
Save