Advertisement
AlexFSmirnov

ModalManager template

Oct 20th, 2020
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import Modal from './Modal';
  3.  
  4. export interface ModalManagerProps {
  5.  
  6. }
  7.  
  8. export interface ModalManagerState {
  9.     modals: ModalDescriptor[];
  10. }
  11.  
  12. interface ModalDescriptor {
  13.     id: number;
  14.     url: string;
  15. }
  16.  
  17. class ModalManager extends React.Component<ModalManagerProps, ModalManagerState> {
  18.     private isInitialized = false;
  19.     private readyCallback: () => void = () => {};
  20.  
  21.     state: ModalManagerState = {
  22.         modals: [], // move to redux state
  23.     };
  24.  
  25.     componentDidMount() {
  26.         this.isInitialized = true;
  27.         this.readyCallback();
  28.     }
  29.  
  30.     public navigateTo = (url: string, data: object, type: 'GET' | 'POST') => {
  31.         if (this.count() === 0) {
  32.             // open new modal with given parameters
  33.         } else {
  34.             // change props of the last opened modal
  35.         }
  36.     };
  37.  
  38.     public navigateInBase = (url: string, data: object, type: 'GET' | 'POST') => {
  39.         this.closeAllModals();
  40.         this.navigateTo(url, data, type);
  41.     };
  42.  
  43.     public openModal = (url: string, data: object, type: 'GET' | 'POST') => {
  44.         // add parameters to the modals array in state
  45.         // dispatch correct windowManager actions
  46.     };
  47.  
  48.     public closeModal = (id: number) => {
  49.         // remove modal with this id
  50.         // dispatch correct windowManager actions
  51.     };
  52.  
  53.     public closeTop = () => this.closeModal(this.state.modals[this.state.modals.length - 1].id);
  54.  
  55.     public closeAllModals = () => {
  56.         this.state.modals.forEach(({ id }) => {
  57.             this.closeModal(id);
  58.         });
  59.     };
  60.  
  61.     public count = () => this.state.modals.length;
  62.  
  63.     public onReady = (callback: () => void) => {
  64.         if (this.isInitialized) {
  65.             callback();
  66.         } else {
  67.             this.readyCallback = callback;
  68.         }
  69.     };
  70.  
  71.     public onEvent = (eventName: string, eventData?: string) => {
  72.         // use modal middleware for events ???
  73.     };
  74.  
  75.     handleModalClosed = (id: number) => (eventData: string) => {
  76.         this.closeModal(id);
  77.     };
  78.  
  79.     render() {
  80.         const { modals } = this.state;
  81.  
  82.         return modals.map(({ id, url }, i) => (
  83.             <Modal
  84.                 url={url}
  85.                 isActive={i === this.count() - 1}
  86.                 onModalClosed={this.handleModalClosed(id)}
  87.                 events={{ 'xhr-end': () => {}, 'navigation-finished': () => {} }}
  88.             />
  89.         ));
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement