Skip to content

Commit

Permalink
feat: seems good (#1559)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlr authored Oct 7, 2024
1 parent b6ccc7f commit a9fd623
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
17 changes: 10 additions & 7 deletions ui/components/ItemDetail/CandidatureLba/services/getSchema.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import * as Yup from "yup"

import { sessionStorageGet } from "@/utils/localStorage"

import { phoneValidation } from "../../../../common/validation/fieldValidations"

export function getInitialSchemaValues() {
const inSessionValue = JSON.parse(sessionStorageGet("application-form-values"))
return {
firstName: "",
lastName: "",
email: "",
phone: "",
fileName: "",
fileContent: null,
message: "",
firstName: inSessionValue?.applicant_first_name ?? "",
lastName: inSessionValue?.applicant_last_name ?? "",
email: inSessionValue?.applicant_email ?? "",
phone: inSessionValue?.applicant_phone ?? "",
fileName: inSessionValue?.applicant_file_name ?? "",
fileContent: inSessionValue?.applicant_file_content ?? "",
message: inSessionValue?.message ?? "",
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LBA_ITEM_TYPE_OLD } from "shared/constants/lbaitem"

import { getItemId } from "@/utils/getItemId"
import { localStorageSet } from "@/utils/localStorage"
import { localStorageSet, sessionStorageSet } from "@/utils/localStorage"

import { apiPost } from "../../../../utils/api.utils"

Expand All @@ -18,21 +18,25 @@ export default async function submitCandidature({
}) {
setSendingState("currently_sending")

const payload = {
const applicationPayload = {
applicant_first_name: formValues.firstName,
applicant_last_name: formValues.lastName,
applicant_email: formValues.email,
applicant_phone: formValues.phone,
message: formValues.message,
applicant_file_name: formValues.fileName,
applicant_file_content: formValues.fileContent,
}
const payload = {
...applicationPayload,
company_siret: LbaJob.ideaType === LBA_ITEM_TYPE_OLD.LBA ? LbaJob.company?.siret : undefined, // either company_siret or job_id
job_id: LbaJob.ideaType === LBA_ITEM_TYPE_OLD.MATCHA ? LbaJob.job?.id : undefined, // either company_siret or job_id
caller,
}

try {
await apiPost("/_private/application", { body: payload, headers: { authorization: `Bearer ${LbaJob.token}` } }, {}, "V2")
sessionStorageSet("application-form-values", applicationPayload)
localStorageSet(`application-${LbaJob.ideaType}-${getItemId(LbaJob)}`, Date.now().toString())
setSendingState("ok_sent")
return true
Expand Down
32 changes: 24 additions & 8 deletions ui/utils/localStorage.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
export function localStorageSet<T>(key: string, value: T) {
export function storageSet<T>(storage: Storage, key: string, value: T) {
try {
if (window?.localStorage) {
if (storage) {
const serializedValue = typeof value === "string" ? value : JSON.stringify(value)
window.localStorage.setItem(key, serializedValue)
storage.setItem(key, serializedValue)
return true
}
return false
} catch (error) {
console.error(`Error setting localStorage key "${key}":`, error)
console.error(`Error setting storage key "${key}":`, error)
return false
}
}

export function localStorageGet(key: string): string | null {
export function storageGet(storage: Storage, key: string): string | null {
try {
if (window?.localStorage) {
return window.localStorage.getItem(key)
if (storage) {
return storage.getItem(key)
}
return null
} catch (error) {
console.error(`Error getting localStorage key "${key}":`, error)
console.error(`Error getting storage key "${key}":`, error)
return null
}
}

export function localStorageSet<T>(key: string, value: T) {
return storageSet(window?.localStorage, key, value)
}

export function localStorageGet(key: string): string | null {
return storageGet(window?.localStorage, key)
}

export function sessionStorageSet<T>(key: string, value: T) {
return storageSet(window?.sessionStorage, key, value)
}

export function sessionStorageGet(key: string): any | null {
return storageGet(window?.sessionStorage, key)
}

0 comments on commit a9fd623

Please sign in to comment.