Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Employee from "../interfaces/employee"
- import { uniqWith } from "lodash"
- import Message from "../interfaces/message"
- import Product from "../interfaces/product"
- import ScannedLot from "../interfaces/scanned-lot"
- import gid from "../utils/gid"
- import { fetchEmployee, fetchProds } from "../utils/simple-fetches"
- export const CLEAR_INPUT_DELAY = 150
- export const PARSE_INPUT_DELAY = 100
- export const HIDE_DURATION = 2000
- const SCAN_LOTS_MSG = "Отсканируйте лоты"
- const SCAN_YOUR_ID_MSG = "Отсканируйте свой ID"
- export enum Types {
- Login,
- StartLoading,
- StopLoading,
- RemoveLot,
- SetErrorLot,
- SetMessage,
- FinallyLotSaving,
- SetLot,
- SetInput,
- ClearErrorLots,
- FinishLotSaving,
- }
- type Payload = {
- [Types.Login]: { employee: Employee; employeeAID: string }
- [Types.StartLoading]: null
- [Types.StopLoading]: null
- [Types.RemoveLot]: ScannedLot
- [Types.SetErrorLot]: { error: string; lot: ScannedLot }
- [Types.SetMessage]: Message
- [Types.FinallyLotSaving]: null
- [Types.SetLot]: {
- productAID: string
- lotnum: number
- order: string
- product: Product
- qty: number
- orderQty: number
- }
- [Types.SetInput]: string
- [Types.ClearErrorLots]: null
- [Types.FinishLotSaving]: null
- }
- type ActionMap<M> = {
- [Key in keyof M]: M[Key] extends null
- ? {
- type: Key
- }
- : {
- type: Key
- payload: M[Key]
- }
- }
- export type Actions = ActionMap<Payload>[keyof ActionMap<Payload>]
- export interface State {
- input: string
- placeholder: string
- currentEmployee: Employee
- scannedLots: ScannedLot[]
- showBackdrop: boolean
- message: Message
- showMessage: boolean
- loading: boolean
- }
- export const initialState: State = {
- input: "",
- placeholder: SCAN_YOUR_ID_MSG,
- currentEmployee: null,
- scannedLots: [],
- showBackdrop: false,
- message: null,
- showMessage: false,
- loading: false,
- }
- export const reducer = (state: State, action: Actions): State => {
- switch (action.type) {
- case Types.Login:
- return action.payload.employee
- ? {
- ...state,
- currentEmployee: action.payload.employee,
- placeholder: SCAN_LOTS_MSG,
- scannedLots: [],
- }
- : {
- ...state,
- placeholder: SCAN_YOUR_ID_MSG,
- currentEmployee: null,
- showMessage: true,
- message: {
- message: `Сотрудник ${action.payload.employeeAID} не найден`,
- error: true,
- },
- scannedLots: [],
- }
- case Types.SetInput:
- return { ...state, input: action.payload }
- case Types.StartLoading:
- return { ...state, loading: true }
- case Types.StopLoading:
- return { ...state, loading: false }
- case Types.RemoveLot:
- const scannedLots = state.scannedLots.filter(
- l => gid(l) !== gid(action.payload)
- )
- return {
- ...state,
- scannedLots,
- placeholder:
- scannedLots.length === 0 ? SCAN_LOTS_MSG : state.placeholder,
- }
- case Types.SetErrorLot:
- return {
- ...state,
- scannedLots: state.scannedLots.map(l =>
- gid(l) === gid(action.payload.lot)
- ? {
- ...l,
- error: action.payload.error,
- }
- : l
- ),
- }
- case Types.SetMessage:
- return {
- ...state,
- message: action.payload,
- showMessage: !!action.payload.message,
- }
- case Types.FinishLotSaving:
- return {
- ...state,
- message: state.scannedLots.some(l => l.error)
- ? {
- message: "Не удалось сохранить один или несколько лотов",
- error: true,
- }
- : {
- message: "Все лоты успешно сохранены",
- },
- }
- case Types.FinallyLotSaving:
- return {
- ...state,
- loading: false,
- showMessage: true,
- placeholder: SCAN_LOTS_MSG,
- }
- case Types.SetLot:
- return action.payload.product
- ? {
- ...state,
- placeholder: "Отсканируйте рабочий центр или другие лоты",
- scannedLots: uniqWith(
- [
- ...state.scannedLots,
- {
- lotnum: action.payload.lotnum,
- product: Number(action.payload.product.id),
- productCode: action.payload.product.code,
- order: action.payload.order,
- productAID: action.payload.productAID,
- qty: Number(action.payload.qty),
- orderQty: action.payload.orderQty,
- },
- ],
- (a, b) => gid(a) === gid(b)
- ),
- }
- : {
- ...state,
- message: {
- message: `Продукт ${action.payload.productAID} не найден`,
- error: true,
- },
- }
- case Types.ClearErrorLots:
- return { ...state, scannedLots: state.scannedLots.filter(l => !l.error) }
- default:
- return state
- }
- }
- export interface LotRow {
- productAID: string
- lotnum: number
- order: string
- qty: number
- orderQty: number
- }
- export const setLotRow = async (row: LotRow, dispatch) => {
- dispatch({ type: Types.StartLoading })
- const {
- data: { products },
- } = await fetchProds(row.productAID)
- dispatch({ type: Types.StopLoading })
- dispatch({
- type: Types.SetLot,
- payload: {
- product: products[0],
- ...row,
- },
- })
- }
- export const login = async (employeeAID: string, dispatch) => {
- dispatch({ type: Types.StartLoading })
- const {
- data: { employees },
- } = await fetchEmployee(employeeAID)
- dispatch({ type: Types.StopLoading })
- dispatch({
- type: Types.Login,
- payload: { employeeAID, employee: employees[0] },
- })
- }
- export interface SaveLotsProps {
- workingCenterAID: string
- handleLot: (
- incompleteLot: ScannedLot,
- workingCenterAID: string
- ) => Promise<void>
- lots: ScannedLot[]
- dispatch: any
- }
- export const saveLots = async (props: SaveLotsProps) => {
- try {
- props.dispatch({ type: Types.StartLoading })
- await Promise.all(
- props.lots.map(l => props.handleLot(l, props.workingCenterAID))
- )
- props.dispatch({ type: Types.FinishLotSaving })
- } catch (e) {
- props.dispatch({
- type: Types.SetMessage,
- payload: {
- message: e.toString(),
- error: true,
- },
- })
- } finally {
- props.dispatch({ type: Types.FinallyLotSaving })
- }
- }
- export interface MainEffectProps {
- state: State
- debouncedInputClear: () => void
- login: (employeeAID: string) => void
- dispatch: any
- setLot: (props: LotRow) => void
- saveLots: (workingCenterAID: string) => void
- }
- export const mainEffect = (props: MainEffectProps) => {
- const employeeAID = props.state.input
- const [order, , productAID, orderQty, qty, lotnum] = props.state.input.split(
- "/"
- )
- const [workingCenterAID] = props.state.input.split("_")
- if (!props.state.input) return
- props.debouncedInputClear()
- if (!isNaN(Number(employeeAID))) {
- props.login(employeeAID)
- } else if (props.state.currentEmployee && lotnum) {
- props.dispatch({ type: Types.ClearErrorLots })
- props.setLot({
- productAID,
- lotnum: Number(lotnum),
- order,
- qty: Number(qty),
- orderQty: Number(orderQty),
- })
- } else if (
- props.state.currentEmployee &&
- workingCenterAID &&
- props.state.scannedLots.filter(l => !l.error).length > 0
- ) {
- props.saveLots(workingCenterAID)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement