From 528057894b7cca88394112bc04e3f66e1e90592a Mon Sep 17 00:00:00 2001 From: sirlolcat Date: Sat, 4 Jun 2022 17:58:25 +0430 Subject: [PATCH] refactor: re-structure logical structure --- react-typescript/src/hooks/useItem.ts | 22 ++++++++ react-typescript/src/model/EEvents.ts | 5 ++ react-typescript/src/model/index.ts | 3 ++ react-typescript/src/model/initialState.ts | 42 +++++++++++++++ .../src/model/reducer/calculateQuality.ts | 52 +++++++++++++++++++ .../src/model/reducer/calculateSellIn.ts | 17 ++++++ react-typescript/src/model/reducer/index.ts | 28 ++++++++++ react-typescript/src/model/useStore.ts | 12 +++++ react-typescript/src/types/index.ts | 8 +++ 9 files changed, 189 insertions(+) create mode 100644 react-typescript/src/hooks/useItem.ts create mode 100644 react-typescript/src/model/EEvents.ts create mode 100644 react-typescript/src/model/index.ts create mode 100644 react-typescript/src/model/initialState.ts create mode 100644 react-typescript/src/model/reducer/calculateQuality.ts create mode 100644 react-typescript/src/model/reducer/calculateSellIn.ts create mode 100644 react-typescript/src/model/reducer/index.ts create mode 100644 react-typescript/src/model/useStore.ts create mode 100644 react-typescript/src/types/index.ts diff --git a/react-typescript/src/hooks/useItem.ts b/react-typescript/src/hooks/useItem.ts new file mode 100644 index 00000000..3e4b549f --- /dev/null +++ b/react-typescript/src/hooks/useItem.ts @@ -0,0 +1,22 @@ +import { useStore } from "../model"; +import EEvents from "../model/EEvents"; +import Item from "../types" + +function useItem() { + const { state, dispatch } = useStore(); + + return { + items: state, + + updateItem: (item: Item):void => { + dispatch({ + type: EEvents.SOLD_ITEM, + payload: { + name: item.name + } + }); + } + }; +} + +export default useItem; \ No newline at end of file diff --git a/react-typescript/src/model/EEvents.ts b/react-typescript/src/model/EEvents.ts new file mode 100644 index 00000000..35c396a7 --- /dev/null +++ b/react-typescript/src/model/EEvents.ts @@ -0,0 +1,5 @@ +const enum EEvents { + SOLD_ITEM = 'SOLD_ITEM', +} + +export default EEvents; \ No newline at end of file diff --git a/react-typescript/src/model/index.ts b/react-typescript/src/model/index.ts new file mode 100644 index 00000000..170d3d44 --- /dev/null +++ b/react-typescript/src/model/index.ts @@ -0,0 +1,3 @@ +import useStore from './useStore'; + +export { useStore }; \ No newline at end of file diff --git a/react-typescript/src/model/initialState.ts b/react-typescript/src/model/initialState.ts new file mode 100644 index 00000000..ef455151 --- /dev/null +++ b/react-typescript/src/model/initialState.ts @@ -0,0 +1,42 @@ + const GildedRose = { + items: [ + { + name: '+5 Dexterity Vest', + sellIn: 10, + quality: 20, + isConjured: false + }, + { + name: 'Aged Brie', + sellIn: 2, + quality: 0, + isConjured: false + }, + { + name: 'Elixir of the Mongoose', + sellIn: 5, + quality: 7, + isConjured: false + }, + { + name: 'Sulfuras, Hand of Ragnaros', + sellIn: 0, + quality: 80, + isConjured: false + }, + { + name: 'Backstage passes to a TAFKAL80ETC concert', + sellIn: 15, + quality: 20, + isConjured: false + }, + { + name: 'Conjured Mana Cake', + sellIn: 3, + quality: 6, + isConjured: true + }, + ] +} + +export default GildedRose; \ No newline at end of file diff --git a/react-typescript/src/model/reducer/calculateQuality.ts b/react-typescript/src/model/reducer/calculateQuality.ts new file mode 100644 index 00000000..4ecdd754 --- /dev/null +++ b/react-typescript/src/model/reducer/calculateQuality.ts @@ -0,0 +1,52 @@ +import Item from "../../types" + +function calculateQuality(state: Item): number { + const sellInAmount = state.sellIn; + + let calculatedQuality = state.quality; + let degradeRate = 1; + let qualityIdentifier = 1; + + if(state.name.includes("Sulfuras")) { + return 80; + } + + if(state.name.includes("Aged Brie")) { + qualityIdentifier = -1; + } + + if(state.name.includes("Backstage passes")) { + qualityIdentifier = -1; + if(sellInAmount <= 10 && sellInAmount > 6) { + degradeRate = 2; + } + if(sellInAmount <= 6 && sellInAmount > 0) { + degradeRate = 3; + } + if(sellInAmount <= 0) { + return 0; + } + } + + if(state.isConjured) { + degradeRate = 2; + } + + if(state.sellIn <= 0) { + return calculatedQuality; + } + + calculatedQuality = calculatedQuality - (qualityIdentifier * degradeRate); + + if(calculatedQuality > 50) { + return 50; + } + + if(calculatedQuality < 0) { + return 0; + } + + return calculatedQuality; +} + +export default calculateQuality; \ No newline at end of file diff --git a/react-typescript/src/model/reducer/calculateSellIn.ts b/react-typescript/src/model/reducer/calculateSellIn.ts new file mode 100644 index 00000000..8ba2f44f --- /dev/null +++ b/react-typescript/src/model/reducer/calculateSellIn.ts @@ -0,0 +1,17 @@ +function updateSellIn (state: any): number { + let calculatedSellIn = state.sellIn; + + if(state.name.includes("Sulfuras")) { + return calculatedSellIn; + } + + calculatedSellIn = calculatedSellIn - 1; + + if(calculatedSellIn < 0) { + return 0; + } + + return calculatedSellIn; +} + +export default updateSellIn; \ No newline at end of file diff --git a/react-typescript/src/model/reducer/index.ts b/react-typescript/src/model/reducer/index.ts new file mode 100644 index 00000000..d9dfd04b --- /dev/null +++ b/react-typescript/src/model/reducer/index.ts @@ -0,0 +1,28 @@ +import Item from '../../types'; +import EEvents from '../EEvents'; +import calculateQuality from '././calculateQuality'; +import calculateSellIn from '././calculateSellIn'; + +function updateQuality(item: Item) { + const calculatedQuality = calculateQuality(item); + return { ...item, quality: calculatedQuality }; +} + +function updateSellIn(item: Item) { + const calculatedSellIn = calculateSellIn(item); + return { ...item, sellIn: calculatedSellIn }; +} + +function reducer(state: Item[], action: any): any { + if(action.type === EEvents.SOLD_ITEM) { + let targetItem = state.find((item) => (item.name.includes(action.payload.name))); + if (targetItem !== undefined) { + const calculatedItems = [...state]; + + + } + } + return state; +} + +export default reducer; \ No newline at end of file diff --git a/react-typescript/src/model/useStore.ts b/react-typescript/src/model/useStore.ts new file mode 100644 index 00000000..03f36535 --- /dev/null +++ b/react-typescript/src/model/useStore.ts @@ -0,0 +1,12 @@ +import { useReducer } from "react"; +import initialState from "./initialState"; +import reducer from "./reducer"; + +export default function useStore() { + const [state, dispatch] = useReducer(reducer, initialState.items); + + return { + state, + dispatch + } +} \ No newline at end of file diff --git a/react-typescript/src/types/index.ts b/react-typescript/src/types/index.ts new file mode 100644 index 00000000..e9b9ce43 --- /dev/null +++ b/react-typescript/src/types/index.ts @@ -0,0 +1,8 @@ +type TItem = { + name: string, + sellIn: number, + quality: number, + isConjured: boolean +} + +export default TItem; \ No newline at end of file