From 553feb7c62957f6fc1df3906adae60128c559134 Mon Sep 17 00:00:00 2001 From: TBS093A Date: Fri, 7 Aug 2020 20:52:51 +0200 Subject: [PATCH] add track Redux store && TODO -> complete track store --- src/stores/track/duck/actions.js | 19 ++++++++ src/stores/track/duck/index.js | 5 ++ src/stores/track/duck/operations.js | 76 +++++++++++++++++++++++++++++ src/stores/track/duck/reducers.js | 54 ++++++++++++++++++++ src/stores/track/duck/types.js | 9 ++++ 5 files changed, 163 insertions(+) create mode 100644 src/stores/track/duck/actions.js create mode 100644 src/stores/track/duck/index.js create mode 100644 src/stores/track/duck/operations.js create mode 100644 src/stores/track/duck/reducers.js create mode 100644 src/stores/track/duck/types.js diff --git a/src/stores/track/duck/actions.js b/src/stores/track/duck/actions.js new file mode 100644 index 0000000..e56259a --- /dev/null +++ b/src/stores/track/duck/actions.js @@ -0,0 +1,19 @@ +import types from './types' + +const getOne = item => ({ + type: types.GET_ONE, item +}) + +const update = item => ({ + type: types.UPDATE, item +}) + +const addRow = item => ({ + type: types.ADD_ROW, item +}) + +export default ({ + getOne, + update, + addRow +}) \ No newline at end of file diff --git a/src/stores/track/duck/index.js b/src/stores/track/duck/index.js new file mode 100644 index 0000000..3b1ec59 --- /dev/null +++ b/src/stores/track/duck/index.js @@ -0,0 +1,5 @@ +import trackReducer from './reducers' +export { default as trackTypes } from './types' +export { default as trackActions } from './actions' + +export default trackReducer \ No newline at end of file diff --git a/src/stores/track/duck/operations.js b/src/stores/track/duck/operations.js new file mode 100644 index 0000000..b9f785c --- /dev/null +++ b/src/stores/track/duck/operations.js @@ -0,0 +1,76 @@ +import AppService from '../../AppService' + +import actions from './actions' + +const trackEndpoint = 'track/' +const trackRowEndpoint = 'track-row/' + +// Track CRUD + +export const getOneTrack = ( id ) => async (dispatch) => { + return await AppService._getOne( + trackEndpoint + id + '/' + ).then( response => { + dispatch( actions.getOne( response ) ) + return response + }) +} + +export const createTrack = async ( track, token ) => { + return await AppService._post( + trackEndpoint, + track, + token + ) +} + +export const updateTrack = async ( id, track, token ) => { + return await AppService._patch( + trackEndpoint + id + '/', + track, + token + ) +} + +export const deleteTrack = async ( id ) => { + return await AppService._delete( + trackEndpoint + id + '/', + token + ) +} + +// Track Row CRUD + +export const getOneTrackRow = ( id ) => async (dispatch) => { + return await AppService._getOne( + trackRowEndpoint + id + '/' + ).then( response => { + dispatch( actions.getOne( response ) ) + return response + }) +} + +export const createTrackRow = async ( track, token ) => { + return await AppService._post( + trackRowEndpoint, + track, + token + ) +} + +export const updateTrackRow = async ( id, track, token ) => { + return await AppService._patch( + trackRowEndpoint + id + '/', + track, + token + ) +} + +export const deleteTrackRow = async ( id ) => { + return await AppService._delete( + trackRowEndpoint + id + '/', + token + ) +} + +// Track Ratings CRUD \ No newline at end of file diff --git a/src/stores/track/duck/reducers.js b/src/stores/track/duck/reducers.js new file mode 100644 index 0000000..e92ea44 --- /dev/null +++ b/src/stores/track/duck/reducers.js @@ -0,0 +1,54 @@ +import types from './types' + +const INITIAL_STATE = { + id: -1, + album_id: -1, + user_id: -1, + title: '', + description: '', + text: '', + image: '', + audio: '', + url_code: '', + track_rows: [] +} + +const trackReducer = ( state = INITIAL_STATE, action ) => { + switch(action.type) { + case types.GET_ONE: + return { + id: action.item.id, + album_id: action.item.album_id, + user_id: action.item.user_id, + title: action.item.title, + description: action.item.description, + text: action.item.text, + image: action.item.image, + audio: action.item.audio, + url_code: action.item.url_code, + track_rows: action.item.track_rows + } + case types.UPDATE: + return { + ...state, + id: action.item.id, + album_id: action.item.album_id, + user_id: action.item.user_id, + title: action.item.title, + description: action.item.description, + text: action.item.text, + image: action.item.image, + audio: action.item.audio, + url_code: action.item.url_code, + } + case types.ADD_ROW: + return { + ...state, + track_rows: [ ...state.track_rows, action.item ] + } + default: + return state + } +} + +export default trackReducer \ No newline at end of file diff --git a/src/stores/track/duck/types.js b/src/stores/track/duck/types.js new file mode 100644 index 0000000..c0c6ae1 --- /dev/null +++ b/src/stores/track/duck/types.js @@ -0,0 +1,9 @@ +const GET_ONE = 'GET_ONE' +const UPDATE = 'UPDATE' +const ADD_ROW = 'ADD_ROW' + +export default ({ + GET_ONE, + UPDATE, + ADD_ROW +}) \ No newline at end of file