From 949dafeb146dabee1e0245912ad44c5f43a651bd Mon Sep 17 00:00:00 2001 From: matvienko Date: Tue, 7 Dec 2021 09:57:20 +0200 Subject: [PATCH] add homeworkStudent model --- src/models/homework-student/actions.js | 152 ++++++++++++++++++ src/models/homework-student/index.js | 8 + src/models/homework-student/reducer.js | 15 ++ .../reducers/add-homework-student-reducer.js | 46 ++++++ .../get-by-id-homework-student-reducer.js | 36 +++++ .../reducers/get-homework-student-reducer.js | 36 +++++ src/models/homework-student/reducers/index.js | 4 + .../update-homework-student-reducer.js | 36 +++++ src/models/homework-student/selectors.js | 8 + src/models/homework-student/types.js | 23 +++ src/root-saga.js | 4 +- 11 files changed, 367 insertions(+), 1 deletion(-) create mode 100644 src/models/homework-student/actions.js create mode 100644 src/models/homework-student/index.js create mode 100644 src/models/homework-student/reducer.js create mode 100644 src/models/homework-student/reducers/add-homework-student-reducer.js create mode 100644 src/models/homework-student/reducers/get-by-id-homework-student-reducer.js create mode 100644 src/models/homework-student/reducers/get-homework-student-reducer.js create mode 100644 src/models/homework-student/reducers/index.js create mode 100644 src/models/homework-student/reducers/update-homework-student-reducer.js create mode 100644 src/models/homework-student/selectors.js create mode 100644 src/models/homework-student/types.js diff --git a/src/models/homework-student/actions.js b/src/models/homework-student/actions.js new file mode 100644 index 00000000..15afa0a0 --- /dev/null +++ b/src/models/homework-student/actions.js @@ -0,0 +1,152 @@ +import { + all, + fork, + put, + call, + takeLatest, + takeEvery, +} from 'redux-saga/effects'; +import { ApiService } from '../../shared/api-service'; +import * as actionTypes from './types'; + +export const addHomeworkStudent = (homeworkStudent) => ({ + type: actionTypes.ADD_HOMEWORK_STUDENT, + payload: { + homeworkStudent, + }, +}); + +export const getHomeworkStudent = () => ({ + type: actionTypes.GET_HOMEWORK_STUDENT, + payload: { + }, +}); + +export const getHomeworkByIdStudent = (id) => ({ + type: actionTypes.GET_HOMEWORK_STUDENT_BY_ID, + payload: { + id, + }, +}); + +export const updateHomeworkStudent = (id, homeworkStudent) => ({ + type: actionTypes.UPDATE_HOMEWORK_STUDENT, + payload: { + id, + homeworkStudent + }, +}); + +function* addHomeworkStudentWorker(data) { + try { + yield put({ type: actionTypes.ADDING_HOMEWORK_STUDENT_STARTED }); + const homeworkStudent = yield call( + ApiService.create, + '/homeworkstudent', + data.payload.homeworkStudent + ); + + yield put({ + type: actionTypes.ADDING_HOMEWORK_STUDENT_SUCCESS, + payload: { homeworkStudent }, + }); + + yield put({ type: actionTypes.CLEAR_LOADED }); + } catch (error) { + yield put({ + type: actionTypes.ADDING_HOMEWORK_STUDENT_FAILED, + payload: { error }, + }); + + yield put({ type: actionTypes.CLEAR_ERROR }); + } +} + +function* getHomeworkStudentWorker() { + try { + yield put({ type: actionTypes.LOADING_HOMEWORK_STUDENT_STARTED }); + + const homeworkStudent = yield call( + ApiService.load, + `/homeworkstudent` + ); + + yield put({ + type: actionTypes.LOADING_HOMEWORK_STUDENT_SUCCESS, + payload: { homeworkStudent }, + }); + } catch (error) { + yield put({ + type: actionTypes.LOADING_HOMEWORK_STUDENT_FAILED, + payload: { error }, + }); + } +} + +function* getHomeworkStudentByIdWorker(data) { + try { + yield put({ type: actionTypes.LOADING_HOMEWORK_STUDENT_BY_ID_STARTED }); + + const homeworkStudent = yield call( + ApiService.load, + `/homeworkstudent/${data.payload.id}` + ); + + yield put({ + type: actionTypes.LOADING_HOMEWORK_STUDENT_BY_ID_SUCCESS, + payload: { homeworkStudent }, + }); + } catch (error) { + yield put({ + type: actionTypes.LOADING_HOMEWORK_STUDENT_BY_ID_FAILED, + payload: { error }, + }); + } +} + +function* updateHomeworkStudentWorker(data) { + try { + yield put({ type: actionTypes.UPDATING_HOMEWORK_STUDENT_STARTED }); + + const homeworkStudent = yield call( + ApiService.update, + `/homeworkstudent/${data.payload.id}`, + data.payload.homeworkStudent + ); + + yield put({ + type: actionTypes.UPDATING_HOMEWORK_STUDENT_SUCCESS, + payload: { homeworkStudent }, + }); + } catch (error) { + yield put({ + type: actionTypes.UPDATING_HOMEWORK_STUDENT_FAILED, + payload: { error }, + }); + } +} + +function* getHomeworkStudentWatcher() { + yield takeLatest(actionTypes.GET_HOMEWORK_STUDENT, getHomeworkStudentWorker); +} + +function* getHomeworkStudentByIdWatcher() { + yield takeLatest(actionTypes.GET_HOMEWORK_STUDENT_BY_ID, getHomeworkStudentByIdWorker); +} + +function* addHomeworkStudentWatcher() { + yield takeEvery(actionTypes.ADD_HOMEWORK_STUDENT, addHomeworkStudentWorker); +} + +function* updateHomeworkStudentWatcher() { + yield takeEvery(actionTypes.UPDATE_HOMEWORK_STUDENT, updateHomeworkStudentWorker); +} + +export function* homeworkStudentWatcher() { + yield all([ + fork(getHomeworkStudentWatcher), + fork(getHomeworkStudentByIdWatcher), + fork(addHomeworkStudentWatcher), + fork(updateHomeworkStudentWatcher), + ]); +} diff --git a/src/models/homework-student/index.js b/src/models/homework-student/index.js new file mode 100644 index 00000000..227c1661 --- /dev/null +++ b/src/models/homework-student/index.js @@ -0,0 +1,8 @@ +export { homeworkStudentReducer } from './reducer.js'; +export { getHomeworkStudent, getHomeworkByIdStudent, addHomeworkStudent, updateHomeworkStudent } from './actions.js'; +export { + homeworkStudentSelector, + homeworkByIdStudentSelector, + addedHomeworkStudentSelector, + updatedHomeworkStudentSelector, +} from './selectors.js'; diff --git a/src/models/homework-student/reducer.js b/src/models/homework-student/reducer.js new file mode 100644 index 00000000..7f111fd8 --- /dev/null +++ b/src/models/homework-student/reducer.js @@ -0,0 +1,15 @@ +import { combineReducers } from 'redux'; + +import { + addHomeworkStudentReducer, + updateHomeworkStudentReducer, + getHomeworkStudentReducer, + getHomeworkByIdStudentReducer, +} from './reducers'; + +export const homeworkStudentReducer = combineReducers({ + homeworkStudent: getHomeworkStudentReducer, + homeworkByIdStudent: getHomeworkByIdStudentReducer, + updatedHomeworkStudent: updateHomeworkStudentReducer, + addedHomeworkStudent: addHomeworkStudentReducer, +}); diff --git a/src/models/homework-student/reducers/add-homework-student-reducer.js b/src/models/homework-student/reducers/add-homework-student-reducer.js new file mode 100644 index 00000000..fd31289e --- /dev/null +++ b/src/models/homework-student/reducers/add-homework-student-reducer.js @@ -0,0 +1,46 @@ +import * as actionTypes from '../types'; + +const INITIAL_STATE = { + data: {}, + isLoading: false, + loaded: false, + error: '', +}; + +export const addHomeworkStudentReducer = (state = INITIAL_STATE, action) => { + switch (action.type) { + case actionTypes.ADDING_HOMEWORK_STUDENT_STARTED: + return { + ...state, + isLoading: true, + error: '', + }; + case actionTypes.ADDING_HOMEWORK_STUDENT_SUCCESS: + return { + ...state, + isLoading: false, + loaded: true, + data: action.payload.homeworkStudent, + error: '', + }; + case actionTypes.ADDING_HOMEWORK_STUDENT_FAILED: + return { + ...state, + isLoading: false, + loaded: false, + error: action.payload.error, + }; + case actionTypes.CLEAR_LOADED: + return { + ...state, + loaded: false, + }; + case actionTypes.CLEAR_ERROR: + return { + ...state, + error: '', + }; + default: + return state; + } +}; diff --git a/src/models/homework-student/reducers/get-by-id-homework-student-reducer.js b/src/models/homework-student/reducers/get-by-id-homework-student-reducer.js new file mode 100644 index 00000000..83b93fd8 --- /dev/null +++ b/src/models/homework-student/reducers/get-by-id-homework-student-reducer.js @@ -0,0 +1,36 @@ +import * as actionTypes from '../types'; + +const INITIAL_STATE = { + data: {}, + isLoading: false, + loaded: false, + error: '', +}; + +export const getHomeworkByIdStudentReducer = (state = INITIAL_STATE, action) => { + switch (action.type) { + case actionTypes.LOADING_HOMEWORK_STUDENT_BY_ID_STARTED: + return { + ...state, + isLoading: true, + error: '', + }; + case actionTypes.LOADING_HOMEWORK_STUDENT_BY_ID_SUCCESS: + return { + ...state, + isLoading: false, + loaded: true, + data: action.payload.homeworkStudent, + error: '', + }; + case actionTypes.LOADING_HOMEWORK_STUDENT_BY_ID_FAILED: + return { + ...state, + isLoading: false, + loaded: false, + error: action.payload.error, + }; + default: + return state; + } +}; diff --git a/src/models/homework-student/reducers/get-homework-student-reducer.js b/src/models/homework-student/reducers/get-homework-student-reducer.js new file mode 100644 index 00000000..badb3669 --- /dev/null +++ b/src/models/homework-student/reducers/get-homework-student-reducer.js @@ -0,0 +1,36 @@ +import * as actionTypes from '../types'; + +const INITIAL_STATE = { + data: {}, + isLoading: false, + loaded: false, + error: '', +}; + +export const getHomeworkStudentReducer = (state = INITIAL_STATE, action) => { + switch (action.type) { + case actionTypes.LOADING_HOMEWORK_STUDENT_STARTED: + return { + ...state, + isLoading: true, + error: '', + }; + case actionTypes.LOADING_HOMEWORK_STUDENT_SUCCESS: + return { + ...state, + isLoading: false, + loaded: true, + data: action.payload.homeworkStudent, + error: '', + }; + case actionTypes.LOADING_HOMEWORK_STUDENT_FAILED: + return { + ...state, + isLoading: false, + loaded: false, + error: action.payload.error, + }; + default: + return state; + } +}; diff --git a/src/models/homework-student/reducers/index.js b/src/models/homework-student/reducers/index.js new file mode 100644 index 00000000..618882b1 --- /dev/null +++ b/src/models/homework-student/reducers/index.js @@ -0,0 +1,4 @@ +export { addHomeworkStudentReducer } from './add-homework-student-reducer'; +export { getHomeworkStudentReducer } from './get-homework-student-reducer'; +export { getHomeworkByIdStudentReducer } from './get-by-id-homework-student-reducer'; +export { updateHomeworkStudentReducer } from './update-homework-student-reducer'; diff --git a/src/models/homework-student/reducers/update-homework-student-reducer.js b/src/models/homework-student/reducers/update-homework-student-reducer.js new file mode 100644 index 00000000..df943330 --- /dev/null +++ b/src/models/homework-student/reducers/update-homework-student-reducer.js @@ -0,0 +1,36 @@ +import * as actionTypes from '../types'; + +const INITIAL_STATE = { + data: {}, + isLoading: false, + loaded: false, + error: '', +}; + +export const updateHomeworkStudentReducer = (state = INITIAL_STATE, action) => { + switch (action.type) { + case actionTypes.UPDATING_HOMEWORK_STUDENT_STARTED: + return { + ...state, + isLoading: true, + error: '', + }; + case actionTypes.UPDATING_HOMEWORK_STUDENT_SUCCESS: + return { + ...state, + isLoading: false, + loaded: true, + data: action.payload.homeworkStudent, + error: '', + }; + case actionTypes.UPDATING_HOMEWORK_STUDENT_FAILED: + return { + ...state, + isLoading: false, + loaded: false, + error: action.payload.error, + }; + default: + return state; + } +}; diff --git a/src/models/homework-student/selectors.js b/src/models/homework-student/selectors.js new file mode 100644 index 00000000..cd741fd9 --- /dev/null +++ b/src/models/homework-student/selectors.js @@ -0,0 +1,8 @@ +export const homeworkStudentSelector = (state) => +state.models.homework.homeworkStudent; +export const homeworkByIdStudentSelector = (state) => +state.models.homework.homeworkStudent; +export const updatedHomeworkStudentSelector = (state) => + state.models.homework.updatedHomeworkStudent; +export const addedHomeworkStudentSelector = (state) => + state.models.homework.addedHomeworkStudent; diff --git a/src/models/homework-student/types.js b/src/models/homework-student/types.js new file mode 100644 index 00000000..bd25989a --- /dev/null +++ b/src/models/homework-student/types.js @@ -0,0 +1,23 @@ +export const ADD_HOMEWORK_STUDENT = 'HOMEWORK/ADD_HOMEWORK_STUDENT'; +export const UPDATE_HOMEWORK_STUDENT = 'HOMEWORK/UPDATE_HOMEWORK_STUDENT'; +export const GET_HOMEWORK_STUDENT = 'HOMEWORK/GET_HOMEWORK_STUDENT'; +export const GET_HOMEWORK_STUDENT_BY_ID = 'HOMEWORK/GET_HOMEWORK_STUDENT_BY_ID'; + +export const ADDING_HOMEWORK_STUDENT_STARTED = 'HOMEWORK/ADDING_HOMEWORK_STUDENT_STARTED'; +export const ADDING_HOMEWORK_STUDENT_SUCCESS = 'HOMEWORK/ADDING_HOMEWORK_STUDENT_SUCCESS'; +export const ADDING_HOMEWORK_STUDENT_FAILED = 'HOMEWORK/ADDING_HOMEWORK_STUDENT_FAILED'; + +export const LOADING_HOMEWORK_STUDENT_STARTED = 'HOMEWORK/LOADING_HOMEWORK_STUDENT_STARTED'; +export const LOADING_HOMEWORK_STUDENT_SUCCESS = 'HOMEWORK/LOADING_HOMEWORK_STUDENT_SUCCESS'; +export const LOADING_HOMEWORK_STUDENT_FAILED = 'HOMEWORK/LOADING_HOMEWORK_STUDENT_FAILED'; + +export const LOADING_HOMEWORK_STUDENT_BY_ID_STARTED = 'HOMEWORK/LOADING_HOMEWORK_STUDENT_BY_ID_STARTED'; +export const LOADING_HOMEWORK_STUDENT_BY_ID_SUCCESS = 'HOMEWORK/LOADING_HOMEWORK_STUDENT_BY_ID_SUCCESS'; +export const LOADING_HOMEWORK_STUDENT_BY_ID_FAILED = 'HOMEWORK/LOADING_HOMEWORK_STUDENT_BY_ID_FAILED'; + +export const UPDATING_HOMEWORK_STUDENT_STARTED = 'HOMEWORK/UPDATING_HOMEWORK_STUDENT_STARTED'; +export const UPDATING_HOMEWORK_STUDENT_SUCCESS = 'HOMEWORK/UPDATING_HOMEWORK_STUDENT_SUCCESS'; +export const UPDATING_HOMEWORK_STUDENT_FAILED = 'HOMEWORK/UPDATING_HOMEWORK_STUDENT_FAILED'; + +export const CLEAR_LOADED = 'HOMEWORK_STUDENT/CLEAR_LOADED'; +export const CLEAR_ERROR = 'HOMEWORK_STUDENT/CLEAR_ERROR'; diff --git a/src/root-saga.js b/src/root-saga.js index f65fa1a7..7a17f8ab 100644 --- a/src/root-saga.js +++ b/src/root-saga.js @@ -9,6 +9,7 @@ import { importWatcher, } from './models/index.js'; import { homeworkWatcher } from './models/homework/actions.js'; +import { homeworkStudentWatcher } from './models/homework-student/actions.js'; export function* rootSaga() { yield all([ @@ -26,6 +27,7 @@ export function* rootSaga() { fork(dashboardWatcher), fork(attachmentsWatcher), fork(importWatcher), - fork(homeworkWatcher) + fork(homeworkWatcher), + fork(homeworkStudentWatcher) ]); } \ No newline at end of file