Add obtaining info about current internship status

This commit is contained in:
Kacper Donat 2020-10-07 22:25:55 +02:00
parent 8b2523572d
commit 263be22901
6 changed files with 118 additions and 6 deletions

View File

@ -1,6 +1,18 @@
import { Identifiable, Internship, Mentor } from "@/data";
import { OneWayTransformer } from "@/serialization";
import { Company, Identifiable, Internship, Mentor, Office } from "@/data";
import { momentSerializationTransformer, OneWayTransformer } from "@/serialization";
import { Nullable } from "@/helpers";
import { MentorDTO, mentorDtoTransformer } from "@/api/dto/mentor";
import { InternshipTypeDTO, internshipTypeDtoTransformer } from "@/api/dto/type";
import { Moment } from "moment";
import { sampleStudent } from "@/provider/dummy";
export enum SubmissionState {
Draft,
Submitted,
Accepted,
Rejected,
Archival,
}
export interface InternshipRegistrationUpdateCompany {
id: string,
@ -12,7 +24,21 @@ export interface InternshipRegistrationUpdate {
start: string,
end: string,
type: number,
mentor: Mentor,
mentor: MentorDTO,
}
export interface InternshipRegistrationDTO extends Identifiable {
start: string;
end: string;
type: InternshipTypeDTO,
state: SubmissionState,
mentor: MentorDTO,
company: Company,
branchAddress: Office,
}
export interface InternshipInfoDTO {
internshipRegistration: InternshipRegistrationDTO;
}
export const internshipRegistrationUpdateTransformer: OneWayTransformer<Nullable<Internship>, Nullable<InternshipRegistrationUpdate>> = {
@ -21,7 +47,7 @@ export const internshipRegistrationUpdateTransformer: OneWayTransformer<Nullable
start: subject?.startDate?.toISOString() || null,
end: subject?.endDate?.toISOString() || null,
type: parseInt(subject?.type?.id || "0"),
mentor: subject.mentor,
mentor: mentorDtoTransformer.reverseTransform(subject.mentor as Mentor),
company: {
id: subject?.company?.id as string,
branchOffice: {
@ -31,3 +57,22 @@ export const internshipRegistrationUpdateTransformer: OneWayTransformer<Nullable
}
}
}
export const internshipRegistrationDtoTransformer: OneWayTransformer<InternshipRegistrationDTO, Internship> = {
transform(dto: InternshipRegistrationDTO, context?: unknown): Internship {
return {
id: dto.id,
office: dto.branchAddress,
company: dto.company,
mentor: mentorDtoTransformer.transform(dto.mentor),
startDate: momentSerializationTransformer.reverseTransform(dto.start) as Moment,
endDate: momentSerializationTransformer.reverseTransform(dto.end) as Moment,
type: internshipTypeDtoTransformer.transform(dto.type),
hours: 0,
isAccepted: dto.state === SubmissionState.Accepted,
lengthInWeeks: 0,
program: [],
intern: sampleStudent, // fixme
};
}
}

28
src/api/dto/mentor.ts Normal file
View File

@ -0,0 +1,28 @@
import { Transformer } from "@/serialization";
import { Mentor } from "@/data";
export interface MentorDTO {
firstName: string;
lastName: string;
email: string;
phoneNumber: string;
}
export const mentorDtoTransformer: Transformer<MentorDTO, Mentor> = {
reverseTransform(subject: Mentor, context?: unknown): MentorDTO {
return {
firstName: subject.name,
lastName: subject.surname,
email: subject.email,
phoneNumber: subject.phone || "",
}
},
transform(subject: MentorDTO, context?: unknown): Mentor {
return {
name: subject.firstName,
surname: subject.lastName,
email: subject.email,
phone: subject.phoneNumber,
}
}
}

View File

@ -1,4 +1,4 @@
import { InternshipRegistrationUpdate } from "@/api/dto/internship-registration";
import { InternshipInfoDTO, InternshipRegistrationUpdate } from "@/api/dto/internship-registration";
import { axios } from "@/api/index";
import { Nullable } from "@/helpers";
@ -9,3 +9,11 @@ export async function update(internship: Nullable<InternshipRegistrationUpdate>)
return true;
}
export async function get(): Promise<InternshipInfoDTO> {
const response = await axios.get<InternshipInfoDTO>(INTERNSHIP_ENDPOINT);
console.log(response);
return response.data;
}

View File

@ -1,4 +1,4 @@
import React from "react";
import React, { useEffect } from "react";
import { Page } from "@/pages/base";
import { Container, Stepper, Typography } from "@material-ui/core";
import { Redirect } from "react-router-dom";
@ -14,6 +14,9 @@ import { InsuranceState } from "@/state/reducer/insurance";
import { InsuranceStep } from "@/pages/steps/insurance";
import { StudentStep } from "@/pages/steps/student";
import { useDeadlines } from "@/hooks";
import api from "@/api";
import { InternshipProposalActions, useDispatch } from "@/state/actions";
import { internshipRegistrationDtoTransformer } from "@/api/dto/internship-registration";
export const MainPage = () => {
const { t } = useTranslation();
@ -22,6 +25,19 @@ export const MainPage = () => {
const deadlines = useDeadlines();
const insurance = useSelector<AppState, InsuranceState>(root => root.insurance);
const dispatch = useDispatch();
useEffect(() => {
(async () => {
const internship = await api.internship.get();
dispatch({
type: InternshipProposalActions.Receive,
state: internship.internshipRegistration.state,
internship: internshipRegistrationDtoTransformer.transform(internship.internshipRegistration),
})
})()
}, [])
if (!student) {
return <Redirect to={ route("user_login") }/>;

View File

@ -6,6 +6,7 @@ import {
SaveSubmissionAction,
SendSubmissionAction
} from "@/state/actions/submission";
import { SubmissionState } from "@/api/dto/internship-registration";
export enum InternshipProposalActions {
Send = "SEND_PROPOSAL",
@ -26,6 +27,8 @@ export interface ReceiveProposalDeclineAction extends ReceiveSubmissionDeclineAc
}
export interface ReceiveProposalUpdateAction extends ReceiveSubmissionUpdateAction<InternshipProposalActions.Receive> {
internship: Internship;
state: SubmissionState,
}
export interface SaveProposalAction extends SaveSubmissionAction<InternshipProposalActions.Save> {

View File

@ -11,6 +11,7 @@ import {
} from "@/state/reducer/submission";
import { Reducer } from "react";
import { SubmissionAction } from "@/state/actions/submission";
import { SubmissionState as ApiSubmissionState } from "@/api/dto/internship-registration";
export type InternshipProposalState = SubmissionState & MayRequireDeanApproval & {
proposal: Serializable<Internship> | null;
@ -43,6 +44,17 @@ const internshipProposalReducer = (state: InternshipProposalState = defaultInter
...state,
proposal: internshipSerializationTransformer.transform(action.internship),
}
case InternshipProposalActions.Receive:
return {
...state,
accepted: action.state === ApiSubmissionState.Accepted,
sent: [
ApiSubmissionState.Accepted,
ApiSubmissionState.Rejected,
ApiSubmissionState.Submitted
].includes(action.state),
proposal: internshipSerializationTransformer.transform(action.internship),
}
default:
return state;
}