summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/repo/stores/utils.js
blob: 81638e46672e5830f65650347cb5fe029bfac136 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
export const dataStructure = () => ({
  id: '',
  type: '',
  name: '',
  url: '',
  path: '',
  level: 0,
  tempFile: false,
  icon: '',
  tree: [],
  loading: false,
  opened: false,
  active: false,
  changed: false,
  lastCommit: {},
  tree_url: '',
  blamePath: '',
  commitsPath: '',
  permalink: '',
  rawPath: '',
  binary: false,
  html: '',
  raw: '',
  content: '',
  parentTreeUrl: '',
});

export const decorateData = (entity, projectUrl = '') => {
  const {
    id,
    type,
    url,
    name,
    icon,
    last_commit,
    tree_url,
    path,
    tempFile,
    active = false,
    opened = false,
    changed = false,
    parentTreeUrl = '',
    level = 0,
  } = entity;

  return {
    ...dataStructure(),
    id,
    type,
    name,
    url,
    tree_url,
    path,
    level,
    tempFile,
    icon: `fa-${icon}`,
    opened,
    active,
    parentTreeUrl,
    changed,
    // eslint-disable-next-line camelcase
    lastCommit: last_commit ? {
      url: `${projectUrl}/commit/${last_commit.id}`,
      message: last_commit.message,
      updatedAt: last_commit.committed_date,
    } : {},
  };
};

export const findEntry = (state, type, name) => state.tree.find(
  f => f.type === type && f.name === name,
);
export const findIndexOfFile = (state, file) => state.findIndex(f => f.path === file.path);

export const setPageTitle = (title) => {
  document.title = title;
};

export const pushState = (url) => {
  history.pushState({ url }, '', url);
};

export const createTemp = ({ name, path, type, level, changed, content }) => {
  const treePath = path ? `${path}/${name}` : name;

  return decorateData({
    id: new Date().getTime().toString(),
    name,
    type,
    tempFile: true,
    path: treePath,
    icon: type === 'tree' ? 'folder' : 'file-text-o',
    changed,
    content,
    parentTreeUrl: '',
    level,
  });
};