Best JavaScript code snippet using root
create.component.js
Source:create.component.js
1import React, { Component, useState, useEffect, useContext } from "react";2import axios from "axios";3import { withSnackbar, useSnackbar } from "notistack";4import { useHistory } from "react-router-dom";5import Select from "react-select";6import { ValidatorForm, TextValidator } from "react-material-ui-form-validator";7import { withNamespaces, useTranslation } from "react-i18next";8import ImageUploader from "react-images-upload";9import { AuthContext } from "../../Context/AuthContext";10import {11 FormControl,12 DialogContentText,13 FormGroup,14 InputLabel,15 Input,16 FormHelperText,17 Card,18 Button,19 Typography,20 TextField,21 Tooltip,22 Dialog,23 DialogActions,24 DialogContent,25 DialogTitle,26 Checkbox,27 FormControlLabel,28 Grid,29} from "@material-ui/core";30import { AddBox, GroupAdd, Delete, Save } from "@material-ui/icons";31import "../../assets/css/style.css";32export default function ProductEdit(props) {33 const [t] = useTranslation();34 const history = useHistory();35 const { enqueueSnackbar } = useSnackbar();36 const [openalert, seTopenalert] = useState(false);37 const { isAuthenticated, user } = useContext(AuthContext);38 const [state, seTstate] = useState({39 username: "",40 username: "",41 name: "",42 surname: "",43 password: "",44 phone: "",45 _id: "",46 created_user: "",47 });48 const [permissions, seTpermissions] = useState({49 staffonlyyou: true,50 staffcreate: false,51 staffedit: false,52 stafflist: false,53 staffdelete: false,54 customersonlyyou: true,55 customerscreate: false,56 customersedit: false,57 customerslist: false,58 customersdelete: false,59 productsonlyyou: true,60 productscreate: false,61 productsedit: false,62 productslist: false,63 productsdelete: false,64 bankaccountsonlyyou: true,65 bankaccountscreate: false,66 bankaccountsedit: false,67 bankaccountslist: false,68 bankaccountsdelete: false,69 customersgrouponlyyou: true,70 customersgroupcreate: false,71 customersgroupedit: false,72 customersgrouplist: false,73 customersgroupdelete: false,74 invoicesonlyyou: true,75 invoicescreate: false,76 invoicesedit: false,77 invoiceslist: false,78 invoicesdelete: false,79 paymentsonlyyou: true,80 paymentscreate: false,81 paymentsedit: false,82 paymentslist: false,83 paymentsdelete: false,84 productsCategoriesonlyyou: true,85 productsCategoriescreate: false,86 productsCategoriesedit: false,87 productsCategorieslist: false,88 productsCategoriesdelete: false,89 expensesonlyyou: true,90 expensescreate: false,91 expensesedit: false,92 expenseslist: false,93 expensesdelete: false,94 expensescategoriesonlyyou: true,95 expensescategoriescreate: false,96 expensescategoriesedit: false,97 expensescategorieslist: false,98 expensescategoriesdelete: false,99 paymentsaccountsonlyyou: true,100 paymentsaccountscreate: false,101 paymentsaccountsedit: false,102 paymentsaccountslist: false,103 paymentsaccountsdelete: false,104 });105 // componentDidMount = useEffect106 useEffect(() => {}, []);107 const onSubmit = (e) => {108 e.preventDefault();109 const Staff = {110 created_user: { name: user.name, id: user.id },111 username: state.username,112 name: state.name,113 surname: state.surname,114 phone: state.phone,115 password: state.password,116 role: permissions,117 isCustomer:false,118 };119 axios120 .post(`/staff/add`, Staff)121 .then((res) => {122 if (res.data.variant == "error") {123 enqueueSnackbar(t("Staff Not Updated ") + res.data.messagge, { variant: res.data.variant });124 } else {125 enqueueSnackbar(t("Staff Updated"), {126 variant: res.data.variant,127 });128 history.push("/stafflist");129 }130 })131 .catch((err) => console.log(err));132 };133 return (134 <div className="containerP">135 <ValidatorForm autoComplete="off" onSubmit={onSubmit}>136 <Grid item container spacing={3}>137 <Grid item container md={3} className="panelGridRelative">138 <Card className="panelLargeIcon">139 <GroupAdd fontSize="large" />140 </Card>141 <div className="listViewPaper">142 <Typography component="h1" variant="h6" color="inherit" noWrap style={{ width: "100%" }} className="typography">143 {t("Create User")}144 </Typography>145 <Grid item container sm={12}>146 <div style={{ clear: "both" }}></div>147 <Grid container item sm={12} spacing={0}>148 <FormGroup className="FormGroup">149 <FormControl>150 <TextValidator151 InputLabelProps={{152 shrink: true,153 }}154 variant="outlined"155 margin="dense"156 label={t("Name")}157 value={state.name}158 onChange={(e) => {159 seTstate({160 ...state,161 name: e.target.value,162 });163 }}164 required165 />166 <FormHelperText>{t("You need Name")}</FormHelperText>167 </FormControl>168 </FormGroup>169 </Grid>170 <Grid container item sm={12} spacing={0}>171 <FormGroup className="FormGroup">172 <FormControl>173 <TextValidator174 InputLabelProps={{175 shrink: true,176 }}177 variant="outlined"178 margin="dense"179 label={t("Surname")}180 value={state.surname}181 onChange={(e) => {182 seTstate({183 ...state,184 surname: e.target.value,185 });186 }}187 required188 />189 <FormHelperText>{t("You need Surname")}</FormHelperText>190 </FormControl>191 </FormGroup>192 </Grid>193 <Grid container item sm={12} spacing={0}>194 <FormGroup className="FormGroup">195 <FormControl>196 <TextValidator197 InputLabelProps={{198 shrink: true,199 }}200 variant="outlined"201 margin="dense"202 label={t("E-mail")}203 value={state.username}204 onChange={(e) => {205 seTstate({206 ...state,207 username: e.target.value,208 });209 }}210 required211 />212 <FormHelperText>{t("You need E-mail")}</FormHelperText>213 </FormControl>214 </FormGroup>215 </Grid>216 <Grid container item sm={12} spacing={0}>217 <FormGroup className="FormGroup">218 <FormControl>219 <TextValidator220 InputLabelProps={{221 shrink: true,222 }}223 variant="outlined"224 margin="dense"225 label={t("Phone")}226 value={state.phone}227 onChange={(e) => {228 seTstate({229 ...state,230 phone: e.target.value,231 });232 }}233 required234 />235 <FormHelperText>{t("You need Phone")}</FormHelperText>236 </FormControl>237 </FormGroup>238 </Grid>239 <Grid container item sm={12} spacing={0}>240 <FormGroup className="FormGroup">241 <FormControl>242 <TextValidator243 InputLabelProps={{244 shrink: true,245 }}246 variant="outlined"247 margin="dense"248 label={t("Password")}249 value={state.password}250 onChange={(e) => {251 seTstate({252 ...state,253 password: e.target.value,254 });255 }}256 />257 <FormHelperText>{t("Note: if you populate this field, password will be changed on this member.")}</FormHelperText>258 </FormControl>259 </FormGroup>260 </Grid>261 </Grid>262 </div>263 <div className="saveButtonPlace">264 <Button type="submit" onClick={onSubmit} className="glow-on-hover">265 <Save fontSize="small" style={{ marginRight: "15px" }} />266 {t("save")}267 </Button>268 </div>269 </Grid>270 {user.role["superadmin"] ? (271 <Grid item container md={5} className="panelGridRelative">272 <div className="listViewPaperPadding">273 <Typography component="h1" variant="h6" color="inherit" noWrap style={{ width: "100%" }}>274 {t("Permissions")}275 </Typography>276 <Grid item container sm={12} className="permissions">277 <div className="permissions_div">278 <b>{t("Staff")}</b>279 <FormControlLabel280 control={281 <Checkbox282 checked={permissions.staffonlyyou}283 onChange={(e, val) => {284 const deg = permissions;285 deg.staffonlyyou = val;286 deg.stafflist = false;287 seTpermissions(deg);288 seTstate({...state})289 }}290 color="primary"291 />292 }293 label={t("View (Own)")}294 />295 <FormControlLabel296 control={297 <Checkbox298 checked={permissions.stafflist}299 onChange={(e, val) => {300 const deg = permissions;301 deg.stafflist = val;302 deg.staffonlyyou = false;303 seTpermissions(deg);304 seTstate({...state})305 306 }}307 color="primary"308 />309 }310 label={t("View (Global)")}311 />312 <FormControlLabel313 control={314 <Checkbox315 checked={permissions.staffcreate}316 onChange={(e, val) => {317 const deg = permissions;318 deg.staffcreate = val;319 seTpermissions(deg);320 seTstate({...state})321 322 }}323 color="primary"324 />325 }326 label={t("Create")}327 />328 <FormControlLabel329 control={330 <Checkbox331 checked={permissions.staffedit}332 onChange={(e, val) => {333 const deg = permissions;334 deg.staffedit = val;335 seTpermissions(deg);336 seTstate({...state})337 }}338 color="primary"339 />340 }341 label={t("Edit")}342 />343 <FormControlLabel344 control={345 <Checkbox346 checked={permissions.staffdelete}347 onChange={(e, val) => {348 const deg = permissions;349 deg.staffdelete = val;350 seTpermissions(deg);351 seTstate({...state})352 }}353 color="primary"354 />355 }356 label={t("Delete")}357 />358 </div>359 <div className="permissions_div">360 <b>{t("Customers")}</b>361 <FormControlLabel362 control={363 <Checkbox364 checked={permissions.customersonlyyou}365 onChange={(e, val) => {366 const deg = permissions;367 deg.customersonlyyou = val;368 deg.customerslist = false;369 seTpermissions(deg);370 seTstate({...state})371 }}372 color="primary"373 />374 }375 label={t("View (Own)")}376 />377 <FormControlLabel378 control={379 <Checkbox380 checked={permissions.customerslist}381 onChange={(e, val) => {382 const deg = permissions;383 deg.customerslist = val;384 deg.customersonlyyou = false;385 seTpermissions(deg);386 seTstate({...state})387 }}388 color="primary"389 />390 }391 label={t("View (Global)")}392 />393 <FormControlLabel394 control={395 <Checkbox396 checked={permissions.customerscreate}397 onChange={(e, val) => {398 const deg = permissions;399 deg.customerscreate = val;400 seTpermissions(deg);401 seTstate({...state})402 }}403 color="primary"404 />405 }406 label={t("Create")}407 />408 <FormControlLabel409 control={410 <Checkbox411 checked={permissions.customersedit}412 onChange={(e, val) => {413 const deg = permissions;414 deg.customersedit = val;415 seTpermissions(deg);416 seTstate({...state})417 }}418 color="primary"419 />420 }421 label={t("Edit")}422 />423 <FormControlLabel424 control={425 <Checkbox426 checked={permissions.customersdelete}427 onChange={(e, val) => {428 const deg = permissions;429 deg.customersdelete = val;430 seTpermissions(deg);431 seTstate({...state})432 }}433 color="primary"434 />435 }436 label={t("Delete")}437 />438 </div>439 <div className="permissions_div">440 <b>{t("Products")}</b>441 <FormControlLabel442 control={443 <Checkbox444 checked={permissions.productsonlyyou}445 onChange={(e, val) => {446 const deg = permissions;447 deg.productsonlyyou = val;448 deg.productslist = false;449 seTpermissions(deg);450 seTstate({...state})451 }}452 color="primary"453 />454 }455 label={t("View (Own)")}456 />457 <FormControlLabel458 control={459 <Checkbox460 checked={permissions.productslist}461 onChange={(e, val) => {462 const deg = permissions;463 deg.productslist = val;464 deg.productsonlyyou = false;465 seTpermissions(deg);466 seTstate({...state})467 }}468 color="primary"469 />470 }471 label={t("View (Global)")}472 />473 <FormControlLabel474 control={475 <Checkbox476 checked={permissions.productscreate}477 onChange={(e, val) => {478 const deg = permissions;479 deg.productscreate = val;480 seTpermissions(deg);481 seTstate({...state})482 }}483 color="primary"484 />485 }486 label={t("Create")}487 />488 <FormControlLabel489 control={490 <Checkbox491 checked={permissions.productsedit}492 onChange={(e, val) => {493 const deg = permissions;494 deg.productsedit = val;495 seTpermissions(deg);496 seTstate({...state})497 }}498 color="primary"499 />500 }501 label={t("Edit")}502 />503 <FormControlLabel504 control={505 <Checkbox506 checked={permissions.productsdelete}507 onChange={(e, val) => {508 const deg = permissions;509 deg.productsdelete = val;510 seTpermissions(deg);511 seTstate({...state})512 }}513 color="primary"514 />515 }516 label={t("Delete")}517 />518 </div>519 <div className="permissions_div">520 <b>{t("Bank Accounts")}</b>521 <FormControlLabel522 control={523 <Checkbox524 checked={permissions.bankaccountsonlyyou}525 onChange={(e, val) => {526 const deg = permissions;527 deg.bankaccountsonlyyou = val;528 deg.bankaccountslist = false;529 seTpermissions(deg);530 seTstate({...state})531 }}532 color="primary"533 />534 }535 label={t("View (Own)")}536 />537 <FormControlLabel538 control={539 <Checkbox540 checked={permissions.bankaccountslist}541 onChange={(e, val) => {542 const deg = permissions;543 deg.bankaccountslist = val;544 deg.bankaccountsonlyyou = false;545 seTpermissions(deg);546 seTstate({...state})547 }}548 color="primary"549 />550 }551 label={t("View (Global)")}552 />553 <FormControlLabel554 control={555 <Checkbox556 checked={permissions.bankaccountscreate}557 onChange={(e, val) => {558 const deg = permissions;559 deg.bankaccountscreate = val;560 seTpermissions(deg);561 seTstate({...state})562 }}563 color="primary"564 />565 }566 label={t("Create")}567 />568 <FormControlLabel569 control={570 <Checkbox571 checked={permissions.bankaccountsedit}572 onChange={(e, val) => {573 const deg = permissions;574 deg.bankaccountsedit = val;575 seTpermissions(deg);576 seTstate({...state})577 }}578 color="primary"579 />580 }581 label={t("Edit")}582 />583 <FormControlLabel584 control={585 <Checkbox586 checked={permissions.bankaccountsdelete}587 onChange={(e, val) => {588 const deg = permissions;589 deg.bankaccountsdelete = val;590 seTpermissions(deg);591 seTstate({...state})592 }}593 color="primary"594 />595 }596 label={t("Delete")}597 />598 </div>599 <div className="permissions_div">600 <b>{t("Customers Group")}</b>601 <FormControlLabel602 control={603 <Checkbox604 checked={permissions.customersgrouponlyyou}605 onChange={(e, val) => {606 const deg = permissions;607 deg.customersgrouponlyyou = val;608 deg.customersgrouplist = false;609 seTpermissions(deg);610 seTstate({...state})611 }}612 color="primary"613 />614 }615 label={t("View (Own)")}616 />617 <FormControlLabel618 control={619 <Checkbox620 checked={permissions.customersgrouplist}621 onChange={(e, val) => {622 const deg = permissions;623 deg.customersgrouplist = val;624 deg.customersgrouponlyyou = false;625 seTpermissions(deg);626 seTstate({...state})627 }}628 color="primary"629 />630 }631 label={t("View (Global)")}632 />633 <FormControlLabel634 control={635 <Checkbox636 checked={permissions.customersgroupcreate}637 onChange={(e, val) => {638 const deg = permissions;639 deg.customersgroupcreate = val;640 seTpermissions(deg);641 seTstate({...state})642 }}643 color="primary"644 />645 }646 label={t("Create")}647 />648 <FormControlLabel649 control={650 <Checkbox651 checked={permissions.customersgroupedit}652 onChange={(e, val) => {653 const deg = permissions;654 deg.customersgroupedit = val;655 seTpermissions(deg);656 seTstate({...state})657 }}658 color="primary"659 />660 }661 label={t("Edit")}662 />663 <FormControlLabel664 control={665 <Checkbox666 checked={permissions.customersgroupdelete}667 onChange={(e, val) => {668 const deg = permissions;669 deg.customersgroupdelete = val;670 seTpermissions(deg);671 seTstate({...state})672 }}673 color="primary"674 />675 }676 label={t("Delete")}677 />678 </div>679 <div className="permissions_div">680 <b>{t("Invoices")}</b>681 <FormControlLabel682 control={683 <Checkbox684 checked={permissions.invoicesonlyyou}685 onChange={(e, val) => {686 const deg = permissions;687 deg.invoicesonlyyou = val;688 deg.invoiceslist = false;689 seTpermissions(deg);690 seTstate({...state})691 }}692 color="primary"693 />694 }695 label={t("View (Own)")}696 />697 <FormControlLabel698 control={699 <Checkbox700 checked={permissions.invoiceslist}701 onChange={(e, val) => {702 const deg = permissions;703 deg.invoiceslist = val;704 deg.invoicesonlyyou = false;705 seTpermissions(deg);706 seTstate({...state})707 }}708 color="primary"709 />710 }711 label={t("View (Global)")}712 />713 <FormControlLabel714 control={715 <Checkbox716 checked={permissions.invoicescreate}717 onChange={(e, val) => {718 const deg = permissions;719 deg.invoicescreate = val;720 seTpermissions(deg);721 seTstate({...state})722 }}723 color="primary"724 />725 }726 label={t("Create")}727 />728 <FormControlLabel729 control={730 <Checkbox731 checked={permissions.invoicesedit}732 onChange={(e, val) => {733 const deg = permissions;734 deg.invoicesedit = val;735 seTpermissions(deg);736 seTstate({...state})737 }}738 color="primary"739 />740 }741 label={t("Edit")}742 />743 <FormControlLabel744 control={745 <Checkbox746 checked={permissions.invoicesdelete}747 onChange={(e, val) => {748 const deg = permissions;749 deg.invoicesdelete = val;750 seTpermissions(deg);751 seTstate({...state})752 }}753 color="primary"754 />755 }756 label={t("Delete")}757 />758 </div>759 760 <div className="permissions_div">761 <b>{t("PaymentsMethod")}</b>762 <FormControlLabel763 control={764 <Checkbox765 checked={permissions.paymentsonlyyou}766 onChange={(e, val) => {767 const deg = permissions;768 deg.paymentsonlyyou = val;769 deg.paymentslist = false;770 seTpermissions(deg);771 seTstate({...state})772 }}773 color="primary"774 />775 }776 label={t("View (Own)")}777 />778 <FormControlLabel779 control={780 <Checkbox781 checked={permissions.paymentslist}782 onChange={(e, val) => {783 const deg = permissions;784 deg.paymentslist = val;785 deg.paymentsonlyyou = false;786 seTpermissions(deg);787 seTstate({...state})788 }}789 color="primary"790 />791 }792 label={t("View (Global)")}793 />794 <FormControlLabel795 control={796 <Checkbox797 checked={permissions.paymentscreate}798 onChange={(e, val) => {799 const deg = permissions;800 deg.paymentscreate = val;801 seTpermissions(deg);802 seTstate({...state})803 }}804 color="primary"805 />806 }807 label={t("Create")}808 />809 <FormControlLabel810 control={811 <Checkbox812 checked={permissions.paymentsedit}813 onChange={(e, val) => {814 const deg = permissions;815 deg.paymentsedit = val;816 seTpermissions(deg);817 seTstate({...state})818 }}819 color="primary"820 />821 }822 label={t("Edit")}823 />824 <FormControlLabel825 control={826 <Checkbox827 checked={permissions.paymentsdelete}828 onChange={(e, val) => {829 const deg = permissions;830 deg.paymentsdelete = val;831 seTpermissions(deg);832 seTstate({...state})833 }}834 color="primary"835 />836 }837 label={t("Delete")}838 />839 </div>840 <div className="permissions_div">841 <b>{t("Products Categories")}</b>842 <FormControlLabel843 control={844 <Checkbox845 checked={permissions.productsCategoriesonlyyou}846 onChange={(e, val) => {847 const deg = permissions;848 deg.productsCategoriesonlyyou = val;849 deg.productsCategorieslist = false;850 seTpermissions(deg);851 seTstate({...state})852 }}853 color="primary"854 />855 }856 label={t("View (Own)")}857 />858 <FormControlLabel859 control={860 <Checkbox861 checked={permissions.productsCategorieslist}862 onChange={(e, val) => {863 const deg = permissions;864 deg.productsCategorieslist = val;865 deg.productsCategoriesonlyyou = false;866 seTpermissions(deg);867 seTstate({...state})868 }}869 color="primary"870 />871 }872 label={t("View (Global)")}873 />874 <FormControlLabel875 control={876 <Checkbox877 checked={permissions.productsCategoriescreate}878 onChange={(e, val) => {879 const deg = permissions;880 deg.productsCategoriescreate = val;881 seTpermissions(deg);882 seTstate({...state})883 }}884 color="primary"885 />886 }887 label={t("Create")}888 />889 <FormControlLabel890 control={891 <Checkbox892 checked={permissions.productsCategoriesedit}893 onChange={(e, val) => {894 const deg = permissions;895 deg.productsCategoriesedit = val;896 seTpermissions(deg);897 seTstate({...state})898 }}899 color="primary"900 />901 }902 label={t("Edit")}903 />904 <FormControlLabel905 control={906 <Checkbox907 checked={permissions.productsCategoriesdelete}908 onChange={(e, val) => {909 const deg = permissions;910 deg.productsCategoriesdelete = val;911 seTpermissions(deg);912 seTstate({...state})913 }}914 color="primary"915 />916 }917 label={t("Delete")}918 />919 </div>920 <div className="permissions_div">921 <b>{t("Expenses")}</b>922 <FormControlLabel923 control={924 <Checkbox925 checked={permissions.expensesonlyyou}926 onChange={(e, val) => {927 const deg = permissions;928 deg.expensesonlyyou = val;929 deg.expenseslist = false;930 seTpermissions(deg);931 seTstate({...state})932 }}933 color="primary"934 />935 }936 label={t("View (Own)")}937 />938 <FormControlLabel939 control={940 <Checkbox941 checked={permissions.expenseslist}942 onChange={(e, val) => {943 const deg = permissions;944 deg.expenseslist = val;945 deg.expensesonlyyou = false;946 seTpermissions(deg);947 seTstate({...state})948 }}949 color="primary"950 />951 }952 label={t("View (Global)")}953 />954 <FormControlLabel955 control={956 <Checkbox957 checked={permissions.expensescreate}958 onChange={(e, val) => {959 const deg = permissions;960 deg.expensescreate = val;961 seTpermissions(deg);962 seTstate({...state})963 }}964 color="primary"965 />966 }967 label={t("Create")}968 />969 <FormControlLabel970 control={971 <Checkbox972 checked={permissions.expensesedit}973 onChange={(e, val) => {974 const deg = permissions;975 deg.expensesedit = val;976 seTpermissions(deg);977 seTstate({...state})978 }}979 color="primary"980 />981 }982 label={t("Edit")}983 />984 <FormControlLabel985 control={986 <Checkbox987 checked={permissions.expensesdelete}988 onChange={(e, val) => {989 const deg = permissions;990 deg.expensesdelete = val;991 seTpermissions(deg);992 seTstate({...state})993 }}994 color="primary"995 />996 }997 label={t("Delete")}998 />999 </div>1000 <div className="permissions_div">1001 <b>{t("Expenses Categories")}</b>1002 <FormControlLabel1003 control={1004 <Checkbox1005 checked={permissions.expensescategoriesonlyyou}1006 onChange={(e, val) => {1007 const deg = permissions;1008 deg.expensescategoriesonlyyou = val;1009 deg.expensescategorieslist = false;1010 seTpermissions(deg);1011 seTstate({...state})1012 }}1013 color="primary"1014 />1015 }1016 label={t("View (Own)")}1017 />1018 <FormControlLabel1019 control={1020 <Checkbox1021 checked={permissions.expensescategorieslist}1022 onChange={(e, val) => {1023 const deg = permissions;1024 deg.expensescategorieslist = val;1025 deg.expensescategoriesonlyyou = false;1026 seTpermissions(deg);1027 seTstate({...state})1028 }}1029 color="primary"1030 />1031 }1032 label={t("View (Global)")}1033 />1034 <FormControlLabel1035 control={1036 <Checkbox1037 checked={permissions.expensescategoriescreate}1038 onChange={(e, val) => {1039 const deg = permissions;1040 deg.expensescategoriescreate = val;1041 seTpermissions(deg);1042 seTstate({...state})1043 }}1044 color="primary"1045 />1046 }1047 label={t("Create")}1048 />1049 <FormControlLabel1050 control={1051 <Checkbox1052 checked={permissions.expensescategoriesedit}1053 onChange={(e, val) => {1054 const deg = permissions;1055 deg.expensescategoriesedit = val;1056 seTpermissions(deg);1057 seTstate({...state})1058 }}1059 color="primary"1060 />1061 }1062 label={t("Edit")} 1063 />1064 <FormControlLabel1065 control={1066 <Checkbox1067 checked={permissions.expensescategoriesdelete}1068 onChange={(e, val) => {1069 const deg = permissions;1070 deg.expensescategoriesdelete = val;1071 seTpermissions(deg);1072 seTstate({...state})1073 }}1074 color="primary"1075 />1076 }1077 label={t("Delete")}1078 />1079 </div>1080 <div className="permissions_div">1081 <b>{t("Payments Accounts")}</b>1082 <FormControlLabel1083 control={1084 <Checkbox1085 checked={permissions.paymentsaccountsonlyyou}1086 onChange={(e, val) => {1087 const deg = permissions;1088 deg.paymentsaccountsonlyyou = val;1089 deg.paymentsaccountslist = false;1090 seTpermissions(deg);1091 seTstate({...state})1092 }}1093 color="primary"1094 />1095 }1096 label={t("View (Own)")}1097 />1098 <FormControlLabel1099 control={1100 <Checkbox1101 checked={permissions.paymentsaccountslist}1102 onChange={(e, val) => {1103 const deg = permissions;1104 deg.paymentsaccountslist = val;1105 deg.paymentsaccountsonlyyou = false;1106 seTpermissions(deg);1107 seTstate({...state})1108 }}1109 color="primary"1110 />1111 }1112 label={t("View (Global)")}1113 />1114 <FormControlLabel1115 control={1116 <Checkbox1117 checked={permissions.paymentsaccountscreate}1118 onChange={(e, val) => {1119 const deg = permissions;1120 deg.paymentsaccountscreate = val;1121 seTpermissions(deg);1122 seTstate({...state})1123 }}1124 color="primary"1125 />1126 }1127 label={t("Create")}1128 />1129 <FormControlLabel1130 control={1131 <Checkbox1132 checked={permissions.paymentsaccountsedit}1133 onChange={(e, val) => {1134 const deg = permissions;1135 deg.paymentsaccountsedit = val;1136 seTpermissions(deg);1137 seTstate({...state})1138 }}1139 color="primary"1140 />1141 }1142 label={t("Edit")} 1143 />1144 <FormControlLabel1145 control={1146 <Checkbox1147 checked={permissions.paymentsaccountsdelete}1148 onChange={(e, val) => {1149 const deg = permissions;1150 deg.paymentsaccountsdelete = val;1151 seTpermissions(deg);1152 seTstate({...state})1153 }}1154 color="primary"1155 />1156 }1157 label={t("Delete")}1158 />1159 </div>1160 </Grid>1161 </div>1162 </Grid>1163 ) : (1164 <div></div>1165 )}1166 </Grid>1167 </ValidatorForm>1168 </div>1169 );...
client.test.js
Source:client.test.js
...53 let socket = new mocks.Socket(123);54 let client = new Client(socket);55 client.isInitiated = true;56 client.updateRooms(); //add to default rooms57 client.setPermissions(1, 2);58 expect(mocks.Socket.rooms).toEqual({59 level_0: {60 socket_123: true61 },62 level_socket_123: {63 socket_123: true64 },65 level_1: {66 socket_123: true67 }68 });69 expect(client.permissions.read.has(defaultBasePermission)).toBe(true);70 expect(client.permissions.read.has('socket_123')).toBe(true);71 expect(client.permissions.read.has(1)).toBe(true);72});73test('4. init method', () => {74 let socket = new mocks.Socket(123);75 let client = new Client(socket);76 let proxy = new OH('test', mocks.server, {});77 let instance = OH.getInstance(proxy);78 instance.io = mocks.io;79 80 client.init(instance);81 expect(client.isInitiated).toBe(true);82 expect(instance.clients.has('socket_123')).toBe(true);83 expect(instance.io.lastEmit).toEqual({84 message: 'init',85 to: {86 socket_123: true87 },88 changes: {89 id: 'socket_123',90 obj: {}91 }92 });93});94test('5. prepareObject method', (done) => {95 let anInfrastructure = cloneDeep(mocks.infrastructure);96 let proxy = new OH('test', mocks.server, anInfrastructure, { delay: 10 });97 let instance = OH.getInstance(proxy);98 let delay = 20;99 instance.io = mocks.io;100 let socket = new mocks.Socket(123);101 let client = new Client(socket);102 handlers.onConnection.call(instance, client); //should trigger event-change for client initiation103 instance.setPermissions('a_number', 1);104 instance.setPermissions('a_string', 0, 0);//on purpose105 instance.setPermissions('nested1.nested2.nested3', [2,3]);106 instance.setPermissions('nested1.nested2_alt[0-2][1]', 1);107 instance.setPermissions('an_arr[0-5].nestedArr', 2);108 instance.setPermissions('an_arr[5].nestedArr', [2,3]);109 client.setPermissions(0, 0);110 111 setTimeout(() => {112 //should init the client by the time we reach here113 expect(instance.io.lastEmit.changes.obj).toEqual({114 a_string: 'some string',115 nested1: {116 nested2: {},117 nested2_alt: [0, [0, undefined, ['a']], 2]118 },119 an_arr: [120 0, 1,121 { a:'a', b:'b' },122 3, 4,123 { a:'a', b:'b' },124 6, 7125 ]126 });127 part2();128 }, delay);129 function part2() {130 client.setPermissions(1);131 let obj = client.prepareObject(instance, '');132 expect(obj).toEqual({133 a_number: 1.23,134 a_string: 'some string',135 nested1: {136 nested2: {},137 nested2_alt: [0, [0, 1, ['a']], 2]138 },139 an_arr: [140 0, 1,141 { a:'a', b:'b' },142 3, 4,143 { a:'a', b:'b' },144 6, 7145 ]146 });147 part3();148 }149 function part3() {150 client.setPermissions(2);151 let obj = client.prepareObject(instance, '');152 expect(obj).toEqual({153 a_string: 'some string',154 nested1: {155 nested2: {156 nested3: true157 },158 nested2_alt: [0, [0, undefined, ['a']], 2]159 },160 an_arr: [161 0, 1,162 { a:'a', b:'b', nestedArr: [{c:'c'}] },163 3, 4,164 { a:'a', b:'b', nestedArr: [{c:'c'}] },165 6, 7166 ]167 });168 part4();169 }170 function part4() {171 client.setPermissions(3);172 let obj = client.prepareObject(instance, '');173 expect(obj).toEqual({174 a_string: 'some string',175 nested1: {176 nested2: {177 nested3: true178 },179 nested2_alt: [0, [0, undefined, ['a']], 2]180 },181 an_arr: [182 0, 1,183 { a:'a', b:'b' },184 3, 4,185 { a:'a', b:'b', nestedArr: [{c:'c'}] },186 6, 7187 ]188 });189 part5();190 }191 function part5() {192 client.setPermissions([1,3]);193 let obj = client.prepareObject(instance, '');194 expect(obj).toEqual({195 a_number: 1.23,196 a_string: 'some string',197 nested1: {198 nested2: {199 nested3: true200 },201 nested2_alt: [0, [0, 1, ['a']], 2]202 },203 an_arr: [204 0, 1,205 { a:'a', b:'b' },206 3, 4,207 { a:'a', b:'b', nestedArr: [{c:'c'}] },208 6, 7209 ]210 });211 part6();212 }213 function part6() {214 client.setPermissions([1,3]);215 let obj = client.prepareObject(instance, 'a_number');216 expect(obj).toEqual(1.23);217 obj = client.prepareObject(instance, 'nested1.nested2');218 expect(obj).toEqual({ nested3: true });219 obj = client.prepareObject(instance, '.an_arr[5].nestedArr[0]');220 expect(obj).toEqual({c:'c'});221 part7();222 }223 function part7() {224 //one crazy sub-property that covers all possible combinations225 let anotherInfrastructure = { allCombinations: { obj1: { obj2: {226 arr1: [0,1,2,227 [228 0,229 [{ obj3: { arr2: [{a:'a',b:'b',c:'c'}, {a:'x',b:'y',c:'z'}, {a:'1',b:'2',c:'3'}] } }],230 2231 ]232 ]233 } } } };234 let proxy = new OH('test', mocks.server, anotherInfrastructure);235 let instance = OH.getInstance(proxy);236 instance.setPermissions('allCombinations.obj1', 4);237 instance.setPermissions('allCombinations.obj1.obj2', 5);238 instance.setPermissions('allCombinations.obj1.obj2.arr1', 6);239 instance.setPermissions('allCombinations.obj1.obj2.arr1[0-3][0]', 7);240 instance.setPermissions('allCombinations.obj1.obj2.arr1[3][2]', 8);241 instance.setPermissions('allCombinations.obj1.obj2.arr1[3][1]', 9);242 instance.setPermissions('allCombinations.obj1.obj2.arr1[3][1][0].obj3', 10);243 instance.setPermissions('allCombinations.obj1.obj2.arr1[3][1][0].obj3.arr2', 11);244 instance.setPermissions('allCombinations.obj1.obj2.arr1[3][1][0].obj3.arr2[0-2].b', 12);245 instance.setPermissions('allCombinations.obj1.obj2.arr1[3][1][0].obj3.arr2[2].c', 13);246 client.setPermissions(0);247 let obj = client.prepareObject(instance, '');248 expect(obj).toEqual({ allCombinations: {} });249 client.setPermissions(4);250 obj = client.prepareObject(instance, '');251 expect(obj).toEqual({ allCombinations: { obj1: {} } });252 client.setPermissions([4,5]);253 obj = client.prepareObject(instance, '');254 expect(obj).toEqual({ allCombinations: { obj1: { obj2: {} } } });255 client.setPermissions([4,5,6]);256 obj = client.prepareObject(instance, '');257 expect(obj).toEqual({ allCombinations: { obj1: { obj2: { arr1: [0,1,2, [undefined,undefined,undefined]] } } } });258 client.setPermissions([4,5,6,7]);259 obj = client.prepareObject(instance, '');260 expect(obj).toEqual({ allCombinations: { obj1: { obj2: { arr1: [0,1,2, [0, undefined, undefined]] } } } });261 client.setPermissions([4,5,6,7,8]);262 obj = client.prepareObject(instance, '');263 expect(obj).toEqual({ allCombinations: { obj1: { obj2: { arr1: [0,1,2, [0, undefined, 2]] } } } });264 client.setPermissions([4,5,6,7,8,9]);265 obj = client.prepareObject(instance, '');266 expect(obj).toEqual({ allCombinations: { obj1: { obj2: { arr1: [0,1,2, [0, [{}], 2]] } } } });267 client.setPermissions([4,5,6,7,8,9,10]);268 obj = client.prepareObject(instance, '');269 expect(obj).toEqual({ allCombinations: { obj1: { obj2: { arr1: [0,1,2, [0, [{ obj3: {} }], 2]] } } } });270 client.setPermissions([4,5,6,7,8,9,10,11]);271 obj = client.prepareObject(instance, '');272 expect(obj).toEqual({ allCombinations: { obj1: { obj2: { arr1: [0,1,2, [0, [{ obj3: { arr2: [{a:'a',c:'c'}, {a:'x',c:'z'}, {a:'1'}] } }], 2]] } } } });273 client.setPermissions([4,5,6,7,8,9,10,11,12]);274 obj = client.prepareObject(instance, '');275 expect(obj).toEqual({ allCombinations: { obj1: { obj2: { arr1: [0,1,2, [0, [{ obj3: { arr2: [{a:'a',b:'b',c:'c'}, {a:'x',b:'y',c:'z'}, {a:'1',b:'2'}] } }], 2]] } } } });276 client.setPermissions([4,5,6,7,8,9,10,11,12,13]);277 obj = client.prepareObject(instance, '');278 expect(obj).toEqual({ allCombinations: { obj1: { obj2: { arr1: [0,1,2, [0, [{ obj3: { arr2: [{a:'a',b:'b',c:'c'}, {a:'x',b:'y',c:'z'}, {a:'1',b:'2',c:'3'}] } }], 2]] } } } });279 client.setPermissions([5,6,7,8,9,10,11,12,13]); //no 4280 obj = client.prepareObject(instance, '');281 expect(obj).toEqual({ allCombinations: {} });282 client.setPermissions([4,5,6, 9]);283 obj = client.prepareObject(instance, '');284 expect(obj).toEqual({ allCombinations: { obj1: { obj2: { arr1: [0,1,2, [undefined, [{}], undefined] ] } } } });285 client.setPermissions([4,5,6, 9, 13]);286 obj = client.prepareObject(instance, '');287 expect(obj).toEqual({ allCombinations: { obj1: { obj2: { arr1: [0,1,2, [undefined, [{}], undefined] ] } } } });288 client.setPermissions([4,5,6, 9,10,11, 13]);289 obj = client.prepareObject(instance, '');290 expect(obj).toEqual({ allCombinations: { obj1: { obj2: { arr1: [0,1,2, [undefined, [{ obj3: { arr2: [{a:'a',c:'c'}, {a:'x',c:'z'}, {a:'1',c:'3'}] } }], undefined] ] } } } });291 done();292 }...
test_osfile_async_setPerms.js
Source:test_osfile_async_setPerms.js
...45 "test_osfile_async_setPerms_nonproto.tmp");46 yield OS.File.writeAtomic(path, new Uint8Array(1));47 try {48 let stat;49 yield OS.File.setPermissions(path, {unixMode: 0o4777});50 stat = yield OS.File.stat(path);51 do_check_modes_eq(stat.unixMode, 0o4777,52 "setPermissions(path, 04777)");53 yield OS.File.setPermissions(path, {unixMode: 0o4777,54 unixHonorUmask: true});55 stat = yield OS.File.stat(path);56 do_check_modes_eq(stat.unixMode, apply_umask(0o4777),57 "setPermissions(path, 04777&~umask)");58 yield OS.File.setPermissions(path);59 stat = yield OS.File.stat(path);60 do_check_modes_eq(stat.unixMode, apply_umask(0o666),61 "setPermissions(path, {})");62 yield OS.File.setPermissions(path, {unixMode: 0});63 stat = yield OS.File.stat(path);64 do_check_modes_eq(stat.unixMode, 0,65 "setPermissions(path, 0000)");66 } finally {67 yield OS.File.remove(path);68 }69});70// Test application to open files.71add_task(function*() {72 // First, create a file we can mess with.73 let path = OS.Path.join(OS.Constants.Path.tmpDir,74 "test_osfile_async_setDates_proto.tmp");75 yield OS.File.writeAtomic(path, new Uint8Array(1));76 try {77 let fd = yield OS.File.open(path, {write: true});78 let stat;79 yield fd.setPermissions({unixMode: 0o4777});80 stat = yield fd.stat();81 do_check_modes_eq(stat.unixMode, 0o4777,82 "fd.setPermissions(04777)");83 yield fd.setPermissions({unixMode: 0o4777, unixHonorUmask: true});84 stat = yield fd.stat();85 do_check_modes_eq(stat.unixMode, apply_umask(0o4777),86 "fd.setPermissions(04777&~umask)");87 yield fd.setPermissions();88 stat = yield fd.stat();89 do_check_modes_eq(stat.unixMode, apply_umask(0o666),90 "fd.setPermissions({})");91 yield fd.setPermissions({unixMode: 0});92 stat = yield fd.stat();93 do_check_modes_eq(stat.unixMode, 0,94 "fd.setPermissions(0000)");95 yield fd.close();96 } finally {97 yield OS.File.remove(path);98 }99});100function run_test() {101 run_next_test();...
Using AI Code Generation
1var rootFolder = DriveApp.getRootFolder();2rootFolder.setPermissions(1234567890, {read: true, write: true, comment: true, manage: true});3rootFolder.setPermissions(1234567890, {read: false, write: false, comment: false, manage: false});4var file = DriveApp.getFileById("1234567890");5file.setPermissions(1234567890, {read: true, write: true, comment: true, manage: true});6file.setPermissions(1234567890, {read: false, write: false, comment: false, manage: false});7var folder = DriveApp.getFolderById("1234567890");8folder.setPermissions(1234567890, {read: true, write: true, comment: true, manage: true});9folder.setPermissions(1234567890, {read: false, write: false, comment: false, manage: false});10var teamDrive = DriveApp.getTeamDriveById("1234567890");11teamDrive.setPermissions(1234567890, {read: true, write: true, comment: true, manage: true});12teamDrive.setPermissions(1234567890, {read: false, write: false, comment: false, manage: false});13var folderInTeamDrive = DriveApp.getFolderById("1234567890");14folderInTeamDrive.setPermissions(1234567890, {read: true, write: true, comment: true, manage: true});15folderInTeamDrive.setPermissions(1234567890, {read: false, write: false, comment: false, manage: false});16var fileInTeamDrive = DriveApp.getFileById("1234567890");17fileInTeamDrive.setPermissions(1234567890, {read: true, write: true, comment: true, manage: true});18fileInTeamDrive.setPermissions(1234567890, {read: false, write: false, comment: false, manage: false});
Using AI Code Generation
1var rootFolder = Folder.root;2rootFolder.setPermissions(777);3var rootFolder = Folder.root;4alert(rootFolder.getPermissions());5var currentFolder = Folder.current;6currentFolder.setPermissions(777);7var currentFolder = Folder.current;8alert(currentFolder.getPermissions());9var folder = new Folder('/home/user1');10folder.setPermissions(777);11var folder = new Folder('/home/user1');12alert(folder.getPermissions());13var file = new File('/home/user1/test.txt');14file.setPermissions(777);15var file = new File('/home/user1/test.txt');16alert(file.getPermissions());17var file = new File('/home/user1/test.txt');18file.setPermissions(777);19var file = new File('/home/user1/test.txt');20alert(file.getPermissions());21var file = new File('/home/user1/test.txt');22file.setPermissions(777);23var file = new File('/home/user1/test.txt');24alert(file.getPermissions());25var file = new File('/home/user1/test.txt');26file.setPermissions(777);27var file = new File('/home/user1/test.txt');28alert(file.getPermissions());29var file = new File('/home/user1/test.txt');30file.setPermissions(777);31var file = new File('/home/user1/test.txt');32alert(file.getPermissions());33var file = new File('/home/user1/test.txt');34file.setPermissions(777);35var file = new File('/home/user1/test.txt');36alert(file.getPermissions());
Using AI Code Generation
1var rootFolder = DriveApp.getRootFolder();2var permissions = rootFolder.getPermissions();3var user = Session.getEffectiveUser();4var email = user.getEmail();5var permission = rootFolder.setPermission(email, DriveApp.Access.READ);6var permissions = rootFolder.getPermissions();7for (var i = 0; i < permissions.length; i++) {8 var permission = permissions[i];9 var email = permission.getEmail();10 var role = permission.getRole();11 var type = permission.getType();12 var expirationDate = permission.getExpirationDate();13 var id = permission.getId();14 Logger.log('Email: ' + email + ', Role: ' + role + ', Type: ' + type + ', Expiration Date: ' + expirationDate + ', Id: ' + id);15}16var rootFolder = DriveApp.getRootFolder();17var permissions = rootFolder.getPermissions();18var user = Session.getEffectiveUser();19var email = user.getEmail();20var permission = rootFolder.setPermission(email, DriveApp.Access.READ);21var permissions = rootFolder.getPermissions();22for (var i = 0; i < permissions.length; i++) {23 var permission = permissions[i];24 var email = permission.getEmail();25 var role = permission.getRole();26 var type = permission.getType();27 var expirationDate = permission.getExpirationDate();28 var id = permission.getId();
Using AI Code Generation
1var rootDir = tizen.filesystem.getStorage('wgt-package');2var directory = rootDir.createDirectory('test');3var file = directory.createFile('test.txt');4var fileURI = file.toURI();5var filePermissions = file.getPermissions();6filePermissions.read = true;7filePermissions.write = true;8filePermissions.execute = true;9file.setPermissions(filePermissions, function() {10 console.log('Permissions set successfully');11}, function(e) {12 console.log('Error: ' + e.message);13});14var rootDir = tizen.filesystem.getStorage('wgt-package');15var directory = rootDir.createDirectory('test');16var file = directory.createFile('test.txt');17var fileURI = file.toURI();18var filePermissions = file.getPermissions();19filePermissions.read = true;20filePermissions.write = true;21filePermissions.execute = true;22tizen.filesystem.setPermissions(fileURI, filePermissions, function() {23 console.log('Permissions set successfully');24}, function(e) {25 console.log('Error: ' + e.message);26});27var rootDir = tizen.filesystem.getStorage('wgt-package');28var directory = rootDir.createDirectory('test');29var file = directory.createFile('test.txt');30var fileURI = file.toURI();31var filePermissions = file.getPermissions();32filePermissions.read = true;33filePermissions.write = true;34filePermissions.execute = true;35file.setPermissions(filePermissions, function() {36 console.log('Permissions set successfully');37}, function(e) {38 console.log('Error: ' + e.message);39});40var rootDir = tizen.filesystem.getStorage('wgt-package');41var directory = rootDir.createDirectory('test');42var file = directory.createFile('test.txt');43var fileURI = file.toURI();44var filePermissions = file.getPermissions();45filePermissions.read = true;46filePermissions.write = true;47filePermissions.execute = true;48file.setPermissions(filePermissions, function() {49 console.log('Permissions set successfully');50}, function(e) {51 console.log('Error: ' + e.message);52});53tizen.filesystem.getPermissions(fileURI, function(permissions) {54 console.log('Permissions: ' + permissions);55}, function(e) {56 console.log('Error: ' + e.message);57});
Using AI Code Generation
1var rootFolder = DriveApp.getRootFolder();2var filesAndFolders = rootFolder.getFilesAndFolders();3while (filesAndFolders.hasNext()) {4 var fileOrFolder = filesAndFolders.next();5 if (fileOrFolder instanceof Folder) {6 fileOrFolder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);7 } else {8 fileOrFolder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);9 }10}11var subFolders = rootFolder.getFolders();12while (subFolders.hasNext()) {13 var subFolder = subFolders.next();14 var filesAndFolders = subFolder.getFilesAndFolders();15 while (filesAndFolders.hasNext()) {16 var fileOrFolder = filesAndFolders.next();17 if (fileOrFolder instanceof Folder) {18 fileOrFolder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);19 } else {20 fileOrFolder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);21 }22 }23}24while (subFolders.hasNext()) {25 var subFolder = subFolders.next();26 subFolder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);27}28rootFolder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!