Skip to content

Commit

Permalink
feat: filter, edit, link stakeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynndp committed Jun 20, 2022
1 parent 6602e0d commit 688a9f2
Show file tree
Hide file tree
Showing 13 changed files with 598 additions and 260 deletions.
149 changes: 97 additions & 52 deletions src/api/stakeholders.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,20 @@
import {
handleResponse,
handleError,
getOrganization,
// getOrganization,
getOrganizationId,
} from './apiUtils';
import { session } from '../models/auth';

const STAKEHOLDER_API = process.env.REACT_APP_STAKEHOLDER_API_ROOT;

export default {
getStakeholderById(id) {
getStakeholders(id, { offset, rowsPerPage, orderBy, order, filter }) {
const orgId = getOrganizationId();
let stakeholderQuery = '';
if (Number(orgId)) {
stakeholderQuery = `${STAKEHOLDER_API}/${orgId}`;
} else if (id) {
stakeholderQuery = `${STAKEHOLDER_API}?id=${id}`;
} else {
stakeholderQuery = `${STAKEHOLDER_API}`;
}

console.log('getStakeholderById ---->', stakeholderQuery);

return fetch(stakeholderQuery, {
method: 'GET',
headers: {
'content-type': 'application/json',
Authorization: session.token,
},
})
.then(handleResponse)
.catch(handleError);
},

getStakeholders({
offset,
rowsPerPage,
orderBy = 'id',
order = 'desc',
filter,
}) {
const where = filter.getWhereObj ? filter.getWhereObj() : {};
const stakeholderFilter = {
where: { ...where, active: true },
const where = filter.getWhereObj();
console.log('where', where);
const lbFilter = {
where,
order: [`${orderBy} ${order}`],
limit: rowsPerPage,
offset,
Expand All @@ -52,28 +24,27 @@ export default {
orgName: true,
firstName: true,
lastName: true,
imageUrl: true,
email: true,
phone: true,
website: true,
logoUrl: true,
mapName: true,
imageUrl: true,
map: true,
stakeholder_uuid: true,
organizationId: true,
},
};
const query = `${STAKEHOLDER_API}/stakeholder?filter=${JSON.stringify(
stakeholderFilter,
)}`;

console.log(
'getStakeholders ---> ',
query,
'org ---> ',
getOrganizationId(),
);
let query = '';
if (!id && orgId && Number(orgId)) {
query = `${STAKEHOLDER_API}/${orgId}?filter=${JSON.stringify(lbFilter)}`;
// } else if (id) {
// query = `${STAKEHOLDER_API}/${id}?filter=${JSON.stringify(lbFilter)}`;
} else {
query = `${STAKEHOLDER_API}?filter=${JSON.stringify(lbFilter)}`;
}

return fetch(query, {
method: 'GET',
headers: {
'content-type': 'application/json',
Authorization: session.token,
Expand All @@ -83,14 +54,67 @@ export default {
.catch(handleError);
},

updateStakeholder(stakeholderUpdate) {
if (stakeholderUpdate.organizationId === 'null') {
stakeholderUpdate = { ...stakeholderUpdate, organizationId: null };
getUnlinkedStakeholders(id, abortController) {
const orgId = getOrganizationId();

console.log('id orgId', id, orgId);

let query = '';
if (!id && orgId && Number(orgId)) {
query = `${STAKEHOLDER_API}/links/${orgId}`;
} else if (id) {
query = `${STAKEHOLDER_API}/links/${id}`;
} else {
query = `${STAKEHOLDER_API}/links`;
}

console.log('getUnlinkedStakeholders ---->', query);

return fetch(query, {
method: 'GET',
headers: {
'content-type': 'application/json',
Authorization: session.token,
},
signal: abortController?.signal,
})
.then(handleResponse)
.catch(handleError);
},

updateLinks(id, stakeholdersData) {
const orgId = getOrganizationId();

console.log('id orgId', id, orgId);

let query = '';
if (orgId && Number(orgId)) {
query = `${STAKEHOLDER_API}/links/${orgId}`;
} else if (id) {
query = `${STAKEHOLDER_API}/links/${id}`;
} else {
query = `${STAKEHOLDER_API}/links`;
}
const { id } = stakeholderUpdate;
const stakeholderQuery = `${STAKEHOLDER_API}/${getOrganization()}stakeholder/${id}`;

return fetch(stakeholderQuery, {
console.log('updateLinks ---->', query);

return fetch(query, {
method: 'PATCH',
headers: {
'content-type': 'application/json',
Authorization: session.token,
},
body: JSON.stringify(stakeholdersData),
})
.then(handleResponse)
.catch(handleError);
},

updateStakeholder(stakeholderUpdate) {
console.log('org ---> ', getOrganizationId());
const query = `${STAKEHOLDER_API}/${getOrganizationId()}`;

return fetch(query, {
method: 'PATCH',
headers: {
'content-type': 'application/json',
Expand All @@ -101,4 +125,25 @@ export default {
.then(handleResponse)
.catch(handleError);
},

createStakeholder(stakeholderData) {
console.log('org ---> ', getOrganizationId());
let query = `${STAKEHOLDER_API}`;
const orgId = getOrganizationId();

if (orgId) {
query = `${STAKEHOLDER_API}/${orgId}`;
}

return fetch(query, {
method: 'POST',
headers: {
'content-type': 'application/json',
Authorization: session.token,
},
body: JSON.stringify(stakeholderData),
})
.then(handleResponse)
.catch(handleError);
},
};
5 changes: 1 addition & 4 deletions src/components/FilterTop.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import React, { useState, useContext } from 'react';
import { withStyles } from '@material-ui/core/styles';
import Grid from '@material-ui/core/Grid';
import Button from '@material-ui/core/Button';
import TextField from '@material-ui/core/TextField';
import MenuItem from '@material-ui/core/MenuItem';
import { Grid, Button, TextField, MenuItem } from '@material-ui/core';
import Autocomplete from '@material-ui/lab/Autocomplete';
import SelectOrg from './common/SelectOrg';
import FilterModel, {
Expand Down
94 changes: 77 additions & 17 deletions src/components/Stakeholders/Add.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useContext } from 'react';
import {
Button,
Dialog,
Expand All @@ -7,11 +7,29 @@ import {
DialogActions,
TextField,
FormControl,
MenuItem,
} from '@material-ui/core';
import { StakeholdersContext } from 'context/StakeholdersContext';

function AddStakeholder({ dispatch }) {
function AddStakeholder() {
const { createStakeholder } = useContext(StakeholdersContext);
const [open, setOpen] = useState(false);
const [data, setData] = useState();
const [data, setData] = useState({});

const defaultTypeList = [
{
name: 'Not set',
value: undefined,
},
{
name: 'Organization',
value: 'Organization',
},
{
name: 'Person',
value: 'Person',
},
];

const openModal = () => {
setOpen(true);
Expand All @@ -22,12 +40,12 @@ function AddStakeholder({ dispatch }) {
setOpen(false);
};

const handleDataChange = (e) => {
const handleChange = (e) => {
setData({ ...data, [e.target.name]: e.target.value });
};

const submit = () => {
dispatch.createStakeholder(data);
createStakeholder(data);
};

return (
Expand All @@ -40,45 +58,87 @@ function AddStakeholder({ dispatch }) {
<DialogContent>
<FormControl>
<TextField
label="ID"
data-testid="type-dropdown"
select
label="type"
htmlFor="type"
id="type"
name="type"
value={data?.type || ''}
onChange={(e) => handleChange(e)}
>
{defaultTypeList.map((type) => (
<MenuItem
data-testid="type-item"
key={type.name}
value={type.value}
>
{type.name}
</MenuItem>
))}
</TextField>
<TextField
label="Organization Name"
variant="outlined"
name="id"
onChange={handleDataChange}
value={data?.id || ''}
name="org_name"
onChange={handleChange}
value={data?.org_name || ''}
/>
<TextField
label="Organization Name"
label="First Name"
variant="outlined"
name="first_name"
onChange={handleChange}
value={data?.first_name || ''}
/>
<TextField
label="Last Name"
variant="outlined"
name="name"
onChange={handleDataChange}
value={data?.name || ''}
name="last_name"
onChange={handleChange}
value={data?.last_name || ''}
/>
<TextField
label="Map"
variant="outlined"
name="map"
onChange={handleDataChange}
placeholder="/map_address"
onChange={handleChange}
value={data?.map || ''}
/>
<TextField
label="Email"
variant="outlined"
name="email"
onChange={handleDataChange}
onChange={handleChange}
value={data?.email || ''}
/>
<TextField
label="Phone"
variant="outlined"
name="phone"
onChange={handleDataChange}
onChange={handleChange}
value={data?.phone || ''}
/>
<TextField
label="Image Url"
variant="outlined"
name="imageUrl"
onChange={handleChange}
value={data?.imageUrl || ''}
/>
<TextField
label="Logo Url"
variant="outlined"
name="logoUrl"
onChange={handleChange}
value={data?.logoUrl || ''}
/>
<TextField
label="Website"
variant="outlined"
name="website"
onChange={handleDataChange}
onChange={handleChange}
value={data?.website || ''}
/>
</FormControl>
Expand Down
Loading

0 comments on commit 688a9f2

Please sign in to comment.