Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.

Commit 401803a

Browse files
committed
fix upload story
1 parent c7c80e2 commit 401803a

File tree

3 files changed

+44
-29
lines changed

3 files changed

+44
-29
lines changed

components/search/resultList.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { useSearchTeamKatalogen } from '../../lib/rest/teamkatalogen'
99
import { useGetProductAreas } from '../../lib/rest/productAreas'
1010
import { SearchResult } from '../../lib/rest/search'
1111
import { deleteStory } from '../../lib/rest/stories'
12+
import { Router, useRouter } from 'next/router'
1213

1314
const Results = ({ children }: { children: React.ReactNode }) => (
1415
<div className="results">{children}</div>
@@ -61,6 +62,7 @@ const ResultList = ({
6162
const userInfo = useContext(UserState)
6263
const { searchResult: teamkatalogen } = useSearchTeamKatalogen()
6364
const { productAreas } = useGetProductAreas()
65+
const router = useRouter()
6466

6567
const isDataProduct = (item: any) => !!item.datasets
6668

@@ -191,7 +193,7 @@ const ResultList = ({
191193
keywords={s.keywords}
192194
editable={true}
193195
description={s.description}
194-
deleteResource={()=> deleteStory(s.id)}
196+
deleteResource={()=> deleteStory(s.id).then(()=>router.reload())}
195197
/>
196198
))}
197199
</Results>

components/stories/newStory.tsx

+26-26
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,21 @@ export const NewStoryForm = () => {
6464
} = useForm({
6565
resolver: yupResolver(schema),
6666
defaultValues: {
67-
name: undefined,
68-
description: '',
69-
teamkatalogenURL: '',
70-
keywords: [] as string[],
67+
name: undefined,
68+
description: '',
69+
teamkatalogenURL: '',
70+
keywords: [] as string[],
7171
},
7272
})
7373

7474
const { errors } = formState
7575
const keywords = watch('keywords')
7676

7777
const onDeleteKeyword = (keyword: string) => {
78-
keywords !== undefined ?
79-
setValue('keywords', keywords.filter((k: string) => k !== keyword))
80-
:
81-
setValue('keywords', [])
78+
keywords !== undefined ?
79+
setValue('keywords', keywords.filter((k: string) => k !== keyword))
80+
:
81+
setValue('keywords', [])
8282
}
8383

8484
const onAddKeyword = (keyword: string) => {
@@ -90,25 +90,25 @@ export const NewStoryForm = () => {
9090
const valueOrNull = (val: string) => (val == '' ? null : val);
9191

9292
const onSubmit = async (data: any) => {
93-
const files= storyFiles.map<UploadFile>(it=>({
94-
path: fixRelativePath(it),
95-
file: it,
96-
}))
97-
const storyInput= {
98-
name: data.name,
99-
description: valueOrNull(data.description),
100-
keywords: data.keywords,
101-
teamkatalogenURL: data.teamkatalogenURL,
102-
productAreaID: productAreaID,
103-
teamID: teamID,
104-
group: data.group,
105-
}
93+
const files = storyFiles.map<UploadFile>(it => ({
94+
path: fixRelativePath(it),
95+
file: it,
96+
}))
97+
const storyInput = {
98+
name: data.name,
99+
description: valueOrNull(data.description),
100+
keywords: data.keywords,
101+
teamkatalogenURL: data.teamkatalogenURL,
102+
productAreaID: productAreaID,
103+
teamID: teamID,
104+
group: data.group,
105+
}
106106

107107
try {
108108
const data = await createStory(storyInput, files);
109109
setError(undefined);
110110
amplitudeLog('skjema fullført', { skjemanavn: 'ny-datafortelling' });
111-
router.push(`/story/${data.id}`);
111+
router.push(`/user/stories`);
112112
} catch (e) {
113113
setError(e as Error);
114114
amplitudeLog('skjemainnsending feilet', {
@@ -117,7 +117,7 @@ export const NewStoryForm = () => {
117117
console.log(e)
118118
}
119119
}
120-
120+
121121

122122
const onCancel = () => {
123123
amplitudeLog(
@@ -147,10 +147,10 @@ export const NewStoryForm = () => {
147147
}
148148
}
149149

150-
const fixRelativePath = (file: File) =>{
150+
const fixRelativePath = (file: File) => {
151151
var path = file.webkitRelativePath
152152
var pathParts = path.split('/');
153-
return pathParts.length <= 1? file.name:pathParts.slice(1).reduce((p, s, i)=>i===0? s: p+ "/" +s)
153+
return pathParts.length <= 1 ? file.name : pathParts.slice(1).reduce((p, s, i) => i === 0 ? s : p + "/" + s)
154154
}
155155

156156
const generateFileTree = (files: File[]) => {
@@ -159,7 +159,7 @@ export const NewStoryForm = () => {
159159
var pathParts = file.webkitRelativePath.split('/');
160160
if (pathParts.length === 1) {
161161
pathParts = [file.name]
162-
}else{
162+
} else {
163163
pathParts = pathParts.slice(1)
164164
}
165165
let currentLevel = tree;

lib/rest/stories.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,26 @@ import { createStoryUrl, deleteTemplate, postTemplate, putTemplate, updateStoryU
33
import { isThenable } from "next/dist/client/components/router-reducer/router-reducer-types"
44

55
export const createStory = (newStory: any, files: any[]) => {
6+
console.log(newStory, files)
67
const formData = new FormData()
78
files.forEach((file, idx) => {
9+
console.log(file, idx)
810
formData.append(`files[${idx}][path]`, file.path)
9-
formData.append(`files[${idx}[file]`, file.file)
11+
formData.append(`files[${idx}][file]`, file.file)
1012
})
1113
formData.append('story', JSON.stringify(newStory))
12-
return postTemplate(createStoryUrl(), formData).then((res) => res.json())
14+
console.log(formData)
15+
console.log(formData.get('files[0][file]'))
16+
return fetch(createStoryUrl(), {
17+
method: 'POST',
18+
credentials: 'include',
19+
body: formData,
20+
}).then(res => {
21+
if (!res.ok) {
22+
throw new Error(res.statusText)
23+
}
24+
return res.json()
25+
})
1326
}
1427

1528
export const updateStory =(storyId: string, updatedStory: any) => {

0 commit comments

Comments
 (0)