Skip to content

Commit

Permalink
Merge branch 'feature-help-requests-view' of https://github.com/engag…
Browse files Browse the repository at this point in the history
…ingnewsproject/misinfo-dashboard into feature-help-requests-view
  • Loading branch information
EthanL06 committed Oct 29, 2024
2 parents ffe798c + f833565 commit 7b463be
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 44 deletions.
2 changes: 1 addition & 1 deletion components/ReportSystem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const ReportSystem = ({

const defaultTopics = ["Health","Other","Politics","Weather"] // tag system 1
const defaultSources = ["Newspaper", "Other","Social","Website"] // tag system 2
const defaultLabels = ["Important", "Flagged"] // tag system 3
const defaultLabels = ["To Investigate", "Investigated: Flagged", "Investigated: Benign"] // tag system 3

// useEffect(() => {
// console.log('Authenticated user:', user);
Expand Down
76 changes: 50 additions & 26 deletions components/ReportsSection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -433,14 +433,33 @@ const ReportsSection = ({
return Array.from(headersSet) // Convert Set to Array
}

const userIDToEmail = async (userID) => {
const docRef = doc(db, 'mobileUsers', userID)
const docSnap = await getDoc(docRef)
if (docSnap.exists()) {
return docSnap.data().email
} else {
console.log('No such document!')
return ''
}
}

// Convert JSON array to CSV
const convertToCSV = (jsonArray) => {
const convertToCSV = async (jsonArray) => {
const headers = extractHeaders(jsonArray)

// Ensure 'images' is the last header. Multiple images mess up the CSV format
const filteredHeaders = headers.filter((header) => header !== 'images')
// Add 'userEmail' column after 'userID'
const extendedHeaders = filteredHeaders.flatMap((header) =>
header === 'userID' ? ['userID', 'userEmail'] : [header],
)
const finalHeaders = [...extendedHeaders, 'images']

const csvRows = []

// Add headers row
csvRows.push(headers.join(','))
csvRows.push(finalHeaders.join(','))

// Function to convert `createdDate` to ISO 8601 format
const formatDateToISO = (createdDate) => {
Expand All @@ -455,40 +474,45 @@ const ReportsSection = ({
}

// Loop through each report and convert to CSV row
jsonArray.forEach((report) => {
const row = headers.map((header) => {
let value
for (const report of jsonArray) {
const row = await Promise.all(
finalHeaders.map(async (header) => {
let value

// Special case for the `createdDate`
if (header === 'createdDate') {
value = formatDateToISO(report.createdDate)
} else if (header === 'userEmail' && report.userID) {
// Fetch the email for the corresponding userID
value = await userIDToEmail(report.userID)
} else {
const keys = header.split('.')
value = report
keys.forEach((key) => {
value = value[key] !== undefined ? value[key] : '' // Safely access nested values
})

// Special case for the `createdDate`
if (header === 'createdDate') {
value = formatDateToISO(report.createdDate)
} else {
const keys = header.split('.')
value = report
keys.forEach((key) => {
value = value[key] !== undefined ? value[key] : '' // Safely access nested values
})

// Handle commas and newlines in CSV fields
if (typeof value === 'string') {
value = value.replace(/"/g, '""') // Escape double quotes
if (value.includes(',') || value.includes('\n')) {
value = `"${value}"` // Wrap in double quotes if necessary
// Handle commas and newlines in CSV fields
if (typeof value === 'string') {
value = value.replace(/"/g, '""') // Escape double quotes
if (value.includes(',') || value.includes('\n')) {
value = `"${value}"` // Wrap in double quotes if necessary
}
}
}
}

return value
})
return value
}),
)
csvRows.push(row.join(','))
})
}

return csvRows.join('\n')
}

// Trigger download of CSV file
const downloadCSV = () => {
const csvData = convertToCSV(reports)
const downloadCSV = async () => {
const csvData = await convertToCSV(reports)
const blob = new Blob([csvData], { type: 'text/csv;charset=utf-8;' })
const url = URL.createObjectURL(blob)

Expand Down
14 changes: 12 additions & 2 deletions components/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const Settings = () => {
const setData = async(agencyID) => {
const defaultTopics = ["Health","Other","Politics","Weather"] // tag system 1
const defaultSources = ["Newspaper", "Other/Otro","Social","Website"] // tag system 2
const defaultLabels = ["Important", "Flagged"] // tag system 3
const defaultLabels = ["To Investigate", "Investigated: Flagged", "Investigated: Benign"] // tag system 3

// create topics collection for the new agency
setDoc(doc(db, "tags", agencyID), {
Expand Down Expand Up @@ -181,7 +181,7 @@ const Settings = () => {
</div>
}
{agencyID &&
<div>
<div>
<div className="flex justify-between mx-6 my-6 tracking-normal items-center">
<div className="font-light">Topic Tags</div>
<button
Expand All @@ -198,6 +198,16 @@ const Settings = () => {
Edit Sources
</button>
</div>
{customClaims.admin &&
<div className="flex justify-between mx-6 my-6 tracking-normal items-center">
<div className="font-light">Labels</div>
<button
onClick={() => setTagSystem(3)}
className="bg-sky-100 hover:bg-blue-200 text-blue-600 font-normal py-2 px-6 border border-blue-600 rounded-xl">
Edit Labels
</button>
</div>
}
{/* <div className="flex justify-between mx-6 my-6 tracking-normal items-center">
<div className="font-light">Customized Labels</div>
<button
Expand Down
15 changes: 10 additions & 5 deletions components/TagSystem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const TagSystem = ({ tagSystem, setTagSystem, agencyID}) => {

const defaultTopics = ["Health","Other","Politics","Weather"] // tag system 1
const defaultSources = ["Newspaper", "Other","Social","Website"] // tag system 2
const defaultLabels = ["Important", "Flagged"] // tag system 3
const defaultLabels = ["To Investigate", "Investigated: Flagged", "Investigated: Benign"] // tag system 3

const tags = ["Topic", "Source", "Labels"]
//const docRef = getDoc(db, "tags", tagSystem)
Expand Down Expand Up @@ -149,11 +149,16 @@ const TagSystem = ({ tagSystem, setTagSystem, agencyID}) => {

const replaceTag = (tag) => {
list[list.indexOf(selected)] = tag
if (active.includes(selected)) {
active[active.indexOf(selected)] = tag
try {
if (active.includes(selected)) {
active[active.indexOf(selected)] = tag
}
setData(tagSystem,list,active,agencyID)
setSelected("")
} catch (error) {
Sentry.captureException(error)
console.error("Error in replaceTag:", error)
}
setData(tagSystem, list, active, agencyID)
setSelected("")
}

const addNewTag = (tag) => {
Expand Down
4 changes: 2 additions & 2 deletions components/modals/NewReportModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ const NewReportModal = ({
// Create default tags if they don't exist
const defaultTopics = ['Health', 'Other', 'Politics', 'Weather']
const defaultSources = ['Newspaper', 'Other/Otro', 'Social', 'Website']
const defaultLabels = ['Important', 'Flagged']
const defaultLabels = ["To Investigate", "Investigated: Flagged", "Investigated: Benign"]
await setDoc(docRef, {
Labels: { list: defaultLabels, active: defaultLabels },
Expand Down Expand Up @@ -488,7 +488,7 @@ const NewReportModal = ({
console.log('Need to create tag collection for agency. ')
const defaultTopics = ['Health', 'Other', 'Politics', 'Weather'] // tag system 1
const defaultSources = ['Newspaper', 'Other/Otro', 'Social', 'Website'] // tag system 2
const defaultLabels = ['Important', 'Flagged'] // tag system 3
const defaultLabels = ["To Investigate", "Investigated: Flagged", "Investigated: Benign"] // tag system 3

// reference to tags collection
const myDocRef = doc(db, 'tags', selectedAgencyId)
Expand Down
30 changes: 22 additions & 8 deletions components/modals/RenameTagModal.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react'
import { IoClose } from "react-icons/io5"
import * as Sentry from '@sentry/react'

const RenameTagModal = ({ replaceTag, selected, list, setRenameTagModal, addNewTag }) => {
const lower = []
Expand All @@ -9,22 +10,35 @@ const RenameTagModal = ({ replaceTag, selected, list, setRenameTagModal, addNewT
const [tag, setTag] = useState("")

const handleChange = (e) => {
setTag(e.target.value)
try {
setTag(e.target.value)
} catch (error) {
console.error("Error in handleChange:", error)
}
}

const handleAddNewTag = (e) => {
e.preventDefault()
if (tag.length != 0 && tag.length <= 20) {
addNewTag(tag)
setRenameTagModal(false)
try {
if (tag.length != 0 && tag.length <= 20) {
addNewTag(tag)
setRenameTagModal(false)
}
} catch (error) {
console.error("Error in handleAddNewTag:", error)
}
}

const handleReplaceTag = (e) => {
e.preventDefault()
if (!lower.includes(tag.toLowerCase()) && tag.length != 0 && tag.length <= 20) {
replaceTag(tag)
setRenameTagModal(false)
try {
if (!lower.includes(tag.toLowerCase()) && tag.length != 0 && tag.length <= 25) {
replaceTag(tag)
setRenameTagModal(false)
}
} catch (error) {
Sentry.captureException(error)
console.error("Error in handleReplaceTag:", error)
}
}

Expand Down Expand Up @@ -53,7 +67,7 @@ const RenameTagModal = ({ replaceTag, selected, list, setRenameTagModal, addNewT
/>
</div>
{lower.includes(tag.toLowerCase()) && <p className="text-red-500 text-sm font-light">This tag already exists, do you want to replace?</p>}
{tag.length > 20 && <p className="text-red-500 text-sm font-light">You cannot type a tag more than 18 characters long. Please try another name</p>}
{tag.length > 25 && <p className="text-red-500 text-sm font-light">You cannot type a tag more than 18 characters long. Please try another name</p>}
<div className="mt-6 flex justify-between">
<button
onClick={handleReplaceTag}
Expand Down

0 comments on commit 7b463be

Please sign in to comment.