Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/create test update #18

Merged
merged 6 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: テスト作成機能完了
  • Loading branch information
pusin819 committed Jul 10, 2024
commit 4cf759f06f01fd08becb65ec307ee8ec3fe9253a
13 changes: 10 additions & 3 deletions src/app/api/testAPIs.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"use server"

import {Question, Test, Section, SubSection, Prisma} from "@prisma/client";
import {Class, Prisma, Question, Section, SubSection, Test} from "@prisma/client";
import {prisma} from "@/app/api/prisma_client"

export interface TestFrame {
test: Test,
sections: SectionFrame[],
classes: Class[],
}

export interface SectionFrame {
Expand Down Expand Up @@ -52,6 +53,13 @@ export const createTest = async (props: TestFrame) => {
}
}
})
},
classes: {
connect: props.classes.map(i => {
return {
id: i.id
}
})
}
}
console.log(JSON.stringify(test.sections));
Expand Down Expand Up @@ -134,7 +142,7 @@ export const getTest = async () => {

// no info about sections ...
export const getTestByClass = async (classId: number) => {
const tests = await prisma.test.findMany({
return prisma.test.findMany({
where: {
classes: {
some: {
Expand All @@ -143,5 +151,4 @@ export const getTestByClass = async (classId: number) => {
}
}
});
return tests;
}
54 changes: 30 additions & 24 deletions src/app/test/createTest/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ export default function Page() {
const [startDate, setStartDate] = useState<Dayjs>(dayjs())
const [asignedClass, setAsignedClass] = useState<Class[]>([])

const [classList, setClassList] = useState<Class[]>([])
console.log(asignedClass)
useEffect(() => {
const fetchClasses = async () => {
// TODO: teacherIdを取得
const classes: Class[] = await getAllClass()
setClassList(classes)
}
fetchClasses()
}, [])

const handleSectionChange = (item: SectionFrame, index: number) => {
const newS: SectionFrame[] = sections.map((s: SectionFrame, i: number) => {
if (i === index) {
Expand Down Expand Up @@ -78,7 +89,7 @@ export default function Page() {
startDate: startDate.toDate(),
endDate: endDate.toDate()
}
const newTestFrame: TestFrame = {test: newTest, sections: sections}
const newTestFrame: TestFrame = {test: newTest, sections: sections, classes: asignedClass}
await createTest(newTestFrame)
}

Expand Down Expand Up @@ -113,15 +124,17 @@ export default function Page() {
}

// クラスの割り当て用
const handleClassChange = (event: any) => {
const {
target: {value},
} = event;
setAsignedClass(
typeof value === 'string' ? value.split(',') : value,
);
};
const handleClassChange = (event: React.ChangeEvent<{ value: unknown }>) => {
const values = event.target.value as number[];
const select = classList.filter(item => values.includes(item.id))

setAsignedClass(select);
};
// const handleChange = (event: React.ChangeEvent<{ value: unknown }>) => {
// const selectedIds = event.target.value as number[];
// const selectedObjects = options.filter(option => selectedIds.includes(option.id));
// setSelectedItems(selectedObjects);
// };
return (
<Stack gap={2} justifyContent={"center"} display={"flex"} marginX={"5vw"}>
<Button variant={"contained"} onClick={createTestButtonFunction} /*disabled={checkDataError()}*/ >Create
Expand Down Expand Up @@ -163,6 +176,7 @@ export default function Page() {
setEndDate={setEndDate}
asignedClass={asignedClass}
handleClassChange={handleClassChange}
classList={classList}
/>
</TabPanels>
{sections.map((s: SectionFrame, index: number) => (
Expand Down Expand Up @@ -212,17 +226,8 @@ const MetaDataPage = ({
setEndDate,
asignedClass,
handleClassChange,
classList,
}: any) => {
const [classList, setClassList] = useState<Class[]>([])
useEffect(() => {
const fetchClasses = async () => {
// TODO: teacherIdを取得
const classes: Class[] = await getAllClass()
setClassList(classes)
}
fetchClasses()
console.log(classList)
}, [])
const dateWarning = () => {
const isBeforeWarning = () => {
if (startDate.isBefore(dayjs())) {
Expand All @@ -247,22 +252,23 @@ const MetaDataPage = ({
labelId={"ClassAssign"}
id={"ClassAssign"}
multiple
value={asignedClass}
value={asignedClass.map((option: Class) => option.id)}
input={<OutlinedInput label="Class"/>}
onChange={handleClassChange}
renderValue={(selected) => (
<Box sx={{display: 'flex', flexWrap: 'wrap', gap: 0.5}}>
{selected.map((value: string) => (
<Chip key={value} label={value}/>
))}
{(selected as number[]).map((value: number) => {
const item = classList.find((option: Class) => option.id === value);
return item ? <Chip key={value} label={item.name}/> : null;
})}
</Box>
)}
>
{
classList.map((c: Class) => (
<MenuItem
key={c.id}
value={c.name}
value={c.id}
>
{c.name}
</MenuItem>
Expand Down