Skip to content

Commit

Permalink
Merge pull request #862 from ita-social-projects/matvienko-856-Create…
Browse files Browse the repository at this point in the history
…-HomeworkStudent-model

add homeworkStudent model
  • Loading branch information
MariaMatvienko authored Dec 20, 2021
2 parents 9d3581a + 277efa1 commit ab88057
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 0 deletions.
152 changes: 152 additions & 0 deletions src/models/homework-student/actions.js
Original file line number Diff line number Diff line change
@@ -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),
]);
}
8 changes: 8 additions & 0 deletions src/models/homework-student/index.js
Original file line number Diff line number Diff line change
@@ -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';
15 changes: 15 additions & 0 deletions src/models/homework-student/reducer.js
Original file line number Diff line number Diff line change
@@ -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,
});
Original file line number Diff line number Diff line change
@@ -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;
}
};
Original file line number Diff line number Diff line change
@@ -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;
}
};
Original file line number Diff line number Diff line change
@@ -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;
}
};
4 changes: 4 additions & 0 deletions src/models/homework-student/reducers/index.js
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -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;
}
};
8 changes: 8 additions & 0 deletions src/models/homework-student/selectors.js
Original file line number Diff line number Diff line change
@@ -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;
23 changes: 23 additions & 0 deletions src/models/homework-student/types.js
Original file line number Diff line number Diff line change
@@ -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';
2 changes: 2 additions & 0 deletions src/root-saga.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
importWatcher, exportWatcher
} from './models/index.js';
import { homeworkWatcher } from './models/homework/actions.js';
import { homeworkStudentWatcher } from './models/homework-student/actions.js';

export function* rootSaga() {
yield all([
Expand All @@ -27,6 +28,7 @@ export function* rootSaga() {
fork(attachmentsWatcher),
fork(importWatcher),
fork(homeworkWatcher),
fork(homeworkStudentWatcher),
fork(exportWatcher)
]);
}

0 comments on commit ab88057

Please sign in to comment.