misc
This commit is contained in:
@@ -3,36 +3,10 @@ import { toast } from "react-toastify";
|
||||
import { API_URL } from "@/config";
|
||||
|
||||
export default function LoginPage() {
|
||||
const [redirectTo, setRedirectTo] = useState("/");
|
||||
const [username, setUsername] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
// On mount, determine where to redirect after login:
|
||||
// 1. Use sessionStorage 'redirectTo' if present
|
||||
// 2. Else use document.referrer if same-origin
|
||||
// 3. Else fallback to "/"
|
||||
useEffect(() => {
|
||||
try {
|
||||
const savedRedirect = sessionStorage.getItem("redirectTo");
|
||||
if (savedRedirect) {
|
||||
setRedirectTo(savedRedirect);
|
||||
} else if (document.referrer) {
|
||||
const refUrl = new URL(document.referrer);
|
||||
// Only accept same origin referrers for security
|
||||
if (refUrl.origin === window.location.origin) {
|
||||
const pathAndQuery = refUrl.pathname + refUrl.search;
|
||||
setRedirectTo(pathAndQuery);
|
||||
sessionStorage.setItem("redirectTo", pathAndQuery);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
// Fail silently; fallback to "/"
|
||||
console.error("Error determining redirect target:", error);
|
||||
setRedirectTo("/");
|
||||
}
|
||||
}, []);
|
||||
|
||||
async function handleSubmit(e) {
|
||||
e.preventDefault();
|
||||
setLoading(true);
|
||||
@@ -84,12 +58,8 @@ export default function LoginPage() {
|
||||
|
||||
if (data.access_token) {
|
||||
toast.success("Login successful!");
|
||||
|
||||
// Clear stored redirect after use
|
||||
sessionStorage.removeItem("redirectTo");
|
||||
|
||||
// Redirect to stored path or fallback "/"
|
||||
window.location.href = redirectTo || "/";
|
||||
// Redirect
|
||||
window.location.href = "/TRip"; // TODO: fix, hardcoded
|
||||
} else {
|
||||
toast.error("Login failed: no access token received");
|
||||
setLoading(false);
|
||||
|
@@ -21,9 +21,11 @@ export default function MediaRequestForm() {
|
||||
const [isSearching, setIsSearching] = useState(false);
|
||||
const [loadingAlbumId, setLoadingAlbumId] = useState(null);
|
||||
const [expandedAlbums, setExpandedAlbums] = useState([]);
|
||||
const [isFetching, setIsFetching] = useState(false);
|
||||
|
||||
const debounceTimeout = useRef(null);
|
||||
const autoCompleteRef = useRef(null);
|
||||
const metadataFetchToastId = useRef(null);
|
||||
|
||||
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); // Helper for delays
|
||||
|
||||
@@ -111,7 +113,13 @@ export default function MediaRequestForm() {
|
||||
|
||||
// Search button click handler
|
||||
const handleSearch = async () => {
|
||||
toast.dismiss();
|
||||
setIsSearching(true);
|
||||
metadataFetchToastId.current = toast.info("Retrieving metadata...",
|
||||
{
|
||||
autoClose: false,
|
||||
}
|
||||
);
|
||||
if (type === "artist") {
|
||||
if (!selectedArtist) {
|
||||
toast.error("Please select a valid artist from suggestions.");
|
||||
@@ -221,6 +229,7 @@ export default function MediaRequestForm() {
|
||||
|
||||
const fetchTracksSequentially = async () => {
|
||||
const minDelay = 600; // ms between API requests
|
||||
setIsFetching(true);
|
||||
for (const album of albumsToFetch) {
|
||||
if (isCancelled) break;
|
||||
|
||||
@@ -242,7 +251,7 @@ export default function MediaRequestForm() {
|
||||
setTracksByAlbum((prev) => ({ ...prev, [album.id]: data }));
|
||||
setSelectedTracks((prev) => ({
|
||||
...prev,
|
||||
[album.id]: data.map((t) => String(t.id)),
|
||||
[album.id]: data.map((t) => t.id),
|
||||
}));
|
||||
} catch (err) {
|
||||
toast.error(`Failed to fetch tracks for album ${album.album}.`);
|
||||
@@ -251,6 +260,12 @@ export default function MediaRequestForm() {
|
||||
}
|
||||
}
|
||||
setLoadingAlbumId(null);
|
||||
setIsFetching(false);
|
||||
try {
|
||||
toast.done(metadataFetchToastId.current);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
};
|
||||
};
|
||||
|
||||
fetchTracksSequentially();
|
||||
@@ -311,6 +326,10 @@ export default function MediaRequestForm() {
|
||||
|
||||
// Submit request handler with progress indicator
|
||||
const handleSubmitRequest = async () => {
|
||||
if (isFetching) {
|
||||
// tracks are not done being fetched
|
||||
return toast.error("Still fetching track metadata, please wait a moment.");
|
||||
}
|
||||
setIsSubmitting(true);
|
||||
try {
|
||||
// Example: simulate submission delay
|
||||
|
Reference in New Issue
Block a user