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

Assign and create survey components #35

Merged
merged 22 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6445061
debugging test page
ctgchris Apr 15, 2024
ccce1d7
finish UI for assign and create survey
ctgchris Apr 25, 2024
c9c3c51
Merge branch 'main' of https://github.com/Hack4Impact-UMD/swaliga-fou…
ctgchris Apr 25, 2024
c1010db
unfinished functionality + testpage
ctgchris Apr 25, 2024
3093152
api calls implementation
ctgchris Apr 28, 2024
f71b860
Merge branch 'main' into assign-create-surveys
ctgchris Apr 28, 2024
f857e37
fixed create component
nkanchinadam Apr 28, 2024
3e4e4d1
fixing assign implementation
ctgchris Apr 28, 2024
26f3e3d
Merge branch 'assign-create-surveys' of https://github.com/Hack4Impac…
ctgchris Apr 28, 2024
85ba50f
revert code
ctgchris Apr 28, 2024
c7de632
fixed Survey type in Assign component
nkanchinadam Apr 28, 2024
b337f43
error with assign
ctgchris Apr 28, 2024
5ec8130
unwrapped returned data from /api/surveys route
nkanchinadam Apr 29, 2024
b9cbde4
cleaned up Assign component
nkanchinadam Apr 29, 2024
fb1a52a
got rid of one of the title input fields
nkanchinadam Apr 29, 2024
e054fc4
cleaned up CSS for Assign & Create components
nkanchinadam Apr 29, 2024
2e7b758
resolved icons
nkanchinadam Apr 29, 2024
eb49038
removed outline from Create component input field
nkanchinadam Apr 29, 2024
d17b4ab
removed outline when input field is selected
nkanchinadam Apr 30, 2024
6eee39e
Merge branch 'main' into assign-create-surveys
nkanchinadam Apr 30, 2024
a6e7ea8
updated package-lock.json
nkanchinadam Apr 30, 2024
c3bf58a
deleted testpage
nkanchinadam Apr 30, 2024
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
628 changes: 172 additions & 456 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/app/api/surveys/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createSurvey, getAllSurveys } from "@/lib/firebase/database/surveys";
export async function GET() {
try {
const allSurveys = await getAllSurveys();
return NextResponse.json({ surveys: allSurveys }, { status: 200 });
return NextResponse.json(allSurveys, { status: 200 });
} catch (error) {
return NextResponse.json({ error: 'error getting all surveys' }, { status: 500 })
}
Expand All @@ -14,7 +14,7 @@ export async function POST(req: NextRequest) {
const body = await req.json();
try {
const survey = await createSurvey(body);
return NextResponse.json({data: survey}, {status: 200});
return NextResponse.json(survey, {status: 200});
} catch (err) {
return NextResponse.json({error: 'error creating survey'}, {status: 500});
}
Expand Down
105 changes: 105 additions & 0 deletions src/components/Assign.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"use client";

import React, { useState, useEffect } from 'react';
import { Survey } from '@/types/survey-types';
import styles from './Assign.module.css';

interface AssignProps {
userIds: string[];
}

export default function Assign({ userIds }: AssignProps) {
const [selectedSurveys, setSelectedSurveys] = useState<string[]>([]);
const [surveys, setSurveys] = useState<Survey[]>([]);

useEffect(() => {
// Fetch the list of surveys from the server when the component mounts
async function fetchSurveys() {
try {
const response = await fetch('/api/surveys');
if (response.ok) {
const surveysArray: Survey[] = await response.json();
setSurveys(surveysArray);
} else {
console.error('Failed to fetch surveys:', response.statusText);
}
} catch (error) {
console.error('Error occurred while fetching surveys:', error);
}
}

fetchSurveys();
}, []);

// Function to toggle the selection of a survey
const toggleSurvey = (surveyId: string) => {
const isSelected = selectedSurveys.includes(surveyId);
setSelectedSurveys(prevSelected => {
if (isSelected) {
return prevSelected.filter(id => id !== surveyId);
} else {
return [...prevSelected, surveyId];
}
});
};

// Function to handle assigning surveys
const assignSurveys = async () => {
if (selectedSurveys.length === 0) {
return;
}

try {
console.log(userIds)
console.log(selectedSurveys)
const response = await fetch('/api/surveys/assign', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
userIds: userIds,
surveyIds: selectedSurveys
})
});

if (response.ok) {
const responseData = await response.json();
console.log(responseData.message);
} else {
const errorData = await response.json();
console.error(errorData.error);
}
} catch (error) {
console.error('Error occurred while assigning surveys:', error);
}
};

