diff --git a/app/(pages)/about/page.js b/app/(pages)/about/page.js index 6e8fe05..c21b59b 100644 --- a/app/(pages)/about/page.js +++ b/app/(pages)/about/page.js @@ -57,11 +57,11 @@ const People = { ], studentEditors: [ { - name: 'Keshav Arora', + name: 'Sachin Pathak', role: 'Developer and Maintainer', designation: 'UG Student, CSE 3rd year', description: 'None', - profile: 'https://avatars.githubusercontent.com/u/124811079', + profile: 'https://avatars.githubusercontent.com/u/103988614?v=4', links: [ { to: 'https://www.linkedin.com/in/keshav-arora-a5a20325b', icon: 'linkedin' }, { to: 'https://github.com/keshav7104/', icon: 'github' }, diff --git a/components/form/preview-fc.js b/components/form/preview-fc.js index d5b5377..3276c18 100644 --- a/components/form/preview-fc.js +++ b/components/form/preview-fc.js @@ -1,172 +1,253 @@ -import { useEffect, useState } from 'react'; -import MarkdownIcon from '../icons/markdown-icon'; -import SpinnerIcon from '../icons/spinner-icon'; -import NoPreview from './no-preview'; -import PreviewedInput from '../MdInput'; -import styles from './preview.module.scss'; -import cx from 'classnames'; -import Image from 'next/image'; -import useFetchCollection from '@/hooks/fetchCollection'; -import { where } from 'firebase/firestore'; +import { useEffect, useState } from "react"; +import MarkdownIcon from "../icons/markdown-icon"; +import SpinnerIcon from "../icons/spinner-icon"; +import NoPreview from "./no-preview"; +import PreviewedInput from "../MdInput"; +import styles from "./preview.module.scss"; +import cx from "classnames"; +import Image from "next/image"; +import useFetchCollection from "@/hooks/fetchCollection"; +import { where } from "firebase/firestore"; -const PreviewFC = ({ display, category, fields,Formfields, images = [], imgCaption, submit, switchForm, loading = false }) => { - const [desc, setDesc] = useState(''); +const PreviewFC = ({ + display, + category, + fields, + Formfields, + images = [], + imgCaption, + submit, + switchForm, + loading = false, +}) => { + const [desc, setDesc] = useState(""); const [editing, setEditing] = useState(false); const [labels, setLabels] = useState({}); - const {docs:Form,refetch} = useFetchCollection("activity-forms",[where("name","==",category)]); + const { docs: Form, refetch } = useFetchCollection("activity-forms", [ + where("name", "==", category), + ]); - useEffect(()=>{ + useEffect(() => { refetch(); - },[category]); + }, [category]); const getPreviewFields = (fields) => { - if (category === '' || !labels) return fields; + if (category === "" || !labels) return fields; const newFields = {}; - Object.keys(labels).forEach(field => { - if (field === 'activityTitle') return; + Object.keys(labels).forEach((field) => { + if (field === "activityTitle") return; if (!fields[field]) { - newFields[field] = labels[field] ? labels[field].toUpperCase() : ''; - } else if (field === 'pi' || field === 'copi' || field === 'author') { - let outStr = '', sep = ', '; + newFields[field] = labels[field] ? labels[field].toUpperCase() : ""; + } else if (field === "pi" || field === "copi" || field === "author") { + let outStr = "", + sep = ", "; if (fields[field].length) { - if (field === 'pi') { - outStr += fields[field].map((el, i) => { - if (i === fields[field].length - 2) sep = 'and ' - if (i === fields[field].length - 1) sep = '' - return `${el.name}, ${el.designation}, ${el.department} ${sep}` - }).join('') - outStr += fields[field].length > 1 ? ' as Principal Investigators' : ' as a Principal Investigator' - } else if (field === 'copi') { - outStr += fields[field].map((el, i) => { - if (i === fields[field].length - 2) sep = 'and ' - if (i === fields[field].length - 1) sep = '' - return `${el.name}, ${el.designation}, ${el.department} ${el.insName ? ', ' + el.insName : ''}${sep}` - }).join('') - outStr += ' and ' + fields[field].length > 1 ? ' as Co-Principal Investigators' : ' as a Co-Principal Investigator' - } else if (field === 'author') { - outStr += fields[field].map((el, i) => { - if (i === fields[field].length - 2) sep = 'and ' - if (i === fields[field].length - 1) sep = '' - return `${el.lastName}, ${el.firstInitials.toUpperCase()}.` - }).join('') + if (field === "pi") { + outStr += fields[field] + .map((el, i) => { + if (i === fields[field].length - 2) sep = "and "; + if (i === fields[field].length - 1) sep = ""; + return `${el.name}, ${el.designation}, ${el.department} ${sep}`; + }) + .join(""); + outStr += + fields[field].length > 1 + ? " as Principal Investigators" + : " as a Principal Investigator"; + } else if (field === "copi") { + outStr += fields[field] + .map((el, i) => { + if (i === fields[field].length - 2) sep = "and "; + if (i === fields[field].length - 1) sep = ""; + return `${el.name}, ${el.designation}, ${el.department} ${ + el.insName ? ", " + el.insName : "" + }${sep}`; + }) + .join(""); + outStr += + " and " + fields[field].length > 1 + ? " as Co-Principal Investigators" + : " as a Co-Principal Investigator"; + } else if (field === "author") { + outStr += fields[field] + .map((el, i) => { + if (i === fields[field].length - 2) sep = "and "; + if (i === fields[field].length - 1) sep = ""; + return `${el.lastName}, ${el.firstInitials.toUpperCase()}.`; + }) + .join(""); } } newFields[field] = outStr ? outStr : labels[field].toUpperCase(); } else { newFields[field] = fields[field]; } - }) + }); return newFields; - } + }; - const replaceKeysWithValues=(oldFields , newFields , str)=> { + const replaceKeysWithValues = (oldFields, newFields, str) => { // Iterate through props object - for (const key in oldFields ) { - if (oldFields .hasOwnProperty(key) && newFields .hasOwnProperty(key)) { - // Create a regular expression to match the key within the string - const regex = new RegExp(oldFields [key], 'g'); - // Replace all occurrences of the key with its value - str = str.replace(regex, newFields [key]); - // oldFields[key]=newFields[key]; - } + for (const key in oldFields) { + if (oldFields.hasOwnProperty(key) && newFields.hasOwnProperty(key)) { + // Create a regular expression to match the key within the string + const regex = new RegExp(oldFields[key], "g"); + // Replace all occurrences of the key with its value + str = str.replace(regex, newFields[key]); + // oldFields[key]=newFields[key]; + } } return str; -} + }; - const updatePreview = () => { - if (Form[category]===undefined) setDesc(''); + const updatePreview = (reset = false) => { + if (Form[category] === undefined) setDesc(""); else { - const oldFields = {} - Form[category].fields.forEach(item=>{ - oldFields[item.name]=item.placeholder; - }) + const oldFields = {}; + Form[category].fields.forEach((item) => { + oldFields[item.name] = item.placeholder; + }); const newFields = getPreviewFields(fields); - // console.log(oldFields,newFields); - setDesc(replaceKeysWithValues(oldFields,newFields,Form[category].preview)); + // console.log(oldFields, newFields); + if (reset === true) { + setDesc( + replaceKeysWithValues(newFields, oldFields, Form[category].preview) + ); + } else { + setDesc( + replaceKeysWithValues(oldFields, newFields, Form[category].preview) + ); + } } - } + }; const handleSubmit = (event) => { - if (document.getElementById('activityForm').checkValidity()) { + if (document.getElementById("activityForm").checkValidity()) { event.preventDefault(); submit(desc); } else { switchForm(true); } - } + }; useEffect(() => { let labels = {}; - Formfields.filter(field => ( - field.type !== 'sectionHeading' - && field.type !== 'person' - && field.type !== 'file' - && field.type !== 'radio' - )).forEach(field => { - if (field.type === 'dateRange') { - labels = { ...labels, date: field.from.placeholder, toDate: field.to.placeholder } + Formfields.filter( + (field) => + field.type !== "sectionHeading" && + field.type !== "person" && + field.type !== "file" && + field.type !== "radio" + ).forEach((field) => { + if (field.type === "dateRange") { + labels = { + ...labels, + date: field.from.placeholder, + toDate: field.to.placeholder, + }; } else { - labels = { ...labels, [field.name]: field.required || field.type === 'list' ? field.placeholder : '' } + labels = { + ...labels, + [field.name]: + field.required || field.type === "list" ? field.placeholder : "", + }; } - }) + }); setLabels(labels); }, [Formfields]); useEffect(() => { updatePreview(); // eslint-disable-next-line - }, [fields,Form]) + }, [fields, Form]); return ( -
- {category === '' ? : (<> -
-
-

{fields.activityTitle ? fields.activityTitle : category!=='' ? category : Untitled}

- { setDesc(txt) }} - editing={status => { setEditing(status) }} - /> +
+ {category === "" ? ( + + ) : ( + <> +
+
+

+ {fields.activityTitle ? ( + fields.activityTitle + ) : category !== "" ? ( + category + ) : ( + Untitled + )} +

+ { + setDesc(txt); + }} + editing={(status) => { + setEditing(status); + }} + /> +
+
- -
-
- {images.map((img, i) => ( -
- -
- ))} -
- {images.length !== 0 && (category !== 1 || category !== 3) && ( -

{imgCaption}

- )} -
- - -
- )} +
+ {images.map((img, i) => ( +
+ +
+ ))} +
+ {images.length !== 0 && (category !== 1 || category !== 3) && ( +

{imgCaption}

+ )} +
+ + +
+ + )}
- ) -} + ); +}; export default PreviewFC;