generated from EyeSeeTea/dhis2-app-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upload temp image to file resouces until save
- Loading branch information
Showing
15 changed files
with
302 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { D2Api } from "$/types/d2-api"; | ||
import { apiToFuture, FutureData } from "$/data/api-futures"; | ||
import { FileResourcesRepository } from "$/domain/repositories/FileResourcesRepository"; | ||
import { FileResource } from "$/domain/entities/FileResource"; | ||
import { Id } from "$/domain/entities/Ref"; | ||
import { Future } from "$/domain/entities/generic/Future"; | ||
|
||
export class FileResourcesD2Repository implements FileResourcesRepository { | ||
constructor(private api: D2Api) {} | ||
|
||
public save(fileResource: FileResource): FutureData<FileResource> { | ||
const formData = new FormData(); | ||
formData.append("file", fileResource.data, fileResource.name); | ||
formData.append("contentType", fileResource.data.type); | ||
formData.append("domain", fileResource.domain); | ||
console.log("formData", formData); | ||
|
||
return apiToFuture( | ||
this.api.request<FileResponse>({ | ||
url: "/fileResources", | ||
method: "post", | ||
requestBodyType: "raw", | ||
data: formData, | ||
}) | ||
).flatMap(response => { | ||
console.log("response", response); | ||
|
||
const id = response.response?.fileResource?.id; | ||
if (!id) { | ||
return Future.error(new Error("Failed to save file resource")); | ||
} else { | ||
const savedFileResource: FileResource = { ...fileResource, id: id }; | ||
return Future.success(savedFileResource); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
type FileResponse = { | ||
response?: { | ||
fileResource?: { | ||
id?: Id; | ||
}; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Future } from "$/domain/entities/generic/Future"; | ||
import { FutureData } from "$/data/api-futures"; | ||
import { FileResourcesRepository } from "$/domain/repositories/FileResourcesRepository"; | ||
import { FileResource } from "$/domain/entities/FileResource"; | ||
|
||
export class FileResourcesTestRepository implements FileResourcesRepository { | ||
public save(fileResource: FileResource): FutureData<FileResource> { | ||
return Future.success({ | ||
...fileResource, | ||
id: "test-id", | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Id } from "$/domain/entities/Ref"; | ||
|
||
export type FileResource = { | ||
id: Id; | ||
name: string; | ||
data: Blob; | ||
domain: "DATA_VALUE"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { FutureData } from "$/data/api-futures"; | ||
import { FileResource } from "$/domain/entities/FileResource"; | ||
|
||
export interface FileResourcesRepository { | ||
save(fileResource: FileResource): FutureData<FileResource>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { FutureData } from "$/data/api-futures"; | ||
import { FileResource } from "$/domain/entities/FileResource"; | ||
import { FileResourcesRepository } from "$/domain/repositories/FileResourcesRepository"; | ||
|
||
export class SaveFileResourceUseCase { | ||
constructor(private fileResourcesRepository: FileResourcesRepository) {} | ||
|
||
public execute(fileResource: FileResource): FutureData<FileResource> { | ||
return this.fileResourcesRepository.save(fileResource); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Backdrop, CircularProgress } from "@material-ui/core"; | ||
import React from "react"; | ||
import styled from "styled-components"; | ||
|
||
export const Loader: React.FC = () => ( | ||
<StyledBackdrop open={true}> | ||
<StyledLoaderContainer> | ||
<CircularProgress color="primary" size={50} /> | ||
</StyledLoaderContainer> | ||
</StyledBackdrop> | ||
); | ||
|
||
const StyledLoaderContainer = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
`; | ||
|
||
const StyledBackdrop = styled(Backdrop)` | ||
color: ${props => props.theme.palette.common.white}; | ||
z-index: 2; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from "react"; | ||
import { Backdrop, CircularProgress } from "@material-ui/core"; | ||
|
||
interface LoaderContainerProps { | ||
loading: boolean; | ||
children: React.ReactNode; | ||
} | ||
|
||
const LoaderContainer: React.FC<LoaderContainerProps> = ({ loading, children }) => { | ||
return ( | ||
<div style={{ position: "relative" }}> | ||
<Backdrop style={{ position: "absolute", zIndex: 1 }} open={loading} invisible={false}> | ||
<CircularProgress color="primary" /> | ||
</Backdrop> | ||
<div style={{ opacity: loading ? 0.5 : 1 }}>{children}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default LoaderContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.