From 30940dda48059cfd7addd27c7cb32f202a4b7486 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 14 Mar 2018 18:56:55 +0100 Subject: [PATCH] [webpack.data.ue] manage page 'manage' load on nav from page 'view' + standalone with redirection if invalid 'code' in URL --- webpack/component/ue/manage.vue | 20 +++- webpack/data/ue.js | 160 +++++++++++++++++++++++++++++--- 2 files changed, 162 insertions(+), 18 deletions(-) diff --git a/webpack/component/ue/manage.vue b/webpack/component/ue/manage.vue index 7b95565..0c0418d 100644 --- a/webpack/component/ue/manage.vue +++ b/webpack/component/ue/manage.vue @@ -4,17 +4,26 @@
+
-

{{ $route.params.code }}

+
{{ `${c.firstName} ${c.lastName}` || 'blabla' }}
+
{{ c.volume }}
-
+ + + + + + +
@@ -31,6 +40,9 @@ name: 'CONTAINER_VIEW', data(){ return { gstore: gstore.get } + }, + beforeMount(){ + gstore.get.manage_load(this.$route.params.code, this.$router); } } diff --git a/webpack/data/ue.js b/webpack/data/ue.js index 81a3589..34bbfb2 100644 --- a/webpack/data/ue.js +++ b/webpack/data/ue.js @@ -262,7 +262,7 @@ gstore.add('ir_handler', function(ue_id){ var gi = gstore.get.ues.map( (data, i) => { return ( data.code && data.code == ue_id ) ? i : ''; }).join(''); /* (2.2) Exit if not found */ - if( isNaN(gi) ) return; + if( gi.length === 0 ) return; var local = gstore.get.ues[gi]; /* (3) Show popup */ @@ -536,10 +536,112 @@ gstore.add('ia_handler', function(ue_i){ }); +/* (9) Manage 'manage' sub-page +---------------------------------------------------------*/ +/* (1) Initialize current opened UE data */ +gstore.add('manage', { + cours: [], + td: [], + tp: [], +}); + + +/* (2) Function to load UE data */ +gstore.add('load_ue_groups', function(code, recur=0){ + + /* (1) If UE not already loaded -> wait for 200ms */ + if( gstore.get.ues.length === 0 && recur > 0 ){ + + return new Promise( (rs, rj) => setTimeout( () => { + + return gstore.get.load_ue_groups(code, --recur).catch( (rj2) => rj(rj2) ); + + }, 200) ); + + } + + + /* (2.1) Check that code is a valid existing UE code */ + if( code == null ) + return new Promise( (rs, rj) => rj({label: 'interne', code: -1}) ); + + /* (2.2) Find index in gstore */ + var ue_i = gstore.get.ues.map( (data, i) => { return ( data.code && data.code == code ) ? i : ''; }).join(''); + + /* (2.3) Exit if not found */ + if( ue_i.length === 0 ) + return new Promise( (rs, rj) => rj({label: 'interne', code: -1}) ); + + + /* (3) Ask for UE Cours data */ + return new Promise( (resolve, reject) => { + + api.call(`GET ue/cours/${code}`, {}, function(rs){ + + // reject on error + if( rs.error !== 0 || !(rs['groups'] instanceof Array) ){ + gstore.get.manage.cours.splice(); + reject({ label: 'Cours', code: rs.error}); + } + + // resolve data on success + gstore.get.manage.cours = rs.groups; + resolve(); + + }); + + /* (4) Ask for UE TD data */ + }).then( () => { + + return new Promise( (resolve, reject) => { + + api.call(`GET ue/td/${code}`, {}, function(rs){ + + // reject on error + if( rs.error !== 0 || !(rs['groups'] instanceof Array) ){ + gstore.get.manage.td.splice(); + reject({ label: 'TD', code: rs.error}); + } + + // resolve data on success + gstore.get.manage.td = rs.groups; + resolve(); + + }); + + }); + + /* (5) Ask for UE TP data */ + }).then( () => { + + return new Promise( (resolve, reject) => { + + api.call(`GET ue/tp/${code}`, {}, function(rs){ + + // reject on error + if( rs.error !== 0 || !(rs['groups'] instanceof Array) ){ + gstore.get.manage.tp.splice(); + reject({ label: 'TP', code: rs.error}); + } + + // resolve data on success + gstore.get.manage.tp = rs.groups; + resolve(); + + }); + + }); + + }); + +}) -/* (9) Manage navigation + + + +/* (10) Manage navigation ---------------------------------------------------------*/ /* (1) Initialize current opened UE */ gstore.add('nav_anim', { @@ -554,18 +656,23 @@ gstore.add('nav_in', function($router, ue_i){ if( ue_i == null || isNaN(ue_i) || gstore.get.ues[ue_i] == null) return; - /* (2) Set animation class */ - gstore.get.nav_anim.in = true; + /* (2) Try to get data (Promise) */ + gstore.get.load_ue_groups(gstore.get.ues[ue_i].code).then( () => { - setTimeout( () => { + /* 1. Start animation */ + gstore.get.nav_anim.in = true; - /* (3) Load page */ - $router.push(`/ue/manage/${gstore.get.ues[ue_i].code}`); + setTimeout( () => { - /* (4) Remove animation class */ - gstore.get.nav_anim.in = false; + /* 2. Load page */ + $router.push(`/ue/manage/${gstore.get.ues[ue_i].code}`); - }, 500 ); + /* 3. Stop animation */ + gstore.get.nav_anim.in = false; + + }, 500 ); + + }).catch( (err) => console.log(`[label] ${err.label} [error] ${err.code}`) ); }); @@ -573,18 +680,43 @@ gstore.add('nav_in', function($router, ue_i){ /* (3) Manage nav out */ gstore.add('nav_out', function($router){ - /* (1) Set animation class */ + /* 1. Start animation */ gstore.get.nav_anim.out = true; setTimeout( () => { - /* (2) Load page */ + /* 2. Load page */ $router.push(`/ue/view/`); - /* (3) Remove animation class */ + /* 3. Stop animation */ gstore.get.nav_anim.out = false; + /* 4. Remove 'manage' data */ + gstore.get.manage.cours = []; + gstore.get.manage.td = []; + gstore.get.manage.tp = []; + }, 500 ); -}); \ No newline at end of file +}); + + + + + + + +/* (11) Load 'manage' page data if loaded from URL +---------------------------------------------------------*/ +gstore.add('manage_load', function(code, $router){ + + /* (1) Try to load data */ + gstore.get.load_ue_groups(code, 3).catch( (err) => { // try at max 3 times (200ms each) for UE to be loaded + + /* (2) On error -> go to 'view' page */ + $router.push('/ue/view/'); + + }); + +})