Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix campaign file upload #95

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions back/api/espace/controllers/espace.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,22 @@ module.exports = {

let entity;
if (ctx.is("multipart")) {
console.log(parseMultipartData(ctx))
const { data, files, campaign_files } = parseMultipartData(ctx);
entity = await strapi.services.espace.update({ id }, data, {
files,
campaign_files
});
entity = await strapi.services.espace.update({ id }, data, {files, campaign_files});
} else {
const { files, ...body } = ctx.request.body;
if (files && files.length > 0) {
await Promise.all(
files.map((file) => {
strapi.plugins["upload"].services.upload.updateFileInfo(file.id, {
caption: file.caption,
});
})
);
}
const { files,campaign_files, ...body } = ctx.request.body;
await Promise.all([files, campaign_files].map(async(fileList)=>{
if (fileList && fileList.length > 0) {
await Promise.all(
fileList.map(async(file) => {
await strapi.plugins["upload"].services.upload.updateFileInfo(file.id, {
caption: file.caption,
});
})
);
}
}))

entity = await strapi.services.espace.update({ id }, body);
}

Expand Down
2 changes: 1 addition & 1 deletion back/config/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = ({ env }) => ({
},
},
upload: {
provider: "do",
provider: env('NODE_ENV') === 'development' ? 'local' : "do",
providerOptions: {
key: process.env.DO_SPACE_ACCESS_KEY,
secret: process.env.DO_SPACE_SECRET_KEY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
},
"x-generation-date": "02/27/2024 4:32:17 PM"
"x-generation-date": "02/28/2024 10:57:52 AM"
},
"x-strapi-config": {
"path": "/documentation",
Expand Down
31 changes: 22 additions & 9 deletions web/components/Campaign/Places/Admin/CampaignFileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@ import { Espace } from '~typings/api'
import Check from 'public/assets/img/check.svg'
import { client } from '~api/client-api'
import { addFilesWithInfo } from '~utils/file'
import { useQueryClient } from 'react-query'

const CampaignFileUpload = ({ place }: { place: Espace }) => {
const { t } = useTranslation('place')
const [isLoading, setLoading] = useState(false)
const { errorToast, successToast } = useToast()
const queryClient = useQueryClient()

const submitForm = async (values) => {
const submitForm = async ({
removedCampaign_files,
campaign_files,
...values
}) => {
setLoading(true)
let createdFiles = null
if (values.removedCampaign_files.length > 0) {
await Promise.all(
values.removedCampaign_files.map(client.upload.filesDelete),
)
if (removedCampaign_files.length > 0) {
await Promise.all(removedCampaign_files.map(client.upload.filesDelete))
}

const newFiles = values?.campaign_files?.filter((file) => !file.id)
const newFiles = campaign_files.filter((file) => !file.id)

if (newFiles.length > 0) {
createdFiles = await Promise.all(
addFilesWithInfo(newFiles, {
Expand All @@ -35,9 +40,14 @@ const CampaignFileUpload = ({ place }: { place: Espace }) => {
}

try {
await client.espaces.espacesUpdate(place.id, {
const res = await client.espaces.espacesUpdate(place.id, {
...values,
files: values.campaign_files.filter((file) => file.id),
campaign_files: campaign_files.filter((file) => file.id),
})
queryClient.setQueryData(['place', place.slug], res.data)
reset({
campaign_files: res.data.campaign_files,
removedCampaign_files: [],
})
successToast(t('campaignSchedule.files_success'))
} catch (e) {
Expand All @@ -47,7 +57,10 @@ const CampaignFileUpload = ({ place }: { place: Espace }) => {
}

const methods = useForm({
defaultValues: {},
defaultValues: {
removedCampaign_files: [],
campaign_files: [],
},
mode: 'onChange',
})

Expand Down
4 changes: 4 additions & 0 deletions web/components/Campaign/Places/Admin/CampaignScheduleInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ScheduleDelete from '~components/Account/Place/ScheduleDelete'
import Link from '~components/Link'
import { ROUTE_ACCOUNT_APPLICATIONS } from '~constants'
import useCampaignDispo from '~hooks/useCampaignDispo'
import Schedule from '~components/Account/Place/Schedule'

interface Props {
place: Espace
Expand Down Expand Up @@ -100,6 +101,9 @@ const CampaignScheduleInfo = ({ place, showForm }: Props) => {
{t('campaign.helpers.applications.schedule.see_applications')}
</Button>
) : null}
<Box display={{ base: 'block', lg: 'none' }} width="100%">
<Schedule isCampaignMode />
</Box>
</VStack>
)
}
Expand Down
46 changes: 24 additions & 22 deletions web/components/Campaign/Places/Detail/CampaignPlaceFiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@ const CampaignPlaceFiles = ({ place }: { place: Espace }) => {
return (
<VStack width="100%" spacing={6} paddingY={4}>
<Divider />
<VStack alignItems="flex-start">
{place?.campaign_files.map((file) => (
<Button
key={file.id}
mb={4}
mr={4}
leftIcon={<Download />}
colorScheme="gray"
fontSize="md"
onClick={() => {
axios({
url: file.url,
method: 'GET',
responseType: 'blob',
}).then(() => {
saveAs(file.url, file.name)
})
}}
>
{file.caption ? `${file.caption} (${file.ext})` : file.name}
</Button>
))}
<VStack alignItems="flex-start" width="100%">
{place?.campaign_files.map((file) => {
return (
<Button
key={file.id}
mb={4}
mr={4}
leftIcon={<Download />}
colorScheme="gray"
fontSize="md"
onClick={() => {
axios({
url: file.url,
method: 'GET',
responseType: 'blob',
}).then(() => {
saveAs(file.url, file.name)
})
}}
>
{file.caption ? `${file.caption} (${file.ext})` : file.name}
</Button>
)
})}
</VStack>
</VStack>
)
Expand Down
1 change: 0 additions & 1 deletion web/components/InputFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Box,
Spacer,
useBreakpointValue,
Circle,
} from '@chakra-ui/react'
import Add from 'public/assets/img/add-circle.svg'
import Attachment from 'public/assets/img/attachment.svg'
Expand Down
Loading