Add proposal reducer

This commit is contained in:
Kacper Donat 2020-07-30 22:02:23 +02:00 committed by Gitea
parent 1f127323a7
commit 45bf07a28b
6 changed files with 104 additions and 3 deletions

3
src/data/deanApproval.ts Normal file
View File

@ -0,0 +1,3 @@
export type DeanApproval = {
}

View File

@ -1 +1,5 @@
export * from './company'
export * from './edition'
export * from './student'
export * from './internship'
export * from './helpers'

View File

@ -1,6 +1,7 @@
import { StudentAction, StudentActions } from "@/state/actions/student";
import { EditionAction, EditionActions } from "@/state/actions/edition";
import { SettingActions, SettingsAction } from "@/state/actions/settings";
import { InternshipProposalAction, InternshipProposalActions } from "@/state/actions/proposal";
import { Dispatch } from "react";
import { useDispatch as useReduxDispatch } from "react-redux";
@ -9,10 +10,11 @@ export * from "./base"
export * from "./edition"
export * from "./settings"
export * from "./student"
export * from "./proposal"
export type Action = StudentAction | EditionAction | SettingsAction;
export type Action = StudentAction | EditionAction | SettingsAction | InternshipProposalAction;
export const Actions = { ...StudentActions, ...EditionActions, ...SettingActions }
export const Actions = { ...StudentActions, ...EditionActions, ...SettingActions, ...InternshipProposalActions }
export type Actions = typeof Actions;
export const useDispatch = () => useReduxDispatch<Dispatch<Action>>()

View File

@ -0,0 +1,37 @@
import { Action } from "@/state/actions/base";
import { Internship } from "@/data";
export enum InternshipProposalActions {
Send = "SEND_PROPOSAL",
Save = "SAVE_PROPOSAL",
Approve = "RECEIVE_PROPOSAL_APPROVE",
Decline = "RECEIVE_PROPOSAL_DECLINE",
Receive = "RECEIVE_PROPOSAL_STATE",
}
export interface SendProposalAction extends Action<InternshipProposalActions.Send> {
internship: Internship;
}
export interface ReceiveProposalApproveAction extends Action<InternshipProposalActions.Approve> {
}
export interface ReceiveProposalDeclineAction extends Action<InternshipProposalActions.Decline> {
}
export interface ReceiveProposalUpdateAction extends Action<InternshipProposalActions.Receive> {
}
export interface SaveProposalAction extends Action<InternshipProposalActions.Save> {
internship: Internship;
}
export type InternshipProposalAction
= SendProposalAction
| SaveProposalAction
| ReceiveProposalApproveAction
| ReceiveProposalDeclineAction
| ReceiveProposalUpdateAction;

View File

@ -1,13 +1,15 @@
import { combineReducers } from "redux";
import studentReducer from "./student"
import studentReducer from "@/state/reducer/student"
import editionReducer from "@/state/reducer/edition";
import settingsReducer from "@/state/reducer/settings";
import internshipProposalReducer from "@/state/reducer/proposal";
const rootReducer = combineReducers({
student: studentReducer,
edition: editionReducer,
settings: settingsReducer,
proposal: internshipProposalReducer,
})
export type AppState = ReturnType<typeof rootReducer>;

View File

@ -0,0 +1,53 @@
import { DeanApproval } from "@/data/deanApproval";
import { InternshipProposalAction, InternshipProposalActions } from "@/state/actions";
import { Internship } from "@/data";
export type ProposalStatus = "draft" | "awaiting" | "accepted" | "declined";
export type InternshipProposalState = {
accepted: boolean;
sent: boolean;
declined: boolean;
requiredDeanApprovals: DeanApproval[];
proposal: Internship | null;
comment: string | null;
}
const defaultInternshipProposalState: InternshipProposalState = {
accepted: false,
declined: false,
proposal: null,
requiredDeanApprovals: [],
sent: false,
comment: null
}
const getInternshipProposalStatus = ({ accepted, declined, sent }: InternshipProposalState): ProposalStatus => {
switch (true) {
case !sent:
return "draft";
case sent && accepted:
return "accepted"
case sent && declined:
return "declined"
case sent && (!accepted && !declined):
return "awaiting"
default:
throw new Error("Invalid proposal state " + JSON.stringify({ accepted, declined, sent }));
}
}
const internshipProposalReducer = (state: InternshipProposalState = defaultInternshipProposalState, action: InternshipProposalAction): InternshipProposalState => {
switch (action.type) {
case InternshipProposalActions.Save:
case InternshipProposalActions.Send:
return {
...state,
proposal: action.internship
}
default:
return state;
}
}
export default internshipProposalReducer;