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