Skip to content

Commit

Permalink
Merge pull request #1022 from maxt0214/verify/capture-filter-by-session
Browse files Browse the repository at this point in the history
Verify/capture filter by session
  • Loading branch information
gwynndp committed Jan 10, 2024
2 parents ff1862f + e439d15 commit 59da67a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/api/treeTrackerApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,23 @@ export default {
handleError(error);
}
},
getSessions() {
try {
const query = `${FIELD_DATA_API}/session`;

log.debug('GET SESSIONS -----', query);

return fetch(query, {
method: 'GET',
headers: {
'content-type': 'application/json',
Authorization: session.token,
},
}).then(handleResponse);
} catch (error) {
handleError(error);
}
},
getAdminUserById(id) {
try {
const query = `${API_ROOT}/auth/admin_users/${id}`;
Expand Down
11 changes: 11 additions & 0 deletions src/components/FilterTop.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import FilterModel, {
ALL_TAGS,
TAG_NOT_SET,
ANY_TAG_SET,
SESSION_NOT_SET,
} from '../models/Filter';
import DateFnsUtils from '@date-io/date-fns';
import {
Expand All @@ -31,6 +32,7 @@ import {

// import { SpeciesContext } from '../context/SpeciesContext';
import { TagsContext } from '../context/TagsContext';
import SelectSession from './common/SelectSession';
// import { CircularProgress } from '@material-ui/core';

export const FILTER_WIDTH = 330;
Expand Down Expand Up @@ -95,6 +97,7 @@ function Filter(props) {
const [organizationId, setOrganizationId] = useState(
filter?.organizationId || ALL_ORGANIZATIONS
);
const [sessionId, setSessionId] = useState(filter?.session_id || SESSION_NOT_SET);
// const [tokenId, setTokenId] = useState(filter?.tokenId || filterOptionAll);

const handleStartDateChange = (date) => {
Expand Down Expand Up @@ -124,6 +127,7 @@ function Filter(props) {
filter.species_id = speciesId;
filter.tag_id = tag ? tag.id : undefined;
filter.organization_id = organizationId;
filter.session_id = sessionId;
// filter.tokenId = tokenId;
props.onSubmit && props.onSubmit(filter);
}
Expand All @@ -141,6 +145,7 @@ function Filter(props) {
setTag(null);
setTagSearchString('');
setOrganizationId(ALL_ORGANIZATIONS);
setSessionId(SESSION_NOT_SET);
// setTokenId(filterOptionAll);

const filter = new FilterModel();
Expand Down Expand Up @@ -376,6 +381,12 @@ function Filter(props) {
setOrganizationId(org.stakeholder_uuid);
}}
/>
<SelectSession
orgId={organizationId}
handleSelection={(session) => {
setSessionId(session.id);
}}
/>
</Grid>
<Grid className={classes.inputContainer}>
<Button
Expand Down
47 changes: 47 additions & 0 deletions src/components/common/SelectSession.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { useContext } from 'react';
import { TextField, MenuItem } from '@material-ui/core';
import { AppContext } from 'context/AppContext';
import { SESSION_NOT_SET } from 'models/Filter';

function SelectSession({ orgId, defaultSessions, handleSelection }) {
const { sessionList } = useContext(AppContext);
console.log("Sessions--- ", sessionList);

const defaultOrgList = defaultSessions
? defaultSessions
: [
{
id: SESSION_NOT_SET,
name: 'Not Set',
value: 'Not Set',
},
];

const handleChange = (e) => {
const session = [...defaultOrgList, ...sessionList].find(
(o) => o.id === e.target.value
);
handleSelection(session);
};

return (
<TextField
select
data-testid="session-dropdown"
htmlFor="session"
id="session"
label="Sessionn"
name="Session"
value={orgId}
onChange={handleChange}
>
{[...defaultOrgList, ...sessionList].map((session) => (
<MenuItem data-testid="org-item" key={session.id} value={session.id}>
{session.name}
</MenuItem>
))}
</TextField>
);
}

export default SelectSession;
10 changes: 9 additions & 1 deletion src/context/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export const AppProvider = (props) => {
const [routes, setRoutes] = useState(getRoutes(localUser));
const [userHasOrg, setUserHasOrg] = useState(false);
const [orgList, setOrgList] = useState([]);
const [sessionList, setSessionList] = useState([]);
const [orgId, setOrgId] = useState(undefined);

// TODO: The below `selectedFilters` state would be better placed under a
Expand All @@ -227,10 +228,11 @@ export const AppProvider = (props) => {
// CustomTableFilter under components/common.
const [selectedFilters, setSelectedFilters] = useState('');

// check if the user has an org load organizations when the user changes
// check if the user has an org, load organizations when the user changes
useEffect(() => {
if (user && token) {
loadOrganizations();
loadSessions();
}
setUserHasOrg(!!user?.policy?.organization?.id);
}, [user, token]);
Expand Down Expand Up @@ -314,6 +316,11 @@ export const AppProvider = (props) => {
setOrgList(orgs);
}

async function loadSessions() {
const sessions = await api.getSessions();
setSessionList(sessions);
}

function getOrganizationUUID() {
const orgId = session.user?.policy?.organization?.id || null;
const foundOrg = orgList.find((org) => org.id === orgId);
Expand All @@ -339,6 +346,7 @@ export const AppProvider = (props) => {
routes,
orgId,
orgList,
sessionList,
userHasOrg,
selectedFilters,
updateSelectedFilter,
Expand Down
12 changes: 12 additions & 0 deletions src/models/Filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const SPECIES_ANY_SET = 'SPECIES_ANY_SET';
export const SPECIES_NOT_SET = 'SPECIES_NOT_SET';
export const ALL_ORGANIZATIONS = 'ALL_ORGANIZATIONS';
export const ORGANIZATION_NOT_SET = 'ORGANIZATION_NOT_SET';
export const SESSION_NOT_SET = 'SESSION_NOT_SET';
export const ALL_TAGS = 'ALL_TAGS';
export const TAG_NOT_SET = 'TAG_NOT_SET';
export const ANY_TAG_SET = 'ANY_TAG_SET';
Expand All @@ -26,6 +27,7 @@ export default class Filter {
species_id;
tag_id;
organization_id;
session_id;
tokenId;
status;

Expand Down Expand Up @@ -93,6 +95,12 @@ export default class Filter {
where.organization_id = this.organization_id;
}

if (this.session_id === SESSION_NOT_SET) {
where.session_id = null;
} else {
where.session_id = this.session_id;
}

if (this.status) {
where.status = this.status;
}
Expand Down Expand Up @@ -180,6 +188,10 @@ export default class Filter {
numFilters += 1;
}

if (this.session_id && this.session_id !== SESSION_NOT_SET) {
numFilters += 1;
}

if (this.tokenId && this.tokenId !== 'All') {
numFilters += 1;
}
Expand Down

0 comments on commit 59da67a

Please sign in to comment.