Skip to content

Commit

Permalink
✨ feat: 增加发布课评功能
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieMii committed Aug 25, 2024
1 parent 14caf6f commit 5df02b9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 96 deletions.
168 changes: 77 additions & 91 deletions src/pages/evaluate/evaluate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable react-hooks/rules-of-hooks */
/* eslint-disable import/first */
import {
Button,
Form,
Radio,
Text,
Textarea,
View,
} from '@tarojs/components';
import { Button, Form, Radio, Text, Textarea, View } from '@tarojs/components';
import { useEffect, useState } from 'react';
import './evaluate.scss';

import Label3 from '@/common/components/label3/label3';
import Star from '@/common/components/star/star';
import Taro from '@tarojs/taro';
import { post } from '@/common/utils/fetch';
import Taro from '@tarojs/taro';

export default function evaluate() {
// function generateUniqueID() {
Expand All @@ -32,7 +25,7 @@ export default function evaluate() {
// 示例用法
//const uniqueID = generateUniqueID();

// 初始化状态,存储所有选中的 Radio 项的值
// 初始化状态,存储所有选中的 Radio 项的值
const [selectedValues, setSelectedValues] = useState<string[]>([]);

// 处理 Radio 变化的函数
Expand Down Expand Up @@ -81,103 +74,96 @@ export default function evaluate() {
{ value: 'Humorous', content: '老师风趣幽默' },
{ value: 'LessHomework', content: '平时作业少' },
{ value: 'KeyPointsForFinal', content: '期末划重点' },
{ value: 'ComprehensiveOnlineMaterials', content: '云课堂资料全' },
]
{ value: 'ComprehensiveOnlineMaterials', content: '云课堂资料全' },
];

const [selectedFeatureValues, setSelectedFeatureValues] = useState<string[]>([]);

const handleFeaturesChecked = (value: string) => {
const currentIndex = selectedFeatureValues.indexOf(value);
if (currentIndex > -1) {
// 如果 id 已选中,移除它
const newSelectedFeatureValues = selectedFeatureValues.filter((v, i) => i !== currentIndex);
const newSelectedFeatureValues = selectedFeatureValues.filter(
(v, i) => i !== currentIndex
);
setSelectedFeatureValues(newSelectedFeatureValues);
} else {
// 否则,添加这个 id
setSelectedFeatureValues([...selectedFeatureValues, value]);
}
};



const [textLength, setLength] = useState(0);
const [comment, setComment] = useState('');





const countContent = (e) => {
const { value } = e.detail;
setComment(value); // 更新状态为当前输入框的值
const length = value.length;
setLength(length);
};

// const course_id = 1; //暂时先指定一个courseId来测试使用

// 更新 id 状态为 number 类型
const [courseId, setId] = useState<number | null>(null);
const [courseName, setName] = useState<string | null>('只能评价自己学过的课程哦');

useEffect(() => {
const getParams = () => {
const instance = Taro.getCurrentInstance();
// 使用可选链操作符安全访问 router 和 params
const params = instance?.router?.params || {};

// 确保 id 是 number 类型
setId(params.id ? Number(params.id) : null);
// 解码 name 参数
setName(params.name ? decodeURIComponent(params.name) : '只能评价自己学过的课程哦');

console.log(params.id);
};

getParams();
}, []); // 这个 effect 仅在组件挂载时运行一次

const postEvaluation = () => {
const evaluationobj = {
star_rating: selectedStarIndex,
content: comment,
course_id: courseId,
assessments: selectedValues,
features: selectedFeatureValues,
id: 0,
status: 'Public',
};
console.log(evaluationobj);
post(`/evaluations/save`, evaluationobj).then((res) => {
if (res.code == 0) {
console.log('发布课评成功');
// 使用 redirectTo 跳转
Taro.redirectTo({
url: '/pages/main/index', // 页面路径
});
}



// const course_id = 1; //暂时先指定一个courseId来测试使用

// 更新 id 状态为 number 类型
const [courseId, setId] = useState<number | null>(null);
const [courseName, setName] = useState<string | null>('只能评价自己学过的课程哦');

useEffect(() => {
const getParams = () => {
const instance = Taro.getCurrentInstance();
// 使用可选链操作符安全访问 router 和 params
const params = instance?.router?.params || {};

// 确保 id 是 number 类型
setId(params.id ? Number(params.id) : null);
// 解码 name 参数
setName(params.name ? decodeURIComponent(params.name) : '只能评价自己学过的课程哦');

console.log(params.id);
};

getParams();
}, []); // 这个 effect 仅在组件挂载时运行一次

const postEvaluation = () =>{
const evaluationobj = {
star_rating : selectedStarIndex,
content:comment,
course_id : courseId,
assessments : selectedValues,
features : selectedFeatureValues,
id: 0,
status: "Public"
}
console.log(evaluationobj)
post(`/evaluations/save`, evaluationobj).then((res) => {
if(res.code == 0){
console.log('发布课评成功');
// 使用 redirectTo 跳转
Taro.redirectTo({
url: '/pages/main/index' // 页面路径
});
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
});
}
};

//星级部分的代码
const [selectedStarIndex, setSelectedStarIndex] = useState(-1);
//星级部分的代码
const [selectedStarIndex, setSelectedStarIndex] = useState(-1);

const onStarClick = (index) => {
console.log('选中的星级索引:', index);
setSelectedStarIndex(index+1);
setSelectedStarIndex(index + 1);
// 执行其他需要的逻辑
};

return (
<Form className="view">
<View className="p">
<Text> 选择课程 : </Text>
<Label3 content = {courseName}></Label3>
<Label3 content={courseName}></Label3>
</View>
<View className="p">
<Text>评价星级 :</Text>
Expand All @@ -187,33 +173,33 @@ const [selectedStarIndex, setSelectedStarIndex] = useState(-1);
<Text>考核方式 :</Text>
<View className="ways">
{testways.map((item) => (
<Radio
key={item.value}
className="myradio"
checked={selectedValues.includes(item.value)} // 判断是否包含该项的 value
value={item.value}
color="transparent"
onClick={() => handleRadioChange(item.value)}
>
{item.text}
</Radio>
))}
<Radio
key={item.value}
className="myradio"
checked={selectedValues.includes(item.value)} // 判断是否包含该项的 value
value={item.value}
color="transparent"
onClick={() => handleRadioChange(item.value)}
>
{item.text}
</Radio>
))}
</View>
</View>
<View className="p">
<Text>课程特点</Text>
<View className="fea">
{features.map((item) => {
return (
<Label3
key={item.value}
id={item.value} // 确保 Label3 组件可以访问到 id
content={item.content}
checked={selectedFeatureValues.includes(item.value)} // 判断是否包含该项的 id
handleChecked={() => handleFeaturesChecked(item.value)} // 传递 handleChecked 函数
/>
);
})}
{features.map((item) => {
return (
<Label3
key={item.value}
id={item.value} // 确保 Label3 组件可以访问到 id
content={item.content}
checked={selectedFeatureValues.includes(item.value)} // 判断是否包含该项的 id
handleChecked={() => handleFeaturesChecked(item.value)} // 传递 handleChecked 函数
/>
);
})}
</View>
</View>
<Textarea
Expand Down
12 changes: 7 additions & 5 deletions src/pages/myclass/myclass.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */

import { Picker, Text, View } from '@tarojs/components';
import { useEffect, useState } from 'react';
import Taro from '@tarojs/taro';
import { useEffect, useState } from 'react';

import './myclass.scss';

Expand Down Expand Up @@ -65,11 +65,10 @@ export default function Myclass() {
const query = `?id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}`;
// 使用 navigateTo 跳转到 evaluate 页面,并传递参数
Taro.navigateTo({
url: `/pages/evaluate/evaluate${query}`
url: `/pages/evaluate/evaluate${query}`,
});
};


return (
<View>
<View className="select">
Expand All @@ -89,8 +88,11 @@ export default function Myclass() {
</View>
<View className="classes">
{myclasses.map((each, index) => (
<View key={index} className="eachClass"
onClick={() => handleClassClick(each.id, each.name)}>
<View
key={index}
className="eachClass"
onClick={() => handleClassClick(each.id, each.name)}
>
<Text className="classname">{each.name}</Text>
<Text className="classteacher">{each.teacher}</Text>
<Text className="classstatus">{each.evaluated ? '已评课' : '未评课'}</Text>
Expand Down

0 comments on commit 5df02b9

Please sign in to comment.