Add authentication #17
1 changed files with 42 additions and 37 deletions
|
@ -1,4 +1,4 @@
|
||||||
import React, { useState } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import { motion } from 'framer-motion';
|
import { motion } from 'framer-motion';
|
||||||
import { Get } from '../../../scripts/pocketbase/Get';
|
import { Get } from '../../../scripts/pocketbase/Get';
|
||||||
import { Authentication } from '../../../scripts/pocketbase/Authentication';
|
import { Authentication } from '../../../scripts/pocketbase/Authentication';
|
||||||
|
@ -41,6 +41,47 @@ const UserEventRequests: React.FC<UserEventRequestsProps> = ({ eventRequests: in
|
||||||
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
|
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
|
||||||
const [isRefreshing, setIsRefreshing] = useState<boolean>(false);
|
const [isRefreshing, setIsRefreshing] = useState<boolean>(false);
|
||||||
|
|
||||||
|
// Refresh event requests
|
||||||
|
const refreshEventRequests = async () => {
|
||||||
|
setIsRefreshing(true);
|
||||||
|
const refreshToast = toast.loading('Refreshing submissions...');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const get = Get.getInstance();
|
||||||
|
const auth = Authentication.getInstance();
|
||||||
|
|
||||||
|
if (!auth.isAuthenticated()) {
|
||||||
|
toast.error('You must be logged in to refresh submissions', { id: refreshToast });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const userId = auth.getUserId();
|
||||||
|
if (!userId) {
|
||||||
|
toast.error('User ID not found', { id: refreshToast });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedRequests = await get.getAll<EventRequest>(
|
||||||
|
'event_request',
|
||||||
|
`requested_user="${userId}"`,
|
||||||
|
'-created'
|
||||||
|
);
|
||||||
|
|
||||||
|
setEventRequests(updatedRequests);
|
||||||
|
toast.success('Submissions refreshed successfully', { id: refreshToast });
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to refresh event requests:', err);
|
||||||
|
toast.error('Failed to refresh submissions. Please try again.', { id: refreshToast });
|
||||||
|
} finally {
|
||||||
|
setIsRefreshing(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Auto refresh on component mount
|
||||||
|
useEffect(() => {
|
||||||
|
refreshEventRequests();
|
||||||
|
}, []);
|
||||||
|
|
||||||
// Format date for display
|
// Format date for display
|
||||||
const formatDate = (dateString: string) => {
|
const formatDate = (dateString: string) => {
|
||||||
if (!dateString) return 'Not specified';
|
if (!dateString) return 'Not specified';
|
||||||
|
@ -88,42 +129,6 @@ const UserEventRequests: React.FC<UserEventRequestsProps> = ({ eventRequests: in
|
||||||
setSelectedRequest(null);
|
setSelectedRequest(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Refresh event requests
|
|
||||||
const refreshEventRequests = async () => {
|
|
||||||
setIsRefreshing(true);
|
|
||||||
const refreshToast = toast.loading('Refreshing submissions...');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const get = Get.getInstance();
|
|
||||||
const auth = Authentication.getInstance();
|
|
||||||
|
|
||||||
if (!auth.isAuthenticated()) {
|
|
||||||
toast.error('You must be logged in to refresh submissions', { id: refreshToast });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const userId = auth.getUserId();
|
|
||||||
if (!userId) {
|
|
||||||
toast.error('User ID not found', { id: refreshToast });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const updatedRequests = await get.getAll<EventRequest>(
|
|
||||||
'event_request',
|
|
||||||
`requested_user="${userId}"`,
|
|
||||||
'-created'
|
|
||||||
);
|
|
||||||
|
|
||||||
setEventRequests(updatedRequests);
|
|
||||||
toast.success('Submissions refreshed successfully', { id: refreshToast });
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Failed to refresh event requests:', err);
|
|
||||||
toast.error('Failed to refresh submissions. Please try again.', { id: refreshToast });
|
|
||||||
} finally {
|
|
||||||
setIsRefreshing(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (eventRequests.length === 0) {
|
if (eventRequests.length === 0) {
|
||||||
return (
|
return (
|
||||||
<div className="bg-base-200 rounded-lg p-8 text-center">
|
<div className="bg-base-200 rounded-lg p-8 text-center">
|
||||||
|
|
Loading…
Reference in a new issue