Skip to content

Commit

Permalink
Persistent cookies
Browse files Browse the repository at this point in the history
Cookies set now have a lifespan of 10 years
  • Loading branch information
marcusfrdk committed Sep 2, 2024
1 parent 19ed9db commit 6c5545c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
6 changes: 4 additions & 2 deletions app/components/SelectInitialProgram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useData } from "@/contexts/DataProvider";
import styled from "@emotion/styled";
import splitProgram from "@/utils/splitProgram";
import Logo from "@/public/bth-logo.svg";
import setCookie from "@/utils/setCookie";

export default function SelectInitialProgram(){
const {programs, updateSelectedProgram} = useData();
Expand All @@ -21,8 +22,9 @@ export default function SelectInitialProgram(){
) return;

updateSelectedProgram(code + semester);
document.cookie = `selectedCode=${code}; SameSite=Strict; Path=/`;
}
setCookie("selectedCode", code);
setCookie("selectedSemester", semester);
};

return (
<Container>
Expand Down
13 changes: 7 additions & 6 deletions app/contexts/DataProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use client";

import { TeachersType } from "@/types/Program";
import splitProgram from "@/utils/splitProgram";
import type { ReactNode } from "react";
import setCookie from "@/utils/setCookie";
import splitProgram from "@/utils/splitProgram";
import { TeachersType } from "@/types/Program";
import { createContext, useState, useContext, useCallback } from "react";

type ProgramType = {
Expand Down Expand Up @@ -72,13 +73,13 @@ export default function DataProvider({

setSelectedProgram(data);
setComparedPrograms(programs => programs.filter(f => f.code !== data.code && f.semester !== data.semester));
document.cookie = `selectedCode=${code}; SameSite=Strict; Path=/`;
document.cookie = `selectedSemester=${semester}; SameSite=Strict; Path=/`;
setCookie("selectedCode", code);
setCookie("selectedSemester", semester);
};

const addComparison = useCallback((program: string) => {
const programs = [...comparedPrograms.map(cp => cp.code + cp.semester), program];
document.cookie = `comparedPrograms=${programs.join(",")}; SameSite=Strict; Path=/`;
setCookie("comparedPrograms", programs.join(","));
setComparedPrograms(programs.map(program => {
const {code, semester} = splitProgram(program);
return {
Expand All @@ -91,7 +92,7 @@ export default function DataProvider({

function removeComparison(program: string){
const programs = comparedPrograms.filter(cp => cp.code + cp.semester !== program).map(cp => cp.code + cp.semester);
document.cookie = `comparedPrograms=${programs.join(",")}; SameSite=Strict; Path=/`;
setCookie("comparedPrograms", programs.join(","));
setComparedPrograms(programs.map(program => {
const {code, semester} = splitProgram(program);
return {
Expand Down
6 changes: 6 additions & 0 deletions app/utils/setCookie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function setCookie(key: string, value: string) {
const currentDate = new Date();
const expirationDate = new Date(currentDate.setFullYear(currentDate.getFullYear() + 10));
const expires = expirationDate.toUTCString();
document.cookie = `${key}=${value}; SameSite=Strict; Expires=${expires}; Path=/`;
};

0 comments on commit 6c5545c

Please sign in to comment.