// @flow
import React from 'react';
import { reduxForm, Field } from 'redux-form';
import type { FieldProps } from 'redux-form';
import { Button, Form } from 'app/components/Form';
import TextInput from 'app/components/Form/TextInput';
import { createValidator, matchesRegex, required } from 'app/utils/validation';
import { omit } from 'lodash';

type Props = FieldProps & {
  group: Object
};

const AddGroupPermission = ({
  editGroup,
  submitting,
  group,
  handleSubmit
}: Props) => {
  const onSubmit = handleSubmit(({ permission }) =>
    editGroup({
      ...omit(group || {}, 'logo'),
      permissions: group.permissions.concat(permission)
    })
  );

  return (
    <Form onSubmit={onSubmit}>
      <h3>Legg til ny rettighet</h3>
      <Field
        label="Rettighet"
        name="permission"
        placeholder="/sudo/admin/events/create/"
        component={TextInput.Field}
      />

      <Button submit disabled={submitting}>
        Legg til rettighet
      </Button>
    </Form>
  );
};

const validate = createValidator({
  permission: [
    required(),
    matchesRegex(
      /^\/([a-zA-Z]+\/)+$/,
      'Rettigheter kan bare inneholde skråstrek og bokstaver, og må begynne og ende med en skråstrek.'
    )
  ]
});

export default reduxForm({
  form: 'add-permission',
  validate
})(AddGroupPermission);

@@ -41,19 +41,8 @@
 rt %7B
- reduxForm,
  Fie
@@ -80,20 +80,19 @@
 type %7B F
-ield
+orm
 Props %7D 
@@ -118,16 +118,26 @@
 import %7B
+ legoForm,
  Button,
@@ -353,20 +353,19 @@
 rops = F
-ield
+orm
 Props & 
@@ -415,18 +415,16 @@
 ion = (%7B
-%0A 
  editGro
@@ -426,18 +426,16 @@
 itGroup,
-%0A 
  submitt
@@ -442,19 +442,8 @@
 ing,
-%0A  group,%0A 
  han
@@ -451,17 +451,17 @@
 leSubmit
-%0A
+ 
 %7D: Props
@@ -469,196 +469,10 @@
  =%3E 
-%7B%0A  const onSubmit = handleSubmit((%7B permission %7D) =%3E%0A    editGroup(%7B%0A      ...omit(group %7C%7C %7B%7D, 'logo'),%0A      permissions: group.permissions.concat(permission)%0A    %7D)%0A  );%0A%0A  return (%0A  
+(%0A
   %3CF
@@ -485,18 +485,22 @@
 Submit=%7B
-on
+handle
 Submit%7D%3E
@@ -504,18 +504,16 @@
 t%7D%3E%0A    
-  
 %3Ch3%3ELegg
@@ -535,18 +535,16 @@
 et%3C/h3%3E%0A
-  
     %3CFie
@@ -552,18 +552,16 @@
 d%0A      
-  
 label=%22R
@@ -570,18 +570,16 @@
 tighet%22%0A
-  
       na
@@ -600,18 +600,16 @@
 %22%0A      
-  
 placehol
@@ -647,18 +647,16 @@
 %22%0A      
-  
 componen
@@ -683,18 +683,16 @@
     
-  
 /%3E%0A%0A    
   %3CB
@@ -687,18 +687,16 @@
 /%3E%0A%0A    
- 
- 
 %3CButton 
@@ -731,18 +731,16 @@
 %3E%0A      
-  
 Legg til
@@ -754,18 +754,16 @@
 het%0A    
-  
 %3C/Button
@@ -764,18 +764,16 @@
 Button%3E%0A
-  
   %3C/Form
@@ -778,16 +778,10 @@
 rm%3E%0A
-  );%0A%7D;%0A
+);
 %0Acon
@@ -1023,21 +1023,20 @@
 default 
-redux
+lego
 Form(%7B%0A 
@@ -1060,16 +1060,257 @@
 ssion',%0A
+  onSubmit: (%7B permission %7D, dispatch, %7B group, editGroup %7D) =%3E%0A    editGroup(%7B%0A      ...omit(group %7C%7C %7B%7D, 'logo'),%0A      permissions: group.permissions.concat(permission)%0A    %7D),%0A  onSubmitSuccess: (result, dispatch, %7B reset %7D) =%3E reset(),%0A
   valida

Reset permission form after submit