github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/frontend/directeurs/src/logic/formatter.ts (about) 1 import { 2 Tels, 3 MaterielSki, 4 Diplome, 5 RoleLabels, 6 Role, 7 Approfondissement, 8 ApprofondissementLabels, 9 DiplomeLabels, 10 SexeLabels, 11 Sexe, 12 BusLabels, 13 Bus, 14 OptionnalId, 15 Roles, 16 Time, 17 Date_ 18 } from "./types"; 19 20 // const DOCUMENTS: map = { 21 // bafa: "BAFA", 22 // bafd: "BAFD", 23 // carte_id: "Carte d'identité/Passeport", 24 // permis: "Permis de conduire", 25 // sb: "Surveillant de baignade", 26 // secour: "Secourisme (PSC1 - AFPS)", 27 // bafd_equiv: "Equivalent BAFD", 28 // bafa_equiv: "Equivalent BAFA", 29 // vaccin: "Vaccins", 30 // carte_vitale: "Carte Vitale", 31 // haccp: "Cuisine (HACCP)", 32 // scolarite: "Certificat de scolarité", 33 // autre: "Autre" 34 // }; 35 36 const DEPARTEMENTS: { [key: string]: string } = { 37 "01": "Ain", 38 "02": "Aisne", 39 "03": "Allier", 40 "04": "Alpes-de-Haute-Provence", 41 "05": "Hautes-Alpes", 42 "06": "Alpes-Maritimes", 43 "07": "Ardèche", 44 "08": "Ardennes", 45 "09": "Ariège", 46 "10": "Aube", 47 "11": "Aude", 48 "12": "Aveyron", 49 "13": "Bouches-du-Rhône", 50 "14": "Calvados", 51 "15": "Cantal", 52 "16": "Charente", 53 "17": "Charente-Maritime", 54 "18": "Cher", 55 "19": "Corrèze", 56 "2A": "Corse-du-Sud", 57 "2B": "Haute-Corse", 58 "21": "Côte-d'Or", 59 "22": "Côtes-d'Armor", 60 "23": "Creuse", 61 "24": "Dordogne", 62 "25": "Doubs", 63 "26": "Drôme", 64 "27": "Eure", 65 "28": "Eure-et-Loir", 66 "29": "Finistère", 67 "30": "Gard", 68 "31": "Haute-Garonne", 69 "32": "Gers", 70 "33": "Gironde", 71 "34": "Hérault", 72 "35": "Ille-et-Vilaine", 73 "36": "Indre", 74 "37": "Indre-et-Loire", 75 "38": "Isère", 76 "39": "Jura", 77 "40": "Landes", 78 "41": "Loir-et-Cher", 79 "42": "Loire", 80 "43": "Haute-Loire", 81 "44": "Loire-Atlantique", 82 "45": "Loiret", 83 "46": "Lot", 84 "47": "Lot-et-Garonne", 85 "48": "Lozère", 86 "49": "Maine-et-Loire", 87 "50": "Manche", 88 "51": "Marne", 89 "52": "Haute-Marne", 90 "53": "Mayenne", 91 "54": "Meurthe-et-Moselle", 92 "55": "Meuse", 93 "56": "Morbihan", 94 "57": "Moselle", 95 "58": "Nièvre", 96 "59": "Nord", 97 "60": "Oise", 98 "61": "Orne", 99 "62": "Pas-de-Calais", 100 "63": "Puy-de-Dôme", 101 "64": "Pyrénées-Atlantiques", 102 "65": "Hautes-Pyrénées", 103 "66": "Pyrénées-Orientales", 104 "67": "Bas-Rhin", 105 "68": "Haut-Rhin", 106 "69": "Rhône", 107 "70": "Haute-Saône", 108 "71": "Saône-et-Loire", 109 "72": "Sarthe", 110 "73": "Savoie", 111 "74": "Haute-Savoie", 112 "75": "Paris", 113 "76": "Seine-Maritime", 114 "77": "Seine-et-Marne", 115 "78": "Yvelines", 116 "79": "Deux-Sèvres", 117 "80": "Somme", 118 "81": "Tarn", 119 "82": "Tarn-et-Garonne", 120 "83": "Var", 121 "84": "Vaucluse", 122 "85": "Vendée", 123 "86": "Vienne", 124 "87": "Haute-Vienne", 125 "88": "Vosges", 126 "89": "Yonne", 127 "90": "Territoire de Belfort", 128 "91": "Essonne", 129 "92": "Hauts-de-Seine", 130 "93": "Seine-Saint-Denis", 131 "94": "Val-de-Marne", 132 "95": "Val-d'Oise", 133 "971": "Guadeloupe", 134 "972": "Martinique", 135 "973": "Guyane", 136 "974": "La Réunion", 137 "976": "Mayotte" 138 }; 139 140 function showDate(dateString: Date_) { 141 const date = new Date(dateString); 142 if (isNaN(date.valueOf())) return ""; 143 return date.toLocaleDateString("fr-FR", { 144 timeZone: "UTC", 145 day: "numeric", 146 month: "short", 147 year: "numeric" 148 }); 149 } 150 151 function showDateTime(dateTimeString: Time) { 152 const date = new Date(dateTimeString); 153 if (isNaN(date.valueOf())) return ""; 154 return date.toLocaleTimeString("fr-FR", { 155 day: "numeric", 156 month: "short", 157 year: "numeric", 158 hour: "numeric", 159 minute: "numeric" 160 }); 161 } 162 163 export const Formatter = { 164 roles: (value: Roles) => 165 (value || []).map(r => RoleLabels[r] || "").join(" ; "), 166 approfondissement: (value: Approfondissement) => 167 ApprofondissementLabels[value] || "", 168 diplome: (value: Diplome) => DiplomeLabels[value] || "", 169 date: showDate, 170 sexe: (value: Sexe) => SexeLabels[value] || "", 171 telephones: (value: Tels) => (value ? value.join(" , ") : "-"), 172 bool: (value: boolean) => (value ? "Oui" : "Non"), 173 departement: (value: string) => 174 DEPARTEMENTS[value] ? `${DEPARTEMENTS[value]} (${value})` : "", 175 date_heure: showDateTime, 176 bus: (value: Bus) => BusLabels[value] || "", 177 materiel_ski: (value: MaterielSki) => (value ? "Demandé" : "-"), 178 nomPrenom: (item: { nom: string; prenom: string }) => 179 `${item.nom} ${item.prenom}`, 180 prenomN: (item: { nom: string; prenom: string }) => 181 `${item.prenom} ${item.nom.trim().substr(0, 1)}.`, 182 taille: (taille: number) => `${(taille / 1000000).toFixed(2)} MB` 183 }; 184 185 function enumToList<T extends string | number>(enums: { [key in T]: string }) { 186 const list: { value: T; text: string }[] = []; 187 for (const key in enums) { 188 list.push({ value: key, text: enums[key] }); 189 } 190 return list.sort((a, b) => (a.text < b.text ? -1 : 1)); 191 } 192 193 export const EditFields = { 194 sexe: enumToList(SexeLabels), 195 roles: enumToList(RoleLabels), 196 diplome: enumToList(DiplomeLabels), 197 appro: enumToList(ApprofondissementLabels), 198 bus: enumToList(BusLabels), 199 departement: Object.keys(DEPARTEMENTS) 200 .map(Formatter.departement) 201 .sort((a, b) => (a < b ? -1 : 1)) 202 // documents: enumToList(DOCUMENTS), 203 // semaine: enumToList(Semain), 204 }; 205 206 const patternMail = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 207 export function isEmailValid(value: string) { 208 return patternMail.test(value) || "Mail invalide."; 209 } 210 211 export const NullDate = "0001-01-01" as Date_; 212 213 declare global { 214 interface Date { 215 toDate_(): Date_; 216 } 217 } 218 219 Date.prototype.toDate_ = function() { 220 return this.toISOString().split("T")[0] as Date_; 221 }; 222 223 export const nullId = (): OptionnalId => { 224 return { Valid: false, Int64: 0 }; 225 }; 226 export const optionnalId = (id: number): OptionnalId => { 227 return { Valid: true, Int64: id }; 228 };