return (
<div className={styles.container}>
<div className={styles.title}>Assign Surveys</div>

{surveys.map((survey) => (
<div key={survey.formId} className={styles.centeredOval}>
<input
type="checkbox"
id={`surveyCheckbox_${survey.formId}`}
className={styles.inputCheckbox}
checked={selectedSurveys.includes(survey.formId)}
onChange={() => toggleSurvey(survey.formId)}
/>
<label
htmlFor={`surveyCheckbox_${survey.formId}`}
className={styles.text}
>
{survey.info.title}
</label>
</div>
))}
<button className={styles.button} onClick={assignSurveys}>
Assign
</button>
<span className={styles.closeIcon}/>
</div>
);
}
75 changes: 75 additions & 0 deletions src/components/assign.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
.container {
display: flex;
flex-direction: column;
align-items: center;

background: #d9d9d9;
padding: 50px 30px 30px 30px;
border-radius: 20px;
/*width: 15%; */
width: 350px;
position: relative;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

.inputGroup {

display: flex;
align-items: center;
margin-bottom: 10px;
}

.button {
width: 105px;
margin-top: 20px;
padding: 4px 0px;
background-color: white;
color: black;
border-radius: 10px;
cursor: pointer;
font-size: 18px;
}

.closeIcon {
position: absolute;
top: 10px;
right: 10px;
width: 24px;
height: 24px;
cursor: pointer;
background-image: url('/images/close-icon.png');
background-size: cover;
}

.centeredOval {
display: flex;
align-items: center;
justify-content: center;
width: 350px;
height: 30px;
border-radius: 50px;
font-size: 16px;
color: black;
background-color: white;
margin-bottom: 10px;
position: relative;

}
.title {
margin-bottom: 20px;
color: black;
font-size: 20px;
}

.inputCheckbox {
transform: scale(1.3);
position: absolute;
left: 10px;


}
.inputCheckbox:checked {
accent-color: black;
}


73 changes: 73 additions & 0 deletions src/components/create.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
.container {
display: flex;
flex-direction: column;
align-items: center;

background: #d9d9d9;
padding: 50px 30px 30px 30px;
border-radius: 20px;
width: 330px;
position: relative;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

.inputGroup {

display: flex;
align-items: center;
margin-bottom: 10px;
}

.button {
background-color: white;
color: black;
border-radius: 10px;
cursor: pointer;
width: 120px;
margin-top: 15px;
padding: 4px 0px;
font-size: 16px;
}

.closeIcon {
position: absolute;
background-image: url('/images/close-icon.png');
background-size: cover;
top: 10px;
right: 10px;
width: 24px;
height: 24px;
cursor: pointer;
}

.centeredOval {
display: flex;
align-items: center;
justify-content: center;
width: 350px;
height: 40px;
border-radius: 50px;
font-size: 16px;
color: black;
background-color: white;
margin-bottom: 10px;
position: relative;

}
.title {
margin-bottom: 20px;
color: black;
font-size: 20px;
}

.inputField {
flex-grow: 1;
border-radius: 15px;
padding: 0px 20px;
font-size:16px;
border: none;
}

.inputField:focus {
outline: none;
}
49 changes: 49 additions & 0 deletions src/components/create.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use client";

import React, { useState } from 'react';
import styles from './Create.module.css';

export default function Create() {
const [surveyTitle, setSurveyTitle] = useState("");

const createSurvey = async () => {
try {
const response = await fetch('/api/surveys', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
documentTitle: surveyTitle,
title: surveyTitle
})
});

if (response.ok) {
} else {
console.error('Failed to create survey:', response.statusText);
}
} catch (error) {
console.error('Error occurred while creating survey:', error);
}
};

return (
<div className={styles.container}>
<div className={styles.title}>Create Survey</div>
<div className={styles.centeredOval}>
<input
name="surveyTitle"
placeholder="Survey Title"
className={styles.inputField}
value={surveyTitle}
onChange={(ev) => setSurveyTitle(ev.target.value)}
/>
</div>
<button className={styles.button} onClick={createSurvey}>
Create
</button>
<span className={styles.closeIcon}/>
</div>
);
}
Loading