Skip to content

Commit

Permalink
Integrate with question API to get topics
Browse files Browse the repository at this point in the history
  • Loading branch information
linxiaoxin committed Aug 2, 2024
1 parent 65fa371 commit a62792a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
25 changes: 12 additions & 13 deletions app/(main)/questions/topics/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { TreeTable } from 'primereact/treetable';
import { Column } from 'primereact/column';
import { InputText } from 'primereact/inputtext';
import { Button } from 'primereact/button';
import { ToggleButton } from 'primereact/togglebutton';
import { Toolbar } from 'primereact/toolbar';
import { Dialog } from 'primereact/dialog';
import { SelectButton } from 'primereact/selectbutton';

const ManageTopics = () => {
const [topicNodes, setTopicNodes] = useState<any[]>([]);
Expand All @@ -34,11 +34,11 @@ const ManageTopics = () => {
if(item.skills){
childnode = item.skills.map( skill => {
return { "key": item.id+"-"+skill.id,
"data": { "id": skill.id,"name": skill.name , "active": skill.active, "type": "skill", "edited": skill.edited, "topicId": item.id}
"data": { "id": skill.id,"name": skill.name , "status": skill.status, "type": "skill", "edited": skill.edited, "topicId": item.id}
};
});
}
return { "key" : item.id, "data" : { "id":item.id, "name": item.name, "active": item.active, "type": "topic", "edited": item.edited}, children : childnode }
return { "key" : item.id, "data" : { "id":item.id, "name": item.name, "status": item.status, "type": "topic", "edited": item.edited}, children : childnode }
});
setTopicNodes(nodes);

Expand All @@ -52,11 +52,10 @@ const ManageTopics = () => {
</div>
);
};
const activeTemplate = (node: any) => {
const statusTemplate = (node: any) => {
return(<>
{!node.data.id ||
<ToggleButton onLabel="Active" offLabel="Inactive"
checked={node.data.active} onChange={(e) => onActiveStatusChange(node, e.target.value)}/>
<SelectButton value={node.data.status} onChange={(e) => onStatusChange(node, e.target.value)} options={['ACTIVE','INACTIVE']}/>
}
{node.data.id != undefined ||
<Button label='Undo' icon="pi pi-undo" onClick={(e)=> undoAddSkill(node)}/>
Expand All @@ -65,18 +64,18 @@ const ManageTopics = () => {
);
}
//edit active status
const onActiveStatusChange = (node: any, value: boolean) => {
const onStatusChange = (node: any, value: string) => {
var found = false;
let updatedTopics = topics.map( (t)=> {
if(node.data.type === 'topic' && t.id === node.key){
t.active = value;
t.status = value;
t.edited = true;
found = true;
}
else if(node.data.type === 'skill' && t.skills){
t.skills.map((s) => {
if(s.id === node.data.id){
s.active = value;
s.status = value;
s.edited = true;
found = true;
}
Expand Down Expand Up @@ -125,7 +124,7 @@ const ManageTopics = () => {
found = true;
let newSkill: Questions.Skill = {
name: "",
active: true,
status: "ACTIVE",
edited: true,
topicId: node.key
}
Expand Down Expand Up @@ -172,7 +171,7 @@ const ManageTopics = () => {
setAddTopic(false);
let newTopic: Questions.Topic = {
name: newTopicName,
active: true,
status: "ACTIVE",
skills: [],
edited: true
}
Expand Down Expand Up @@ -223,8 +222,8 @@ const ManageTopics = () => {
expandedKeys={expandedKeys} onToggle={(e) => setExpandedKeys(e.value)} >
<Column field="name" header="Name" expander
editor={nameEditor} style={{ height: '3.5rem' }} className="w-20rem"/>
<Column body={activeTemplate} field="active" header="Active" className="w-4rem"/>
<Column body={actionTemplate} className="w-4rem" />
<Column body={statusTemplate} field="status" header="Status" className="w-16rem"/>
<Column body={actionTemplate} className="w-8rem" />
</TreeTable>
</div>
</div>
Expand Down
30 changes: 15 additions & 15 deletions public/demo/data/topics.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,83 @@
"topics": [{
"id": 1,
"name": "Chemical Bonding",
"active": true,
"status": "ACTIVE",
"skills": [
{
"id": 1,
"active": true,
"status": "ACTIVE",
"name": "Simple molecular structure"
},
{
"id": 2,
"active": true,
"status": "ACTIVE",
"name": "Intermolecular forces"
},
{
"id": 3,
"active": true,
"status": "ACTIVE",
"name": "Types of structure"
},
{
"id": 4,
"active": true,
"status": "ACTIVE",
"name": "Comparing physical property"
}
]
},
{
"id": 2,
"name": "Atomic Structure",
"active": true,
"status": "ACTIVE",
"skills": [
{
"id": 5,
"active": true,
"status": "ACTIVE",
"name": "Atomic and proton number, mass and nucleon number"
},
{
"id": 6,
"active": true,
"status": "ACTIVE",
"name": "Distribution of mass and charge"
}

]
},
{
"id": 3,
"active": true,
"status": "ACTIVE",
"name": "State of matter"
},
{
"id": 4,
"active": true,
"status": "ACTIVE",
"name": "Atoms, molecules and stoichiometry"
},
{
"id": 5,
"active": true,
"status": "ACTIVE",
"name": "Chemical energetics"
}
],
"skills": [{
"id": 1,
"name": "Simple molecular structure",
"active": true,
"status": "ACTIVE",
"topic_id": 1
},{
"id": 2,
"name": "Intermolecular forces",
"active": true,
"status": "ACTIVE",
"topic_id": 1
},{
"id": 3,
"name": "Types of structure",
"active": true,
"status": "ACTIVE",
"topic_id": 1
},{
"id": 4,
"name": "Comparing physical property",
"active": true,
"status": "ACTIVE",
"topic_id": 1
}]
}
8 changes: 7 additions & 1 deletion service/QuestionsService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Questions } from '@/types';
const QuesionsSvcUrl = process.env.NEXT_PUBLIC_QUEMISTRY_QUESTIONS_URL || ''
const retrieveQuestionUrl = `${process.env.NEXT_PUBLIC_QUEMISTRY_GATEWAY_URL}/questions/retrieve`
const saveQuestionUrl = `${process.env.NEXT_PUBLIC_QUEMISTRY_GATEWAY_URL}/questions`
const QuestionsTopicsUrl = `${process.env.NEXT_PUBLIC_QUEMISTRY_QUESTIONS_URL}/topics`

export const QuestionsService = {
addMCQ(data : any) {
Expand Down Expand Up @@ -69,7 +70,12 @@ export const QuestionsService = {
// );
// },
getTopics() {
return fetch('/demo/data/topics.json', { headers: { 'Cache-Control': 'no-cache' } })
return fetch(QuestionsTopicsUrl, {
headers: {
'Content-Type': 'application/json'
},
credentials: 'include'
})
.then((res) => res.json())
.then((d) => d.topics as Questions.Topic[]);
},
Expand Down
4 changes: 2 additions & 2 deletions types/questions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ declare namespace Questions {
interface Topic{
id?: number;
name: string;
active: boolean;
status: string;
skills: Skill[];
edited: boolean = false; //false by default (only used by UI)
}

interface Skill{
id?: number;
name: string;
active: boolean;
status: string;
topicId: number;
edited: boolean = false; //false by default (only used by UI)
}
Expand Down

0 comments on commit a62792a

Please sign in to comment.