<commit_before>// @flow
import React, { createContext, useContext, useReducer } from "react"
import { cartTypes } from "constants/cart"

const storageKey = "dscCart"

const CartContext: Object = createContext()

const initialState = {
  addedItems: JSON.parse(localStorage.getItem(storageKey) || "[]"),
  showCartDialog: false,
  maxItemsInCart: false,
}

type CartItem = {
  id: string,
  name: string,
  summary: string,
  type: string,
}

const cartReducer = (
  state: Object,
  action: {
    type: string,
    payload: {
      ...CartItem,
      fee: string,
      removeIndex: number, // used to remove item
    },
  },
) => {
  switch (action.type) {
    case cartTypes.ADD_TO_CART:
      const newItems = state.addedItems.concat({
        id: action.payload.id,
        name: action.payload.name,
        summary: action.payload.summary,
        fee: action.payload.fee,
      })
      if (newItems.length > 12) {
        return {
          ...state,
          maxItemsInCart: true,
        }
      }
      localStorage.setItem(storageKey, JSON.stringify(newItems))
      return {
        addedItems: newItems,
        showCartDialog: true,
        maxItemsInCart: false,
      }
    case cartTypes.REMOVE_FROM_CART:
      const updatedItems = [
        ...state.addedItems.slice(0, action.payload.removeIndex),
        ...state.addedItems.slice(action.payload.removeIndex + 1),
      ]
      localStorage.setItem(storageKey, JSON.stringify(updatedItems))
      return {
        addedItems: updatedItems,
      }
    case cartTypes.HIDE_CART_DIALOG:
      return {
        ...state,
        showCartDialog: false,<commit_msg>refactor: put maxItemsInCart in localstorage<commit_after>// @flow
import React, { createContext, useContext, useReducer } from "react"
import { cartTypes } from "constants/cart"

const storageKey = "dscCart"
const maxKey = "dscMaxItems"

const CartContext: Object = createContext()

const initialState = {
  addedItems: JSON.parse(localStorage.getItem(storageKey) || "[]"),
  showCartDialog: false,
  maxItemsInCart: JSON.parse(localStorage.getItem(maxKey) || "false"),
}

type CartItem = {
  id: string,
  name: string,
  summary: string,
  type: string,
}

const cartReducer = (
  state: Object,
  action: {
    type: string,
    payload: {
      ...CartItem,
      fee: string,
      removeIndex: number, // used to remove item
    },
  },
) => {
  switch (action.type) {
    case cartTypes.ADD_TO_CART:
      const newItems = state.addedItems
        .concat({
          id: action.payload.id,
          name: action.payload.name,
          summary: action.payload.summary,
          fee: action.payload.fee,
        })
        .slice(0, 12)
      if (newItems.length === 12) {
        localStorage.setItem(storageKey, JSON.stringify(newItems))
        localStorage.setItem(maxKey, JSON.stringify(true))
        return {
          addedItems: newItems,
          showCartDialog: true,
          maxItemsInCart: true,
        }
      }
      localStorage.setItem(storageKey, JSON.stringify(newItems))
      localStorage.setItem(maxKey, JSON.stringify(false))
      return {
        addedItems: newItems,
        showCartDialog: true,
        maxItemsInCart: false,
      }

    case cartTypes.REMOVE_FROM_CART:
      const updatedItems = [
        ...state.addedItems.slice(0, action.payload.removeIndex),
        ...state.addedItems.slice(action.payload.removeIndex + 1),
      ]
      localStorage.setItem(storageKey, JSON.stringify(updatedItems))
      localStorage.setItem(maxKey, JSON.stringify(false))
      return {
        addedItems: updatedItems,
        maxItemsInCart: false,
      }
    case cartTypes.HIDE_CART_DIALOG:
      return {
        ...state,
        showCartDialog: false,
{'fraction_non_alphanumeric': 0.11518885614787035, 'fraction_numerical': 0.0029466916688990086, 'mean_word_length': 2.5426944971537, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2965803', 'n_tokens_mistral': 1149, 'n_tokens_neox': 1140, 'n_words': 233}