Skip to content

Commit

Permalink
inputting pollen data spreadsheet
Browse files Browse the repository at this point in the history
- form to input file & click preview (generating preview is seperate task)

- e2e tests for valid spreadsheet, invalid inputs and no input
  • Loading branch information
arnard76 committed Apr 27, 2024
1 parent 12ce4b0 commit a9dfc66
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
41 changes: 41 additions & 0 deletions cypress/e2e/cms/pollenData.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { URLS } from "../consts"
import { loginAttempt, mockValidCredentials, userIsLoggedIn } from "../authenticationUtils"

describe("pollen data", () => {
describe("inputting excel file", () => {
beforeEach(() => {
cy.visit(URLS.EDIT_POLLEN_DATA)
mockValidCredentials()
loginAttempt()
userIsLoggedIn()
cy.visit(URLS.EDIT_POLLEN_DATA) // TODO: login should already redirect here!!!!
})

it("valid excel file", () => {
cy.contains("label", "Upload .xlsx spreadsheet file with pollen data")
.find("input[type='file']")
.selectFile("cypress/fixtures/AAPC dummy data.xlsx")

cy.contains("button", "Preview data").click()
cy.contains("Preview generated ✅")
})

it("no file", () => {
cy.contains("label", "Upload .xlsx spreadsheet file with pollen data").find("input[type='file']")

cy.contains("button", "Preview data").click()
cy.contains("No file uploaded")
})

it("invalid file", () => {
cy.contains("label", "Upload .xlsx spreadsheet file with pollen data")
.find("input[type='file']")
.selectFile("cypress/fixtures/authSuccessResponse.json")

cy.contains("button", "Preview data").click()
cy.contains(
"The file you have uploaded doesn't seem to be an Excel spreadsheet. The filename 'authSuccessResponse.json' doesn't have '.xlsx'"
)
})
})
})
Binary file added cypress/fixtures/AAPC dummy data.xlsx
Binary file not shown.
50 changes: 48 additions & 2 deletions frontend/app/(cms)/pollen/edit/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
"use client"

import React from "react"
import React, { useEffect, useState, useRef } from "react"

export default function EditPollen() {
return <>Page for editing pollen data 🙂📉📊</>
const [errorMessage, setErrorMessage] = useState(null as string | null)
const fileInputReference = useRef(null)
const [validInputFile, setValidInputFile] = useState(null as null | File)

useEffect(() => {
if (errorMessage) setValidInputFile(null)
}, [errorMessage])

function validateInputFile(e: React.MouseEvent<HTMLButtonElement, MouseEvent>) {
e.preventDefault()

if (!fileInputReference.current) return

const fileInputElement: HTMLInputElement = fileInputReference.current

if (!fileInputElement.files?.length || !fileInputElement.files[0]) return setErrorMessage("No file uploaded")

const uploadedFile = fileInputElement.files[0]

console.log({ uploadedFile })

if (!uploadedFile.name.includes(".xlsx"))
return setErrorMessage(
`The file you have uploaded doesn't seem to be an Excel spreadsheet. The filename '${uploadedFile.name}' doesn't have '.xlsx'`
)

setErrorMessage(null)
setValidInputFile(uploadedFile)
}
return (
<>
<form>
<label>
<span>Upload .xlsx spreadsheet file with pollen data</span>
<input type="file" ref={fileInputReference} />
</label>

<button type="submit" onClick={validateInputFile}>
Preview data
</button>
<button type="submit">Save data</button>
{errorMessage && <p className="form-error">{errorMessage}</p>}
</form>

{validInputFile && <div>Preview generated ✅</div>}
</>
)
}
2 changes: 2 additions & 0 deletions frontend/app/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const ROUTES = {
FORGOT_PASSWORD: "/forgot-password",

REDIRECT_AFTER_LOGIN: "/",

EDIT_POLLEN_DATA: "/pollen/edit",
}

export const OPEN_AUTH_ROUTES = [ROUTES.LOGIN, ROUTES.FORGOT_PASSWORD] // no auth required for these ones

0 comments on commit a9dfc66

Please sign in to comment.