diff --git a/src/stores/album/duck/actions.js b/src/stores/album/duck/actions.js new file mode 100644 index 0000000..11f8dbe --- /dev/null +++ b/src/stores/album/duck/actions.js @@ -0,0 +1,19 @@ +import types from './types' + +const getAll = item => ({ + type: types.GET_ALL, item +}) + +const getOne = item => ({ + type: types.GET_ONE, item +}) + +const getRatings = item => ({ + rype: types.GET_RATINGS, item +}) + +export default { + getAll, + getOne, + getRatings +} \ No newline at end of file diff --git a/src/stores/album/duck/index.js b/src/stores/album/duck/index.js new file mode 100644 index 0000000..e69de29 diff --git a/src/stores/album/duck/operations.js b/src/stores/album/duck/operations.js new file mode 100644 index 0000000..3e6f94d --- /dev/null +++ b/src/stores/album/duck/operations.js @@ -0,0 +1,71 @@ +import AppService from '../../AppService' + +import actions from './actions' + +const endpoint = 'album/' + +// Album CRUD + +export const getAllAlbum = () => async (dispatch) => { + return await AppService._getList( + endpoint + ).then( response => { + dispatch( actions.getAll( response ) ) + }) +} + +export const getOneAlbum = (id) => async( dispatch ) => { + return await AppService._getOne( + endpoint + id + ).then( response => { + dispatch( actions.getOne( response ) ) + }) +} + +export const createAlbum = async (album, token) => { + return await AppService._post( + endpoint, + album, + token + ) +} + +export const updateAlbum = async (album, token) => { + return await AppService._patch( + endpoint, + album, + token + ) +} + +export const deleteAlbum = async (id, token) => { + return await AppService._delete( + endpoint + id, + token + ) +} + +// Album Ratings CRUD + +export const getAllAlbumRating = (id) => async ( dispatch ) => { + return await AppService._getList( + endpoint + id + '/ratings/' + ).then( response => { + dispatch( actions.getRatings( response ) ) + }) +} + +export const createAlbumRating = (album_id, rating, token) => async ( dispatch ) => { + return await AppService._post( + endpoint + album_id + '/rating/', + rating, + token + ) +} + +export const deleteAlbumRating = (album_id, rating_id, token) => async ( dispatch ) => { + return await AppService._delete( + endpoint + album_id + '/rating/' + rating_id, + token + ) +} \ No newline at end of file diff --git a/src/stores/album/duck/reducers.js b/src/stores/album/duck/reducers.js new file mode 100644 index 0000000..c298bd0 --- /dev/null +++ b/src/stores/album/duck/reducers.js @@ -0,0 +1,51 @@ +import types from './types' + +const INITIAL_STATE = { + actualAlbum: { + id: -1, + user_id: -1, + title: '', + description: '', + image: '', + url_code: '', + tracks: [], + ratings: [] + }, + albums: [] +} + +const albumReducer = ( state = INITIAL_STATE, action ) => { + switch(action.type) { + case types.GET_ALL: + return { + ...state, + albums: action.item + } + case types.GET_ONE: + return { + ...state, + actualAlbum: { + ...state.actualAlbum, + id: action.item.id, + user_id: action.item.user_id, + title: action.item.title, + description: action.item.description, + image: action.item.image, + url_code: action.item.url_code, + tracks: action.item.tracks, + } + } + case types.GET_RATINGS: + return { + ...state, + actualAlbum: { + ...state.actualAlbum, + ratings: action.item + } + } + default: + return state + } +} + +export default albumReducer \ No newline at end of file diff --git a/src/stores/album/duck/types.js b/src/stores/album/duck/types.js new file mode 100644 index 0000000..6287b09 --- /dev/null +++ b/src/stores/album/duck/types.js @@ -0,0 +1,9 @@ +const GET_ALL = 'GET_ALL' +const GET_ONE = 'GET_ONE' +const GET_RATINGS = 'GET_RATINGS' + +export default ({ + GET_ALL, + GET_ONE, + GET_RATINGS +}) \ No newline at end of file diff --git a/src/stores/user/duck/operations.js b/src/stores/user/duck/operations.js index 55d48bf..f20c645 100644 --- a/src/stores/user/duck/operations.js +++ b/src/stores/user/duck/operations.js @@ -60,7 +60,7 @@ export const deleteAuth = (token) => async (dispatch) => { export const registerUser = async (user) => { response = await AppService._post( - this.endpoint, + endpoint, user, AppService.defaultToken )