Skip to content

Commit

Permalink
create theme service and context
Browse files Browse the repository at this point in the history
  • Loading branch information
Prince Vasconcelos committed Mar 22, 2021
1 parent 689c886 commit b9f7747
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 0 deletions.
134 changes: 134 additions & 0 deletions src/contexts/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import React, {useReducer} from 'react'

const INITIAL_STATE = {
cart: {},
pokemons: {},
// search: {},
endpoints: {},
theme: 'meupiru'
}

const DataContext = React.createContext()

function dataReducer(state, { type, payload }) {
switch (type) {
case 'UPDATE_CART': {
const {
quantity,
pokemon: {
name,
weight
}
} = payload

return {
...state,
cart: {
...state.cart,
[name]: {
quantity: state.cart[name]
? state.cart[name].quantity + quantity
: quantity,
price: weight,
}
}
}
}
case 'REMOVE_FROM_CART': {
const newState = {...state}
delete newState.cart[payload.name]
return newState
}
case 'SAVE_ENDPOINT_RESULT': {
const {url, result} = payload
return {
...state,
endpoints: {
...state.endpoints,
[url]: result
}
}
}
case 'SAVE_POKEMON_DATA': {
return {
...state,
pokemons: {
...state.pokemons,
...payload
}
}
}
case 'BUY_POKEMON': {
const { name, weight } = state.pokemons[payload]

return {
...state,
cart: {
...state.cart,
[name]: {
quantity: state.cart[name]
? state.cart[name].quantity + 1
: 1,
price: weight,
}
}
}
}
case 'CHANGE_THEME': {
return {
...state,
theme: payload
}
}
default: {
throw new Error(`Unhandled action type: ${type}`)
}
}
}

export function DataProvider ({ children, value }) {
const [state, dispatch] = useReducer(dataReducer, INITIAL_STATE)

const updateCart = payload => dispatch({
type: 'UPDATE_CART',
payload
})

const savePokemonData = payload => dispatch({
type: 'SAVE_POKEMON_DATA',
payload
})

const removePokemon = payload => dispatch({
type: 'REMOVE_FROM_CART',
payload
})

const saveEndpointResult = payload => dispatch({
type: 'SAVE_ENDPOINT_RESULT',
payload
})

const changeTheme = payload => dispatch({
type: 'CHANGE_THEME',
payload
})

const buyPokemon = payload => dispatch({
type: 'BUY_POKEMON',
payload
})

return <DataContext.Provider value={{
...state,
updateCart,
savePokemonData,
removePokemon,
saveEndpointResult,
changeTheme,
buyPokemon,
...value
}}>{children}</DataContext.Provider>
}

export default DataContext
20 changes: 20 additions & 0 deletions src/service/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import axios from 'axios'

// axios.defaults.headers.common = {
// 'Authorization': `Bearer ${localStorage.getItem('access_token')}`
// }

// export default {
// get: url => axios.get(url).then(d => d.data).catch(e => {
// if (e.response.status === 401) {
// window.location.href = process.env.REACT_APP_LOGIN_URL
// }
// }),
// }

export default {
get: url => axios
.get(url)
.then(d => d.data)
.catch(e => {})
}
59 changes: 59 additions & 0 deletions src/themes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const types = {
normal: {
colors: {
primary: "white",
},
},
fire: {
colors: {
primary: "#de5239",
},
},
water: {
colors: {
primary: "#8bc5cd",
},
},
grass: {
colors: {
primary: "#62d5b4",
},
},
electric: {
colors: {
primary: "#f6bd20",
},
},
bug: {
colors: {
primary: "#A8B820",
},
},
dragon: {
colors: {
primary: "#705848",
},
},
ghost: {
colors: {
primary: "#705898",
},
},
};

const global = {
colors: {
fire: '#de5239',
water: '#8bc5cd',
grass: '#62d5b4',
electric: '#f6bd20',
bug: '#A8B820',
dragon: '#705848',
ghost: '#705898'
}
}

export default type => ({
...global,
...types[type]
})

0 comments on commit b9f7747

Please sign in to comment.