From 6de00e05f25183ebc011d445c101f5b12a382954 Mon Sep 17 00:00:00 2001 From: tangjiang <465264938@qq.com> Date: Mon, 10 Feb 2020 18:25:45 +0800 Subject: [PATCH] update oj url match question --- .../modules/developer/newOrEditTask/index.js | 35 ++++++++++++++++--- .../modules/developer/studentStudy/index.js | 33 ++++++++++++----- public/react/src/redux/actions/actionTypes.js | 1 + public/react/src/redux/actions/index.js | 4 ++- public/react/src/redux/actions/ojForUser.js | 8 +++-- public/react/src/redux/actions/ojForm.js | 23 +++++++++--- .../react/src/redux/reducers/ojFormReducer.js | 11 +++++- 7 files changed, 91 insertions(+), 24 deletions(-) diff --git a/public/react/src/modules/developer/newOrEditTask/index.js b/public/react/src/modules/developer/newOrEditTask/index.js index cbe536999..2e19471a2 100644 --- a/public/react/src/modules/developer/newOrEditTask/index.js +++ b/public/react/src/modules/developer/newOrEditTask/index.js @@ -35,7 +35,9 @@ const NewOrEditTask = (props) => { getUserInfoForNew, handleCancelPublish, validateOjForm, - getQuestion + getQuestion, + saveSearchParams, + setOjInitialValue // updateTestAndValidate, } = props; @@ -62,6 +64,25 @@ const NewOrEditTask = (props) => { const id = props.match.params.id; // 保存OJForm的id号,指明是编辑还是新增 props.saveOJFormId(id); + // 获取地址栏查询参数 + const $searchs = window.location.search && window.location.search.substring(1); + if ($searchs) { + const $params = $searchs.split('&') || []; + let obj = Object.create(null); + $params.forEach(item => { + const keys = item.split('='); + obj[keys[0]] = keys[1]; + }); + // 保存初始值 + if (obj['newoj']) { + setOjInitialValue({ + difficult: obj['difficult'], + sub_discipline_id: obj['sub_discipline_id'], + tag_discipline_id: obj['tag_discipline_id'] && obj['tag_discipline_id'].split(',') + }); + } + saveSearchParams({searchParams: $searchs, curPage: obj['pages']}); + } if (id) { // id号即 identifier // TODO id 存在时, 编辑, 获取 store 中的记录数 props.getOJFormById(id); @@ -96,7 +117,8 @@ const NewOrEditTask = (props) => { props.clearOJFormStore(); // 清空描述信息 toStore('oj_description', ''); - props.history.push('/problems'); + // props.history.push('/problems'); + props.history.push(`/question?${props.searchParams}`); } // 发布 @@ -236,7 +258,7 @@ const NewOrEditTask = (props) => { } const mapStateToProps = (state) => { - const { ojForm, identifier, testCases, isPublish } = state.ojFormReducer; + const { ojForm, identifier, testCases, isPublish, searchParams } = state.ojFormReducer; const { publishLoading, submitLoading } = state.commonReducer; const { userInfo } = state.userReducer; return { @@ -246,7 +268,8 @@ const mapStateToProps = (state) => { isPublish, // 是否已发布 publishLoading, submitLoading, - userInfo + userInfo, + searchParams } }; @@ -278,7 +301,9 @@ const mapDispatchToProps = (dispatch) => ({ // 新建时获取信息 getUserInfoForNew: () => dispatch(actions.getUserInfoForNew()), validateOjForm: (props, type, cb) => dispatch(actions.validateOjForm(props, type, cb)), - getQuestion: (params) => dispatch(actions.getQuestion(params)) + getQuestion: (params) => dispatch(actions.getQuestion(params)), + saveSearchParams: (params) => dispatch(actions.saveSearchParams(params)), + setOjInitialValue: (params) => dispatch(actions.setOjInitialValue(params)) }); export default withRouter(connect( diff --git a/public/react/src/modules/developer/studentStudy/index.js b/public/react/src/modules/developer/studentStudy/index.js index c5e1fad38..d40269e7c 100644 --- a/public/react/src/modules/developer/studentStudy/index.js +++ b/public/react/src/modules/developer/studentStudy/index.js @@ -4,7 +4,7 @@ * @Github: * @Date: 2019-11-23 10:53:19 * @LastEditors : tangjiang - * @LastEditTime : 2020-01-06 15:27:34 + * @LastEditTime : 2020-02-10 18:24:01 */ import './index.less'; import React, { useEffect, useState } from 'react'; @@ -28,7 +28,9 @@ function StudentStudy(props) { // user_program_identifier, restoreInitialCode, changeUserCodeTab, - changeShowOrHideControl + changeShowOrHideControl, + searchParams, + saveSearchParams } = props; const { @@ -44,7 +46,16 @@ function StudentStudy(props) { saveUserProgramIdentifier(id); // startProgramQuestion(id); getUserProgramDetail(id); - + const $searchs = window.location.search && window.location.search.substring(1); + if ($searchs) { + const $params = $searchs.split('&') || []; + let obj = Object.create(null); + $params.forEach(item => { + const keys = item.split('='); + obj[keys[0]] = keys[1]; + }); + saveSearchParams({searchParams: $searchs, curPage: obj['pages']}); + } if (tab) { changeUserCodeTab(tab); } @@ -94,7 +105,7 @@ function StudentStudy(props) { changeShowOrHideControl(false); props.saveEditorCodeForDetail(''); props.clearOjForUserReducer(); - props.history.push(`/problems/${identifier}/edit`); + props.history.push(`/problems/${identifier}/edit?{searchParams}`); } // 处理退出 const handleClickQuit = () => { @@ -103,7 +114,8 @@ function StudentStudy(props) { // 将控制台关闭 changeShowOrHideControl(false); props.saveEditorCodeForDetail(''); - props.history.push('/problems'); + // props.history.push('/problems'); + props.history.push(`/question?${searchParams}`); } return ( @@ -159,13 +171,15 @@ function StudentStudy(props) { const mapStateToProps = (state) => { const { userInfo } = state.userReducer; - const { hack_identifier, user_program_identifier, hack } = state.ojForUserReducer; + const { hack_identifier, user_program_identifier, hack } = state.ojForUserReducer; + const { searchParams } = state.ojFormReducer; return { hack, userInfo, user_program_identifier, - hack_identifier - }; + hack_identifier, + searchParams + }; }; const mapDispatchToProps = (dispatch) => ({ @@ -179,7 +193,8 @@ const mapDispatchToProps = (dispatch) => ({ restoreInitialCode: (identifier, msg) => dispatch(actions.restoreInitialCode(identifier, msg)), changeShowOrHideControl: (flag) => dispatch(actions.changeShowOrHideControl(flag)), clearOjForUserReducer: () => dispatch(actions.clearOjForUserReducer()), - changeUserCodeTab: (tab) => dispatch(actions.changeUserCodeTab(tab)) + changeUserCodeTab: (tab) => dispatch(actions.changeUserCodeTab(tab)), + saveSearchParams: (params) => dispatch(actions.saveSearchParams(params)) }); export default withRouter(connect( diff --git a/public/react/src/redux/actions/actionTypes.js b/public/react/src/redux/actions/actionTypes.js index 22dfa5b77..4d422fc91 100644 --- a/public/react/src/redux/actions/actionTypes.js +++ b/public/react/src/redux/actions/actionTypes.js @@ -37,6 +37,7 @@ const types = { GET_COURSE_QUESTION: 'GET_COURSE_QUESTION', // 获取编辑题 CHANGE_KNOWLEDGES: 'CHANGE_KNOWLEDGES', // 保存所选择的知识点 SET_OJ_INITIAL_VALUE: 'SET_OJ_INITIAL_VALUE', // 设置初始值 + SET_SEARCH_PARAMS: 'SET_SEARCH_PARAMS', // 只在地址栏参数 // study SAVE_USER_PROGRAM_ID: 'SAVE_USER_PROGRAM_ID',// 保存用户编程题id值 USER_PROGRAM_DETAIL: 'USER_PROGRAM_DETAIL', // 用户编程题详情 diff --git a/public/react/src/redux/actions/index.js b/public/react/src/redux/actions/index.js index 9fbfc3c7a..a52a6c530 100644 --- a/public/react/src/redux/actions/index.js +++ b/public/react/src/redux/actions/index.js @@ -38,7 +38,8 @@ import { getQuestion, saveKnowledge, setOjInitialValue, - tagDisciplines + tagDisciplines, + saveSearchParams } from './ojForm'; import { @@ -145,6 +146,7 @@ export default { saveKnowledge, setOjInitialValue, tagDisciplines, + saveSearchParams, // addTestCase, deleteTestCase, diff --git a/public/react/src/redux/actions/ojForUser.js b/public/react/src/redux/actions/ojForUser.js index e7520c533..87c877fa5 100644 --- a/public/react/src/redux/actions/ojForUser.js +++ b/public/react/src/redux/actions/ojForUser.js @@ -4,7 +4,7 @@ * @Github: * @Date: 2019-11-27 13:42:11 * @LastEditors : tangjiang - * @LastEditTime : 2020-01-09 14:14:47 + * @LastEditTime : 2020-02-10 18:17:00 */ import types from "./actionTypes"; import { Base64 } from 'js-base64'; @@ -24,7 +24,9 @@ import { notification } from "antd"; // 进入编程页面时,首先调用开启编程题接口 export const startProgramQuestion = (id, props) => { - return (dispatch) => { + return (dispatch, getState) => { + const {searchParams} = getState().ojFormReducer; + console.log(searchParams); fetchStartProgram(id).then(res => { const { status, data } = res; if (status === 200) { @@ -48,7 +50,7 @@ export const startProgramQuestion = (id, props) => { // console.log(path); // props.history.push(`/myproblems/${identifier}`); props.history.push({ - pathname: `/myproblems/${identifier}`, + pathname: `/myproblems/${identifier}?${searchParams}`, }); } } diff --git a/public/react/src/redux/actions/ojForm.js b/public/react/src/redux/actions/ojForm.js index aa8a7f50d..9098dae81 100644 --- a/public/react/src/redux/actions/ojForm.js +++ b/public/react/src/redux/actions/ojForm.js @@ -4,7 +4,7 @@ * @Github: * @Date: 2019-11-20 16:35:46 * @LastEditors : tangjiang - * @LastEditTime : 2020-01-07 16:45:34 + * @LastEditTime : 2020-02-10 18:09:17 */ import types from './actionTypes'; import CONST from '../../constants'; @@ -206,7 +206,7 @@ export const validateOjForm = (props, type, cb) => { if (hasSuccess) { // console.log('表单保存的数据为: ', getState()); const {ojFormReducer} = getState(); - const {code, score, ojForm, testCases = [], tag_discipline_id = []} = ojFormReducer; + const {code, score, ojForm, testCases = [], tag_discipline_id = [], curPage = 1} = ojFormReducer; const {category, description, difficult, language, name, openOrNot, timeLimit, sub_discipline_id} = ojForm; let paramsObj = {}; const hack = { // 编程题干 @@ -312,14 +312,19 @@ export const validateOjForm = (props, type, cb) => { payload: identifier }); - console.log(identifier , props.identifier); + // console.log(identifier , props.identifier); if (identifier || props.identifier) { dispatch(getOJFormById(identifier || props.identifier)); } // 保存成功后,调用编辑接口并改变路 if (paramsObj['submitType'] === 'add' && identifier) { - - props.history.push(`/problems/${identifier}/edit`); + //更改查询参数 + dispatch({ + type: types.SET_SEARCH_PARAMS, + searchParams: `editoj=1&pages=${curPage}`, + curPage: curPage + }); + props.history.push(`/problems/${identifier}/edit?editoj=1&pages=${curPage}`); }; } // 保存或更新后,调用start接口 @@ -763,3 +768,11 @@ export const tagDisciplines = (params) => { }); } } + +// 保存地址栏参数 +export const saveSearchParams = (params) => { + return { + type: types.SET_SEARCH_PARAMS, + payload: params + } +} diff --git a/public/react/src/redux/reducers/ojFormReducer.js b/public/react/src/redux/reducers/ojFormReducer.js index 0da4d415b..adbbcfa92 100644 --- a/public/react/src/redux/reducers/ojFormReducer.js +++ b/public/react/src/redux/reducers/ojFormReducer.js @@ -4,7 +4,7 @@ * @Github: * @Date: 2019-11-20 16:40:32 * @LastEditors : tangjiang - * @LastEditTime : 2020-01-03 17:38:50 + * @LastEditTime : 2020-02-10 17:59:55 */ import { Base64 } from 'js-base64'; import types from '../actions/actionTypes'; @@ -75,6 +75,8 @@ const init = { isPublish: 0, // 是否是发布状态: 0 未发布 1 已发布 courseQuestions: [], // 课程题库 knowledges: [], // 知识点下拉值 + searchParams: '', // 地址栏参数 + curPage: 1, // 当前页数 } const tcValidateObj = { @@ -350,11 +352,18 @@ const ojFormReducer = (state = initialState, action) => { } case types.SET_OJ_INITIAL_VALUE: const _p = action.payload; + console.log(_p.tag_discipline_id); return { ...state, ojForm: Object.assign({}, state.ojForm, {difficult: _p.difficult, sub_discipline_id: _p.sub_discipline_id}), tag_discipline_id: _p.tag_discipline_id || [] } + case types.SET_SEARCH_PARAMS: + return { + ...state, + searchParams: action.payload.searchParams, + curPage: action.payload.curPage + } default: return state; }