Skip to content

Commit

Permalink
Merge pull request #1129 from gwynndp/session-filter
Browse files Browse the repository at this point in the history
Session filter
  • Loading branch information
gwynndp authored Jan 12, 2024
2 parents 4289c68 + 312f3da commit bb9e7fc
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 25 deletions.
2 changes: 0 additions & 2 deletions src/api/treeTrackerApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,6 @@ export default {
try {
const query = `${FIELD_DATA_API}/session`;

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

return fetch(query, {
method: 'GET',
headers: {
Expand Down
13 changes: 13 additions & 0 deletions src/components/CaptureFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import TextField from '@material-ui/core/TextField';
import MenuItem from '@material-ui/core/MenuItem';
import Autocomplete from '@material-ui/lab/Autocomplete';
import SelectOrg from './common/SelectOrg';
import SelectSession from './common/SelectSession';
import FilterModel, {
ALL_SPECIES,
ALL_SESSIONS,
SPECIES_ANY_SET,
SPECIES_NOT_SET,
ALL_ORGANIZATIONS,
Expand Down Expand Up @@ -94,6 +96,9 @@ function Filter(props) {
const [tag, setTag] = useState(null);
const [tagSearchString, setTagSearchString] = useState('');
const [organizationId, setOrganizationId] = useState(ALL_ORGANIZATIONS);
const [sessionId, setSessionId] = useState(
filter?.session_id || ALL_SESSIONS
);
const [tokenId, setTokenId] = useState(filter?.tokenId || filterOptionAll);

const handleStartDateChange = (date) => {
Expand Down Expand Up @@ -123,6 +128,7 @@ function Filter(props) {
species_id: speciesId,
tag_id: tag ? tag.id : undefined,
organization_id: organizationId,
session_id: sessionId,
tokenId: tokenId.trim(),
};
const filter = new FilterModel(test);
Expand All @@ -144,6 +150,7 @@ function Filter(props) {
setTag(null);
setTagSearchString('');
setOrganizationId(ALL_ORGANIZATIONS);
setSessionId(ALL_SESSIONS);
setTokenId(filterOptionAll);
const filter = new FilterModel();
props.onSubmit && props.onSubmit(filter);
Expand Down Expand Up @@ -338,6 +345,12 @@ function Filter(props) {
setOrganizationId(org.stakeholder_uuid);
}}
/>
<SelectSession
sessionId={sessionId}
handleSelection={(session) => {
setSessionId(session.id);
}}
/>
</Grid>
<Grid className={classes.inputContainer}>
<Button
Expand Down
10 changes: 6 additions & 4 deletions src/components/FilterTop.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import FilterModel, {
ALL_TAGS,
TAG_NOT_SET,
ANY_TAG_SET,
SESSION_NOT_SET,
ALL_SESSIONS,
} from '../models/Filter';
import DateFnsUtils from '@date-io/date-fns';
import {
Expand Down Expand Up @@ -97,7 +97,9 @@ function Filter(props) {
const [organizationId, setOrganizationId] = useState(
filter?.organizationId || ALL_ORGANIZATIONS
);
const [sessionId, setSessionId] = useState(filter?.session_id || SESSION_NOT_SET);
const [sessionId, setSessionId] = useState(
filter?.session_id || ALL_SESSIONS
);
// const [tokenId, setTokenId] = useState(filter?.tokenId || filterOptionAll);

const handleStartDateChange = (date) => {
Expand Down Expand Up @@ -145,7 +147,7 @@ function Filter(props) {
setTag(null);
setTagSearchString('');
setOrganizationId(ALL_ORGANIZATIONS);
setSessionId(SESSION_NOT_SET);
setSessionId(ALL_SESSIONS);
// setTokenId(filterOptionAll);

const filter = new FilterModel();
Expand Down Expand Up @@ -382,7 +384,7 @@ function Filter(props) {
}}
/>
<SelectSession
orgId={organizationId}
sessionId={sessionId}
handleSelection={(session) => {
setSessionId(session.id);
}}
Expand Down
50 changes: 33 additions & 17 deletions src/components/common/SelectSession.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
import React, { useContext } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import { TextField, MenuItem } from '@material-ui/core';
import { AppContext } from 'context/AppContext';
import { SESSION_NOT_SET } from 'models/Filter';
import { ALL_SESSIONS /* SESSION_NOT_SET */ } from 'models/Filter';
import { getDateTimeStringLocale } from 'common/locale';

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

const defaultOrgList = defaultSessions
const defaultList = defaultSessions?.length
? defaultSessions
: [
{
id: SESSION_NOT_SET,
name: 'Not Set',
value: 'Not Set',
id: ALL_SESSIONS,
name: 'All',
value: 'All',
},
];
const [sessions, setSessions] = useState(defaultList);

useEffect(() => {
// format the session data for the dropdown
const sesh = sessionList.map((s) => ({
id: s.id,
org: s.organization,
name: `${s.wallet} ${s.organization ? ` / ${s.organization} ` : ''}`,
date: getDateTimeStringLocale(s.created_at),
value: s.id,
}));

setSessions(() => [...defaultList, ...sesh]);
}, [sessionList]);

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

Expand All @@ -30,14 +41,19 @@ function SelectSession({ orgId, defaultSessions, handleSelection }) {
data-testid="session-dropdown"
htmlFor="session"
id="session"
label="Sessionn"
label="Session"
name="Session"
value={orgId}
value={sessionId}
onChange={handleChange}
>
{[...defaultOrgList, ...sessionList].map((session) => (
<MenuItem data-testid="org-item" key={session.id} value={session.id}>
{session.name}
{[...sessions].map((session) => (
<MenuItem
data-testid="session-item"
key={session.id}
value={session.id}
>
{session.date} -- {session.name}{' '}
{session.organization ? ` / ${session.organization}` : ''}
</MenuItem>
))}
</TextField>
Expand Down
2 changes: 1 addition & 1 deletion src/context/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export const AppProvider = (props) => {
}

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

Expand Down
6 changes: 5 additions & 1 deletion src/models/Filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ 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 ALL_SESSIONS = 'ALL_SESSIONS';
export const SESSION_NOT_SET = 'SESSION_NOT_SET';
export const ANY_SESSION_SET = 'ANY_SESSION_SET';
export const ALL_TAGS = 'ALL_TAGS';
export const TAG_NOT_SET = 'TAG_NOT_SET';
export const ANY_TAG_SET = 'ANY_TAG_SET';
Expand Down Expand Up @@ -97,7 +99,9 @@ export default class Filter {

if (this.session_id === SESSION_NOT_SET) {
where.session_id = null;
} else {
} else if (this.session_id === ANY_SESSION_SET) {
where.session_id = 'not null';
} else if (this.session_id !== ALL_SESSIONS) {
where.session_id = this.session_id;
}

Expand Down

0 comments on commit bb9e7fc

Please sign in to comment.