1
0

Group and Location fetching hooked into Groups / Locations

Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
James Ketrenos 2022-04-09 16:59:20 -07:00
parent 0daf3c72b6
commit 6238cdd6db
5 changed files with 76 additions and 28 deletions

View File

@ -11,9 +11,10 @@ import { base } from "./Common.js";
import { Location } from "./Location.js"; import { Location } from "./Location.js";
function Group() { function Group() {
const { csrfToken, user, setUser } = useContext(GlobalContext); const { csrfToken, user } = useContext(GlobalContext);
const groupId = useParams().group; const groupId = useParams().group;
const [ group, setGroup ] = useState(null); const [ group, setGroup ] = useState(undefined);
const [ events, setEvents ] = useState(null);
const [ error, setError ] = useState(null); const [ error, setError ] = useState(null);
const [ locations, setLocations ] = useState([]); const [ locations, setLocations ] = useState([]);
@ -46,7 +47,6 @@ function Group() {
effect(); effect();
}, [user, setGroup, groupId, csrfToken]); }, [user, setGroup, groupId, csrfToken]);
useEffect(() => { useEffect(() => {
if (!user || !groupId || !csrfToken) { if (!user || !groupId || !csrfToken) {
return; return;
@ -71,7 +71,36 @@ function Group() {
if (!data) { if (!data) {
return; return;
} }
setGroup(data); setEvents(data);
};
effect();
}, [user, setEvents, groupId, csrfToken]);
useEffect(() => {
if (!user || !groupId || !csrfToken) {
return;
}
const effect = async () => {
const res = await window.fetch(
`${base}/api/v1/groups/${groupId}`, {
method: 'GET',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
'CSRF-Token': csrfToken
}
});
const data = await res.json();
if (res.status >= 400) {
setError(data.message ? data.message : res.statusText);
return;
}
if (!data) {
return;
}
setGroup(data[0]);
}; };
effect(); effect();
}, [user, setGroup, groupId, csrfToken]); }, [user, setGroup, groupId, csrfToken]);
@ -79,11 +108,12 @@ function Group() {
return ( return (
<Paper className="Group" style={{ <Paper className="Group" style={{
display: 'flex', display: 'flex',
flexDirection: 'column' flexDirection: 'column',
textAlign: 'left',
}}> }}>
{ error && <div>{error}</div>} { error && <div>{error}</div>}
{ !error && <> { !error && group && <>
<div>Group: {groupId}</div> <div style={{fontWeight: 'bold'}}>{group.name}</div>
<div>Locations</div> <div>Locations</div>
{ locations.map(location => { locations.map(location =>
<Location location={location} key={location.id}/>) } <Location location={location} key={location.id}/>) }

View File

@ -104,6 +104,7 @@ function Location(props) {
{ typeof location[field] === 'string' { typeof location[field] === 'string'
&& !location[field].match(/^http.*/) && !location[field].match(/^http.*/)
&& location[field] } && location[field] }
{ typeof location[field] === 'number' && location[field]}
</div> </div>
</div> </div>
} ); } );
@ -133,7 +134,10 @@ function Location(props) {
return ( return (
<Paper className="Location" style={{ <Paper className="Location" style={{
display: 'flex', display: 'flex',
flexDirection: 'column' flexDirection: 'column',
padding: '0.5rem',
marginTop: '0.25rem',
marginBottom: '0.25rem'
}}> }}>
{ error && <div>{error}</div>} { error && <div>{error}</div>}
{ !error && <> { !error && <>

View File

@ -39,6 +39,8 @@ app.use(session({
})); }));
const csrfDebug = (req) => { const csrfDebug = (req) => {
return;
// eslint-disable-next-line no-unreachable
console.log('CSRF debug: ', { console.log('CSRF debug: ', {
method: req.method, method: req.method,
path: req.path, path: req.path,

View File

@ -1,20 +1,35 @@
const express = require('express'), const express = require('express'),
router = express.Router(); router = express.Router();
router.get('/', async (req, res/*, next*/) => { const originalGroups = [ {
return res.status(200).send( id: 1,
[ { ownerId: 1,
id: 1, name: 'Beer Tuesday',
ownerId: 1, group: 'beer-tuesday',
name: 'Beer Tuesday', nextEvent: Date.now() + 86400 * 14 * 1000 /* 2 weeks from now */
group: 'beer-tuesday', } ];
nextEvent: Date.now() + 86400 * 14 * 1000 /* 2 weeks from now */
} ] router.get('/:group?', async (req, res/*, next*/) => {
); const { group } = req.params;
if (!group) {
return res.status(200).send(originalGroups);
}
const found = originalGroups.find((item) => {
if (typeof group === 'number') {
return item.id === group;
}
if (typeof group === 'string') {
return item.group === group;
}
return false;
});
if (!found) {
return res.status(404).send({ message: `Unable to find ${group}.`});
}
return res.status(200).send([ found ]);
}); });
router.get('/:group/events', async (req, res/*, next*/) => {
router.get('/:groupId/events', async (req, res/*, next*/) => {
return res.status(200).send( return res.status(200).send(
[{ [{
id: 1, id: 1,
@ -24,17 +39,13 @@ router.get('/:groupId/events', async (req, res/*, next*/) => {
); );
}); });
router.post('/:groupId', async (req, res) => { router.post('/:group', async (req, res) => {
const { groupId } = req.params; const { group } = req.params;
if (!groupId) { if (!group) {
return res.status(400).send({ message: `Invalid group.`}); return res.status(400).send({ message: `Invalid group.`});
} }
const group = { return res.status(200).send({ id: group });
id: groupId
};
return res.status(200).send({ id: group.id });
}); });

View File

@ -5,6 +5,7 @@ const express = require('express');
const router = express.Router(); const router = express.Router();
const originalLocations = require('../location-data.js'); const originalLocations = require('../location-data.js');
originalLocations.forEach((item, index) => item.id = index + 1);
router.put('/', (req, res) => { router.put('/', (req, res) => {
const location = req.body; const location = req.body;