Skip to content

Instantly share code, notes, and snippets.

@dayhaysoos
Created October 6, 2021 15:52
Show Gist options
  • Save dayhaysoos/39a1896c8d64a585ff6a96141ffc2460 to your computer and use it in GitHub Desktop.
Save dayhaysoos/39a1896c8d64a585ff6a96141ffc2460 to your computer and use it in GitHub Desktop.
import { useReducer } from 'react'
import { Storage, API } from 'aws-amplify'
import {
createCandidate,
deleteCandidate,
updateCandidate
} from '../graphql/mutations'
import { getCandidateByUsername } from '../graphql/queries'
import { v4 as uuid } from 'uuid'
import config from '../aws-exports'
import { useAuth } from '../context/auth'
const {
aws_user_files_s3_bucket_region: region,
aws_user_files_s3_bucket: bucket
} = config
function candidateReducer(state, action) {
switch (action.type) {
case 'error': {
return {
...state,
status: 'rejected',
error: action.error
}
}
case 'success': {
return {
...state,
status: 'resolved',
data: action.result,
error: null
}
}
case 'start': {
return {
...state,
status: 'pending'
}
}
default: {
throw new Error(`Unhandled action type: ${action.type}`)
}
}
}
function useCandidate() {
const [state, dispatch] = useReducer(candidateReducer, {
status: 'idle',
data: null,
error: null
})
const { auth, user } = useAuth()
async function createCandidateEntry(data) {
if (data.resume.length === 0) {
dispatch({ type: 'error', error: 'Resume is required' })
alert('Resume is required')
return
}
for (const key in data) {
if (data[key] === '' || data[key]?.length === 0) {
delete data[key]
} else if (data[key] === 'Yes') {
data[key] = true
} else if (data[key] === 'No') {
data[key] = false
}
}
const file = data.resume[0]
const { type: mimeType } = file
const key = `resume/${uuid()}/${file.name}`
const url = `https://${bucket}.s3.${region}.amazonaws.com/public/${key}`
const fileForUpload = { bucket, region, key }
dispatch({ type: 'start' })
try {
await Storage.put(key, file, {
contentType: mimeType
})
const result = await API.graphql({
query: createCandidate,
variables: { input: { ...data, resume: fileForUpload } }
})
await auth.updateUserAttributes(user, {
'custom:profile_status': 'submitted'
})
dispatch({ type: 'success', result })
} catch (error) {
console.log(error)
dispatch({ type: 'error', error })
}
}
async function getCandidate(username) {
console.log('lol', username)
try {
const result = await API.graphql({
query: getCandidateByUsername,
variables: { username }
})
console.log('result.data', result.data)
dispatch({
type: 'success',
result: result.data.getCandidateByUsername.items[0]
})
} catch (error) {
console.log(error)
dispatch({ type: 'error', error })
}
}
async function updateCandidateEntry(data) {
if (data.resume.length === 0) {
throw new Error(`Resume is required if you didn't upload one already`)
}
for (const key in data) {
if (data[key] === '' || data[key]?.length === 0) {
delete data[key]
} else if (data[key] === 'Yes') {
data[key] = true
} else if (data[key] === 'No') {
data[key] = false
}
}
if (data.resume.length === 1) {
const file = data.resume[0]
const { type: mimeType } = file
const key = `resume/${uuid()}/${file.name}`
const url = `https://${bucket}.s3.${region}.amazonaws.com/public/${key}`
const fileForUpload = { bucket, region, key }
try {
await Storage.put(key, file, {
contentType: mimeType
})
await API.graphql({
query: updateCandidate,
variables: { input: { ...data, resume: fileForUpload } }
})
await auth.updateUserAttributes(user, {
'custom:profile_status': 'submitted'
})
} catch (error) {
console.log('Error', error)
dispatch({ type: 'error', error })
}
} else {
try {
await API.graphql({
query: updateCandidate,
variables: { input: { ...data } }
})
await auth.updateUserAttributes(user, {
'custom:profile_status': 'submitted'
})
} catch (error) {
console.log('Error', error)
dispatch({ type: 'error', error })
}
}
}
async function deleteCandidateEntry(data) {
const { id } = data
try {
API.graphql({
query: deleteCandidate,
variables: { input: { id } }
})
await auth.updateUserAttributes(user, {
'custom:profile_status': ''
})
} catch (error) {
console.log('Error:', error)
dispatch({ type: 'error', error })
}
}
return {
status: state.status,
candidateData: state.data,
error: state.error,
getState: () => state,
createCandidateEntry,
getCandidate,
updateCandidateEntry,
deleteCandidateEntry
}
}
export default useCandidate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment