Skip to content

Commit

Permalink
Merge pull request #54 from naari3/save-per
Browse files Browse the repository at this point in the history
Save page per size to cookie
  • Loading branch information
naari3 authored Nov 29, 2020
2 parents 346aff1 + 32cbd5b commit a8dccfa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 49 deletions.
85 changes: 38 additions & 47 deletions components/PerSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -1,63 +1,54 @@
import React, { useEffect } from "react";
import React, { FC } from "react";
import Link from "next/link";
import styles from "./PerSwitcher.module.css";
import {
useDispatch as useSearchDispatch,
useGlobalState as useSearchGlobalState,
} from "../contexts/SearchContext";
import {
useDispatch as useLoadingDispatch,
useGlobalState as useLoadingGlobalState,
} from "../contexts/LoadingContext";
import { useGlobalState as useSearchGlobalState } from "../contexts/SearchContext";
import { useGlobalState as useLoadingGlobalState } from "../contexts/LoadingContext";
import removeEmpty from "../lib/removeEmpty";
import { setCookie } from "nookies";

const PerSwitcher = React.memo(() => {
const PerButton: FC<{ per: number }> = ({ children, per }) => {
const loading = useLoadingGlobalState();
const options = useSearchGlobalState();
const optionsDispatch = useSearchDispatch();

return loading || options.per === per ? (
<span>{children}</span>
) : (
<Link
href={{
pathname: "/search",
query: removeEmpty({
...options,
page: 1,
per: per,
}),
}}
>
<a
onClick={() => {
console.log(per);
setCookie(null, "per", per.toString(), {
maxAge: 30 * 24 * 60 * 60,
path: "/",
});
}}
>
<span>{children}</span>
</a>
</Link>
);
};

const PerSwitcher = React.memo(() => {
const options = useSearchGlobalState();

return (
<div className={styles.perSwitcher}>
<ul className={styles.perList}>
<li className={`${options.per === 100 ? styles.selected : ""}`}>
{loading || options.per === 100 ? (
<span></span>
) : (
<Link
href={{
pathname: "/search",
query: removeEmpty({
...options,
page: 1,
per: 100,
}),
}}
>
<a>
<span></span>
</a>
</Link>
)}
<PerButton per={100}></PerButton>
</li>
<li className={`${options.per === 50 ? styles.selected : ""}`}>
{loading || options.per === 50 ? (
<span></span>
) : (
<Link
href={{
pathname: "/search",
query: removeEmpty({
...options,
page: 1,
per: 50,
}),
}}
>
<a>
<span></span>
</a>
</Link>
)}
<PerButton per={50}></PerButton>
</li>
</ul>
</div>
Expand Down
9 changes: 8 additions & 1 deletion pages/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ const roundDate = (datestr: string): string => {
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const cookies = parseCookies(ctx);
let viewing = cookies.viewing;
let per = parseQueryToInt(cookies.per);
if (viewing !== "detail" && viewing !== "icon") viewing = "detail";
if (!per) per = 50;
const { query } = ctx;
const client = new VideoClient({ context: "otomad-search" });
console.log(query);
Expand All @@ -273,8 +275,13 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
startTimeGte: roundDate(parseQueryToString(query.startTimeGte)),
startTimeLte: roundDate(parseQueryToString(query.startTimeLte)),
page: roundNumber(parseQueryToInt(query.page)),
per: Math.min(100, roundNumber(parseQueryToInt(query.per)) || 50),
per: Math.min(100, roundNumber(parseQueryToInt(query.per)) || per),
};
console.log({
cookie: per,
query: query.per,
final: Math.min(100, roundNumber(parseQueryToInt(query.per)) || per),
});

const response = await (async (): Promise<
Response<Pick<Video, typeof usedFields[number]>>
Expand Down
2 changes: 1 addition & 1 deletion reducers/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type SearchOptions = {
per?: number;
};

export const initialState: SearchOptions = { _sort: "-startTime", per: 100 };
export const initialState: SearchOptions = { _sort: "-startTime" };

export type State = SearchOptions;

Expand Down

1 comment on commit a8dccfa

@vercel
Copy link

@vercel vercel bot commented on a8dccfa Nov 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.