fix: duration query string (#465)

pull/459/head^2
Zeng1998 2 years ago committed by GitHub
parent 205ad0fd6d
commit da80d4ef62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,4 +1,5 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit"; import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { parse, ParsedQs } from "qs";
interface Duration { interface Duration {
from: number; from: number;
@ -31,7 +32,7 @@ const getValidPathname = (pathname: string): string => {
const getStateFromLocation = () => { const getStateFromLocation = () => {
const { pathname, search, hash } = window.location; const { pathname, search, hash } = window.location;
const urlParams = new URLSearchParams(search); const urlParams = parse(search.slice(1));
const state: State = { const state: State = {
pathname: getValidPathname(pathname), pathname: getValidPathname(pathname),
hash: hash, hash: hash,
@ -40,20 +41,22 @@ const getStateFromLocation = () => {
if (search !== "") { if (search !== "") {
state.query = {}; state.query = {};
state.query.tag = urlParams.get("tag") ?? undefined; state.query.tag = urlParams["tag"] as string;
state.query.type = (urlParams.get("type") as MemoSpecType) ?? undefined; state.query.type = urlParams["type"] as MemoSpecType;
state.query.text = urlParams.get("text") ?? undefined; state.query.text = urlParams["text"] as string;
const shortcutIdStr = urlParams.get("shortcutId"); const shortcutIdStr = urlParams["shortcutId"] as string;
state.query.shortcutId = shortcutIdStr ? Number(shortcutIdStr) : undefined; state.query.shortcutId = shortcutIdStr ? Number(shortcutIdStr) : undefined;
const from = parseInt(urlParams.get("from") ?? "0"); const durationObj = urlParams["duration"] as ParsedQs;
const to = parseInt(urlParams.get("to") ?? "0"); if (durationObj) {
if (to > from && to !== 0) { const duration: Duration = {
state.query.duration = { from: Number(durationObj["from"]),
from, to: Number(durationObj["to"]),
to,
}; };
if (duration.to > duration.from && duration.to !== 0) {
state.query.duration = duration;
} }
state.query.visibility = (urlParams.get("visibility") as Visibility) ?? undefined; }
state.query.visibility = urlParams["visibility"] as Visibility;
} }
return state; return state;

Loading…
Cancel
Save