<commit_before>import client from 'kolibri.client';
import urls from 'kolibri.urls';
import { currentLanguage, createTranslator } from 'kolibri.utils.i18n';
import { Presets, permissionPresets } from '../constants';
import { FacilityImportResource } from '../api';

const SetupStrings = createTranslator('SetupStrings', {
  personalFacilityName: {
    message: 'Home Facility for {name}',
    context:
      "Default facility name when Kolibri is installed with the 'Quick start' setup option for at home learning, outside any type of structure or institution like a school or a library. '{name}' will display the full name of the super admin user for their Kolibri server. Note that users can change this default name after the setup, and put whatever name they want to use for their home facility.",
  },
});

export default {
  state() {
    return {
      started: false,
      // The shape of onboardingData needs to match the DevisionProvisionSerializer
      onboardingData: {
        // If device name is null, it will default to hostname. Blank names aren't allowed.
        device_name: null,
        // Set in DefaultLanguageForm
        language_id: currentLanguage,
        // Set in FacilityPermissionsForm
        facility: {
          name: '',
        },
        preset: null,
        // Set in GuessAccessForm
        allow_guest_access: null,
        // Keys match schema of FacilityDatasetModel
        settings: {
          // Set in CreateLearnerAccountForm
          learner_can_sign_up: null,
          learner_can_edit_name: null,
          learner_can_edit_username: null,
          // Set in RequirePasswordForLearnersForm
          learner_can_login_with_no_password: null,
          learner_can_edit_password: null,
        },
        // Set in SuperuserCredentialsForm
        superuser: {
          full_name: '',
          username: '',
          password: '',
        },
      },
      loading: false,
      error: false,
    };
  },
  actions: {
    logIntoSyncedFacility(store, credentials) {
      return store.dispatch('kolibriLogin', {
        username: credentials.username,
        password: <PASSWORD>,
        facility: credentials.facility,
        disableRedirect: true,
      });
    },
    provisionDeviceAfterImport(store, credentials) {
      const onboardingData = store.state.onboardingData;
      return FacilityImportResource.provisiondevice({
        device_name: onboardingData.device_name,
        language_id: onboardingData.language_id,
      }).then(() => {
        store.dispatch('kolibriLogin', credentials);
      });
    },
    provisionDevice(store) {
      const onboardingData = store.state.onboardingData;

      // Make a copy so data is available when 'kolibriLogin' is called
      const superuser = { ...onboardingData.superuser };

      // Strip out onboarding data so serializer can apply defaults
      if (onboardingData.preset === Presets.PERSONAL) {
        onboardingData.settings = {};
        onboardingData.device_name = null;
        onboardingData.facility.name = SetupStrings.$tr('personalFacilityName', {
          name: store.state.onboardingData.superuser.full_name,
        }).slice(0, 49);
      }

      store.commit('SET_LOADING', true);

      return client({
        url: urls['kolibri:core:deviceprovision'](),
        data: onboardingData,
        method: 'post',
      }).then(
        response => {
          superuser.facility = response.data.facility.id;
          store.dispatch('kolibriLogin', superuser);
        },
        error => {
          store.commit('SET_ERROR', true);
          store.dispatch('handleApiError', error);
        }
      );
    },
    setPersonalUsageDefaults(store) {
      store.commit('SET_FACILITY_PRESET', Presets.PERSONAL);
    },
    setFormalUsageDefaults(store) {
      const defaults = permissionPresets.formal.mappings;
      store.commit('SET_FACILITY_PRESET', Presets.FORMAL);
      store.commit('SET_ALLOW_GUEST_ACCESS', false);
      store.commit('SET_LEARNER_CAN_SIGN_UP', defaults.learner_can_sign_up);
      store.commit(
        'SET_LEARNER_CAN_LOGIN_WITH_NO_PASSWORD',
        defaults.learner_can_login_with_no_password
      );
    },
    setNonformalUsageDefaults(store) {
      const defaults = permissionPresets.nonformal.mappings;
      store.commit('SET_FACILITY_PRESET', Presets.NONFORMAL);
      store.commit('SET_ALLOW_GUEST_ACCESS', true);
      store.commit('SET_LEARNER_CAN_SIGN_UP', defaults.learner_can_sign_up);
      store.commit(
        'SET_LEARNER_CAN_LOGIN_WITH_NO_PASSWORD',<commit_msg>Remove need to login after provisioning as backend has already done it.<commit_after>import client from 'kolibri.client';
import urls from 'kolibri.urls';
import { currentLanguage, createTranslator } from 'kolibri.utils.i18n';
import redirectBrowser from 'kolibri.utils.redirectBrowser';
import { Presets, permissionPresets } from '../constants';
import { FacilityImportResource } from '../api';

const SetupStrings = createTranslator('SetupStrings', {
  personalFacilityName: {
    message: 'Home Facility for {name}',
    context:
      "Default facility name when Kolibri is installed with the 'Quick start' setup option for at home learning, outside any type of structure or institution like a school or a library. '{name}' will display the full name of the super admin user for their Kolibri server. Note that users can change this default name after the setup, and put whatever name they want to use for their home facility.",
  },
});

export default {
  state() {
    return {
      started: false,
      // The shape of onboardingData needs to match the DevisionProvisionSerializer
      onboardingData: {
        // If device name is null, it will default to hostname. Blank names aren't allowed.
        device_name: null,
        // Set in DefaultLanguageForm
        language_id: currentLanguage,
        // Set in FacilityPermissionsForm
        facility: {
          name: '',
        },
        preset: null,
        // Set in GuessAccessForm
        allow_guest_access: null,
        // Keys match schema of FacilityDatasetModel
        settings: {
          // Set in CreateLearnerAccountForm
          learner_can_sign_up: null,
          learner_can_edit_name: null,
          learner_can_edit_username: null,
          // Set in RequirePasswordForLearnersForm
          learner_can_login_with_no_password: null,
          learner_can_edit_password: null,
        },
        // Set in SuperuserCredentialsForm
        superuser: {
          full_name: '',
          username: '',
          password: '',
        },
      },
      loading: false,
      error: false,
    };
  },
  actions: {
    logIntoSyncedFacility(store, credentials) {
      return store.dispatch('kolibriLogin', {
        username: credentials.username,
        password: <PASSWORD>,
        facility: credentials.facility,
        disableRedirect: true,
      });
    },
    provisionDeviceAfterImport(store, credentials) {
      const onboardingData = store.state.onboardingData;
      return FacilityImportResource.provisiondevice({
        device_name: onboardingData.device_name,
        language_id: onboardingData.language_id,
      }).then(() => {
        store.dispatch('kolibriLogin', credentials);
      });
    },
    provisionDevice(store) {
      const onboardingData = store.state.onboardingData;

      // Strip out onboarding data so serializer can apply defaults
      if (onboardingData.preset === Presets.PERSONAL) {
        onboardingData.settings = {};
        onboardingData.device_name = null;
        onboardingData.facility.name = SetupStrings.$tr('personalFacilityName', {
          name: store.state.onboardingData.superuser.full_name,
        }).slice(0, 49);
      }

      store.commit('SET_LOADING', true);

      return client({
        url: urls['kolibri:core:deviceprovision'](),
        data: onboardingData,
        method: 'post',
      }).then(
        () => {
          redirectBrowser();
        },
        error => {
          store.commit('SET_ERROR', true);
          store.dispatch('handleApiError', error);
        }
      );
    },
    setPersonalUsageDefaults(store) {
      store.commit('SET_FACILITY_PRESET', Presets.PERSONAL);
    },
    setFormalUsageDefaults(store) {
      const defaults = permissionPresets.formal.mappings;
      store.commit('SET_FACILITY_PRESET', Presets.FORMAL);
      store.commit('SET_ALLOW_GUEST_ACCESS', false);
      store.commit('SET_LEARNER_CAN_SIGN_UP', defaults.learner_can_sign_up);
      store.commit(
        'SET_LEARNER_CAN_LOGIN_WITH_NO_PASSWORD',
        defaults.learner_can_login_with_no_password
      );
    },
    setNonformalUsageDefaults(store) {
      const defaults = permissionPresets.nonformal.mappings;
      store.commit('SET_FACILITY_PRESET', Presets.NONFORMAL);
      store.commit('SET_ALLOW_GUEST_ACCESS', true);
      store.commit('SET_LEARNER_CAN_SIGN_UP', defaults.learner_can_sign_up);
      store.commit(
        'SET_LEARNER_CAN_LOGIN_WITH_NO_PASSWORD',
{'fraction_non_alphanumeric': 0.09652723843337402, 'fraction_numerical': 0.0011095084877399313, 'mean_word_length': 2.779454926624738, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4822965', 'n_tokens_mistral': 2586, 'n_tokens_neox': 2546, 'n_words': 590}