mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
refactor: re-structure logical structure
This commit is contained in:
parent
8c090978fd
commit
528057894b
22
react-typescript/src/hooks/useItem.ts
Normal file
22
react-typescript/src/hooks/useItem.ts
Normal file
@ -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;
|
||||
5
react-typescript/src/model/EEvents.ts
Normal file
5
react-typescript/src/model/EEvents.ts
Normal file
@ -0,0 +1,5 @@
|
||||
const enum EEvents {
|
||||
SOLD_ITEM = 'SOLD_ITEM',
|
||||
}
|
||||
|
||||
export default EEvents;
|
||||
3
react-typescript/src/model/index.ts
Normal file
3
react-typescript/src/model/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import useStore from './useStore';
|
||||
|
||||
export { useStore };
|
||||
42
react-typescript/src/model/initialState.ts
Normal file
42
react-typescript/src/model/initialState.ts
Normal file
@ -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;
|
||||
52
react-typescript/src/model/reducer/calculateQuality.ts
Normal file
52
react-typescript/src/model/reducer/calculateQuality.ts
Normal file
@ -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;
|
||||
17
react-typescript/src/model/reducer/calculateSellIn.ts
Normal file
17
react-typescript/src/model/reducer/calculateSellIn.ts
Normal file
@ -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;
|
||||
28
react-typescript/src/model/reducer/index.ts
Normal file
28
react-typescript/src/model/reducer/index.ts
Normal file
@ -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;
|
||||
12
react-typescript/src/model/useStore.ts
Normal file
12
react-typescript/src/model/useStore.ts
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
8
react-typescript/src/types/index.ts
Normal file
8
react-typescript/src/types/index.ts
Normal file
@ -0,0 +1,8 @@
|
||||
type TItem = {
|
||||
name: string,
|
||||
sellIn: number,
|
||||
quality: number,
|
||||
isConjured: boolean
|
||||
}
|
||||
|
||||
export default TItem;
|
||||
Loading…
Reference in New Issue
Block a user