Skip to content

Commit

Permalink
3214 - redux: update repository item
Browse files Browse the repository at this point in the history
  • Loading branch information
sorja committed Feb 7, 2024
1 parent 054c723 commit 0a18292
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions src/client/store/ui/repository/actions/upsertRepositoryItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,43 @@ type Props = CycleParams & {
repositoryItem: RepositoryItem
}

export const upsertRepositoryItem = createAsyncThunk<RepositoryItem, Props, ThunkApiConfig>(
'repositoryItem/upsert',
async (props) => {
const { file, repositoryItem } = props
const { assessmentName, cycleName, countryIso } = props
const formData = new FormData()
formData.append('name', repositoryItem?.name)
formData.append('link', repositoryItem?.link)
const updateRepositoryItem = async (props: Props) => {
const { file, repositoryItem } = props
const { assessmentName, cycleName, countryIso } = props
const formData = new FormData()
formData.append('repositoryItem', JSON.stringify(repositoryItem))

if (file) formData.append('file', file)

const params = { countryIso, assessmentName, cycleName }
const config = { params }

const { data } = await axios.patch(ApiEndPoint.CycleData.Repository.one(), formData, config)
return data
}

if (file) formData.append('file', file)
const createRepositoryItem = async (props: Props): Promise<RepositoryItem> => {
const { file, repositoryItem } = props
const { assessmentName, cycleName, countryIso } = props
const formData = new FormData()
formData.append('name', repositoryItem?.name)
formData.append('link', repositoryItem?.link)

const params = { countryIso, assessmentName, cycleName }
const config = { params }
const { data } = await axios.post(ApiEndPoint.CycleData.Repository.one(), formData, config)
if (file) formData.append('file', file)

return data
const params = { countryIso, assessmentName, cycleName }
const config = { params }

const { data } = await axios.post(ApiEndPoint.CycleData.Repository.one(), formData, config)
return data
}

export const upsertRepositoryItem = createAsyncThunk<RepositoryItem, Props, ThunkApiConfig>(
'repositoryItem/upsert',
async (props) => {
const repositoryItem = props.repositoryItem.uuid
? await updateRepositoryItem(props)
: await createRepositoryItem(props)
return repositoryItem
}
)

0 comments on commit 0a18292

Please sign in to comment.