From 10257ac7cffecf5034a19050a18b6f0ff432d6da Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Thu, 12 Nov 2020 20:16:09 +0100 Subject: [PATCH] Allow file downloading --- src/api/internship.ts | 13 +++++++------ src/api/upload.ts | 6 ++++++ src/components/fileinfo.tsx | 2 +- src/forms/internship.tsx | 2 +- translations/pl.yaml | 3 +++ 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/api/internship.ts b/src/api/internship.ts index ba09dfa..b17c190 100644 --- a/src/api/internship.ts +++ b/src/api/internship.ts @@ -21,20 +21,21 @@ export class ValidationError extends Error { } } +interface ApiError { + key: string; + parameters: { [name: string]: string }, +} + interface UpdateResponse { status: SubmissionState; - errors?: string[]; + errors?: ApiError[]; } export async function update(internship: Nullable): Promise { const response = (await axios.put(INTERNSHIP_REGISTRATION_ENDPOINT, internship)).data; if (response.status == SubmissionState.Draft) { - throw new ValidationError( - response.errors?.map( - msg => ({ key: msg, parameters: {} }) - ) || [] - ); + throw new ValidationError(response.errors || []); } return response.status; diff --git a/src/api/upload.ts b/src/api/upload.ts index 4657fce..43aab7a 100644 --- a/src/api/upload.ts +++ b/src/api/upload.ts @@ -2,6 +2,7 @@ import { axios } from "@/api/index"; import { InternshipDocument } from "@/api/dto/internship-registration"; import { prepare } from "@/routing"; import { Identifiable } from "@/data"; +import store from "@/state/store"; export enum UploadType { Ipp = "IppScan", @@ -17,6 +18,7 @@ export interface DocumentFileInfo extends Identifiable { const CREATE_DOCUMENT_ENDPOINT = '/document'; const DOCUMENT_UPLOAD_ENDPOINT = '/document/:id/scan'; +const DOCUMENT_DOWNLOAD_ENDPOINT = 'document/:id/scan/download'; export async function create(type: UploadType) { const response = await axios.post(CREATE_DOCUMENT_ENDPOINT, { type }); @@ -35,3 +37,7 @@ export async function fileinfo(document: InternshipDocument): Promise(prepare(DOCUMENT_UPLOAD_ENDPOINT, { id: document.id as string })); return response.data; } + +export function link(document: InternshipDocument): string { + return axios.defaults.baseURL + prepare(DOCUMENT_DOWNLOAD_ENDPOINT, { id: document.id as string }) + "?auth=" + store.getState().user.token; +} diff --git a/src/components/fileinfo.tsx b/src/components/fileinfo.tsx index 544baee..6989f91 100644 --- a/src/components/fileinfo.tsx +++ b/src/components/fileinfo.tsx @@ -84,7 +84,7 @@ export const FileInfo = ({ document, ...props }: FileInfoProps) => { - + } diff --git a/src/forms/internship.tsx b/src/forms/internship.tsx index f5bfe04..c5d83f0 100644 --- a/src/forms/internship.tsx +++ b/src/forms/internship.tsx @@ -387,7 +387,7 @@ export const InternshipForm: React.FunctionComponent = () => { { errors.length > 0 && { t('internship.validation.has-errors') }
    - { errors.map(message =>
  • { t(`internship.validation.${message.key}`, message.parameters) }
  • ) } + { errors.map(message =>
  • { t(`validation.api.${message.key}`, message.parameters) }
  • ) }
} { t('internship.sections.intern-info') } diff --git a/translations/pl.yaml b/translations/pl.yaml index 32e762e..819cb77 100644 --- a/translations/pl.yaml +++ b/translations/pl.yaml @@ -212,6 +212,9 @@ steps: Należy zgłosić się do pełnomocnika ds. praktyk Twojego kierunku i podpisać umowę ubezpieczenia. (TODO) validation: + api: + GreaterThanOrEqualValidator: Wartość pola "{{ PropertyName }}" musi być większa bądź równa {{ ComparisonValue }}. + NotEmptyValidator: Wartosć pola "{{ PropertyName }}" nie może być pusta. required: "To pole jest wymagane" email: "Wprowadź poprawny adres e-mail" phone: "Wprowadź poprawny numer telefonu"