Skip to content

Commit

Permalink
fix(bugs): toast显示问题
Browse files Browse the repository at this point in the history
  • Loading branch information
eleliauk committed Dec 24, 2024
2 parents b059f6a + 50ff2b4 commit d6ac23e
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 85 deletions.
2 changes: 1 addition & 1 deletion project.private.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"setting": {
"compileHotReLoad": true,
"bigPackageSizeSupport": true,
"urlCheck": false,
"urlCheck": true,
"autoAudits": true,
"preloadBackgroundData": false,
"skylineRenderEnable": false
Expand Down
22 changes: 10 additions & 12 deletions src/common/components/chart/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Image, View } from '@tarojs/components';
import Taro, { CanvasContext } from '@tarojs/taro';
import React, { CSSProperties, useEffect } from 'react';
import React, { CSSProperties, useEffect, useMemo } from 'react';

interface LineChartProps {
data?: number[];
Expand Down Expand Up @@ -58,8 +58,10 @@ const LineChart: React.FC<LineChartProps> = (props) => {
const ctx = Taro.createCanvasContext(id ?? DEFAULT_CHART_ID);
void drawChart(ctx);
}
}, []);

}, [propData]);
const hasData = useMemo(() => {
return !propData?.some((data) => data);
}, [propData]);
const drawChart = (ctx: CanvasContext) => {
// 初始化
const data = propData ?? DEFAULT_DATA;
Expand Down Expand Up @@ -133,10 +135,10 @@ const LineChart: React.FC<LineChartProps> = (props) => {
// 高亮
ctx.lineWidth = 1;
const highlightPos = heightLightPercent ?? DEFAULT_HEIGHTLIGHT_POS;
const centerX = padding + barWidth * highlightPos + BLANK;
const centerX = ((width - 2 * padding) / 7) * highlightPos + (1 / 2) * barWidth;
drawGradientRectangle(
ctx,
centerX - barWidth / 2,
centerX + barWidth / 2,
padding - 6,
barWidth,
height - 2 * padding + 6,
Expand All @@ -146,11 +148,7 @@ const LineChart: React.FC<LineChartProps> = (props) => {
ctx.beginPath();
ctx.fillStyle = DEFAULT_TEXT_COLOR;
ctx.font = '15px sans-serif';
ctx.fillText(
title ?? DEFAULT_TITLE,
centerX - ctx.measureText(title ?? DEFAULT_TITLE).width / 2,
padding / 2
);
ctx.fillText(title ?? DEFAULT_TITLE, centerX, padding / 2);
void ctx.draw();
};

Expand All @@ -170,8 +168,8 @@ const LineChart: React.FC<LineChartProps> = (props) => {
},
}}
>
{propData && <View>数据量太少,暂不支持预览</View>}
{propData ? (
{hasData && <View>数据量太少,暂不支持预览</View>}
{hasData ? (
<Image
src="https://s2.loli.net/2024/12/11/sJ9kANj5yz6HiUa.png"
style={{
Expand Down
87 changes: 43 additions & 44 deletions src/pages/classInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable import/first */
import { Image, Text, View } from '@tarojs/components';

import Taro, { useDidShow } from '@tarojs/taro';
import Taro from '@tarojs/taro';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { AtIcon } from 'taro-ui';

Expand Down Expand Up @@ -129,37 +128,30 @@ export default function Index() {

if (courseId) void getCommentData();
};
const fetchAnswer = () => {
const fetchAnswer = async () => {
try {
void get(
const res = await get(
`/questions/list?biz=Course&biz_id=${courseId}&cur_question_id=${0}&limit=${3}`
).then((res) => {
console.log(res);

console.log('questionlist1', res.data);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return
const questionsWithAnswers = res.data.map((item) => ({
...item,
preview_answers: item.preview_answers || [],
}));
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
setQuestionlist(res.data);
Taro.hideLoading();
});
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
setQuestionlist(res.data);
} catch (e) {
console.error('Failed to fetch course data:', e);
throw e;
}
};
const fetchGrades = async () => {
try {
await get(`/grades/courses/${courseId}`).then((res) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
setGrade(res.data); // 设置 grade 数据
!res.data && bailout();
Taro.hideLoading();
});
const res = await get(`/grades/courses/${courseId}`);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
setGrade(res.data);
if (!res.data) {
bailout();
return;
}
} catch (err) {
console.error('Failed to fetch grades data', err);
throw err;
}
};
const getNumData = () => {
Expand All @@ -176,26 +168,29 @@ export default function Index() {
useEffect(() => {
initData();
}, [courseId]);
useDidShow(() => {
initData();
if (courseId) {
void Taro.showLoading({
title: '加载中',
});
void fetchGrades();
void getNumData();
void fetchAnswer();
}
});
useEffect(() => {
if (courseId) {
void Taro.showLoading({
title: '加载中',
});
void fetchGrades();
void getNumData();
void fetchAnswer();
}
const fetchData = async () => {
if (!courseId) return;

try {
void Taro.showLoading({
title: '加载中',
});

// 并行请求数据
await Promise.all([fetchGrades(), getNumData(), fetchAnswer()]);
} catch (error) {
console.error('Failed to fetch data:', error);
void Taro.showToast({
title: '加载失败',
icon: 'error',
});
} finally {
void Taro.hideLoading();
}
};

void fetchData();
}, [courseId]);
// 监听 collect 状态更新
useEffect(() => {
Expand All @@ -214,8 +209,12 @@ export default function Index() {
);
const { heightLightPercent, yData } = useMemo(() => {
const percent = (grade?.avg ?? 0) / 10;
console.log(
grade?.grades.map((item) => item.total_grades?.length ?? 0),
grade?.grades
);
return {
heightLightPercent: percent > 4 ? percent - 3 : 0,
heightLightPercent: percent > 4 ? percent - 4 : 0,
yData: grade?.grades.map((item) => item.total_grades?.length ?? 0),
};
}, [grade]);
Expand Down Expand Up @@ -244,7 +243,7 @@ export default function Index() {
<Image src={Icon as string} className="h-full w-full"></Image>
</View>
<Text className="text-3xl font-semibold tracking-widest text-[#FFD777]">
木犀课栈 此功能敬请期待
木犀课栈
</Text>
</View>
</View>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/evaluate/evaluate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export default function evaluate() {
<Image src={Icon as string} className="h-full w-full"></Image>
</View>
<Text className="text-3xl font-semibold tracking-widest text-[#FFD777]">
木犀课栈 此功能敬请期待
木犀课栈
</Text>
</View>
</View>
Expand Down
1 change: 1 addition & 0 deletions src/pages/evaluateInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */

/* eslint-disable import/first */

import { Image, Text, Textarea, View } from '@tarojs/components';
import Taro from '@tarojs/taro';
import { useEffect, useRef, useState } from 'react';
Expand Down
40 changes: 14 additions & 26 deletions src/pages/main/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable react-hooks/exhaustive-deps */
/* eslint-disable import/first */
import { Image, ScrollView, Text, View } from '@tarojs/components';

import Taro, { useDidShow } from '@tarojs/taro';
import Taro from '@tarojs/taro';
import { useCallback, useEffect, useMemo, useState } from 'react';

import './index.scss';
Expand Down Expand Up @@ -68,17 +67,18 @@ export default function Index() {
}
}, [classType]);

useDidShow(() => {
void dispatch
.refershComments()
.then(() => {
Taro.hideLoading();
})
.catch(() => {
Taro.hideLoading();
void Taro.showToast({ title: '加载失败', icon: 'none' });
});
});
// useDidShow(() => {
// void Taro.showLoading({ title: '加载中' });
// void dispatch
// .refershComments()
// .then(() => {
// Taro.hideLoading();
// })
// .catch(() => {
// Taro.hideLoading();
// void Taro.showToast({ title: '加载失败', icon: 'none' });
// });
// });

const handleSearch = (searchText: string) => {
console.log('搜索文本:', searchText);
Expand All @@ -90,6 +90,7 @@ export default function Index() {
const res = (await postBool('/checkStatus', {
name: 'kestack',
})) as StatusResponse;

setTest(res.data.status);
} catch (error) {
console.error('Error fetching status:', error);
Expand Down Expand Up @@ -137,19 +138,6 @@ export default function Index() {
<Text className="text-3xl font-semibold tracking-widest text-[#FFD777]">
木犀课栈
</Text>
<View className="mt-8 w-4/5">
<View className="rounded-lg bg-white p-4 shadow-md">
<Text className="mb-4 text-lg font-medium">功能介绍</Text>
<View className="flex flex-col gap-3">
{['查询课程信息', '教师评分反馈'].map((item, index) => (
<View key={index} className="flex items-center">
<View className="mr-2 h-2 w-2 rounded-full bg-[#FFD777]" />
<Text className="text-gray-600">{item}</Text>
</View>
))}
</View>
</View>
</View>
</View>
</View>
) : (
Expand Down
2 changes: 1 addition & 1 deletion src/pages/publishQuestion/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export default function Index() {
<Image src={Icon as string} className="h-full w-full"></Image>
</View>
<Text className="text-3xl font-semibold tracking-widest text-[#FFD777]">
木犀课栈 此功能敬请期待
木犀课栈
</Text>
</View>
</View>
Expand Down

0 comments on commit d6ac23e

Please sign in to comment.